JAVA菜鸟入门 (19) inner calss: static v.s. non-static在main中被实例化时的区别

本文通过四个示例对比讲解了Java中内类和静态内类的区别及应用场景,重点介绍了如何利用它们实现产品类实例的排序功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 

首先, class中的class粗略地分成2类, static v.s non-static."Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes." from stackoverflow.

用一个例子来demo二者的使用区别。

假设有一个类,叫做Product,里面有2个field。其中field name, 代表产品的名字,然后有field price,代表产品的价格。现在需要将一堆产品的实例按照价格从高到低进行排序。

比如 prouduct 1 (name = "apple", price = 2), product 2 (name = "orange", price = 9), 那么价格降序后将是product2, product1。


2. Example One

没有使用inner class的写法

package com.leetcode.TEST1;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by feliciafay on 10/24/15.
 */
public class Product {
    String name;
    int price;
    public Product() {
    }
    public Product(String s, int p) {
        name = s;
        price = p;
    }
    public String toString() {
        return  name + ":" + price;
    }
   public  class Mycomparator implements Comparator<Product> {
        public int compare(Product o1, Product o2) {
            if (o1.price == o2.price) {
                return 0;
            } else {
                return o2.price - o1.price;
            }
        }
    }
    public void sortProduct( ArrayList<Product> list) {
        Collections.sort(list, new Mycomparator());
        return;
    }
    public static void main (String [] args) {
        Product p = new Product();
        ArrayList<Product> list = new ArrayList<Product> ();
        list.add(new Product("a", 2));
        list.add(new Product("b", 7));
        list.add(new Product("c", 3));
        list.add(new Product("d", 15));
        list.add(new Product("e", 9));
        list.add(new Product("f", 30));
        list.add(new Product("g", 2));
        list.add(new Product("h", 15));
        list.add(new Product("i", 2));;
        p.sortProduct(list);
        System.out.print(Arrays.toString(list.toArray()));
    }
}

输出:  降序排序的product。

[f:30, d:15, h:15, e:9, b:7, c:3, a:2, g:2, i:2]




3. Example Two

Product2是inner class, 最外层的类是Solution,它包含了Inner class, Product2.

public class Solution {
    public class Product2 {
        private String name;
        private int price;
        Product2(){}
        Product2(String a , int b){
            name = a;
            price = b;
        }
        public String toString() {
            return  name + ":" + price;
        }
    }

    public  class Mycomparator implements Comparator<Product2> {
        public int compare(Product2 o1, Product2 o2) {
            if (o1.price == o2.price) {
                return 0;
            } else {
                return o2.price - o1.price;
            }
        }
    }

    public void sortProduct( ArrayList<Product2> list) {
        Collections.sort(list, new Mycomparator());
        return;
    }

    public static void main (String [] args) {
        Solution solution = new Solution();
        ArrayList<Product2> list = new ArrayList<Product2> ();
        list.add(solution.new Product2("aa", 2));
        list.add(solution.new Product2("bb", 7));
        list.add(solution.new Product2("cc", 3));
        list.add(solution.new Product2("dd", 15));
        list.add(solution.new Product2("ee", 9));
        list.add(solution.new Product2("ff", 30));
        list.add(solution.new Product2("gg", 2));
        list.add(solution.new Product2("hh", 15));
        list.add(solution.new Product2("ii", 2));
        solution.sortProduct(list);
        System.out.print(Arrays.toString(list.toArray()));
    }
}
输出 :

[ff:30, dd:15, hh:15, ee:9, bb:7, cc:3, aa:2, gg:2, ii:2]


4. Example Three

使用了static inner class,  Product2 是Solution的inner class

public class Solution {
    public static class Product2 {
        private String name;
        private int price;
        Product2(){}
        Product2(String a , int b){
            name = a;
            price = b;
        }
        public String toString() {
            return  name + ":" + price;
        }
    }

    public  class Mycomparator implements Comparator<Product2> {
        public int compare(Product2 o1, Product2 o2) {
            if (o1.price == o2.price) {
                return 0;
            } else {
                return o2.price - o1.price;
            }
        }
    }

    public void sortProduct( ArrayList<Product2> list) {
        Collections.sort(list, new Mycomparator());
        return;
    }

    public static void main (String [] args) {
        Solution solution = new Solution();
        ArrayList<Product2> list = new ArrayList<Product2> ();
        list.add(new Product2("aaa", 2));
        list.add(new Product2("bbb", 7));
        list.add(new Product2("ccc", 3));
        list.add(new Product2("ddd", 15));
        list.add(new Product2("eee", 9));
        list.add(new Product2("fff", 30));
        list.add(new Product2("ggg", 2));
        list.add(new Product2("hhh", 15));
        list.add(new Product2("iii", 2));
        solution.sortProduct(list);
        System.out.print(Arrays.toString(list.toArray()));
    }
}

输出

[fff:30, ddd:15, hhh:15, eee:9, bbb:7, ccc:3, aaa:2, ggg:2, iii:2]

所以,注意在inner class(i.e. non-static class) 和 static nested class之间,new出instance的时候,语法有一些小区别。


5. Example Four

还在可以把class Product2写出来作为和Solution class,也就是main函数入口的class平级的class。这个是通用的写法,不举例了。


6. Inner Class的好处。

信息隐藏和分组, 可能inner class的内容只需要被它的outer class用到,这样设定为inner之后,外界就看不到了。



参考文章:

1. http://stackoverflow.com/questions/13044983/when-is-it-ok-to-create-object-of-a-class-inside-a-method-of-that-class

2. http://blog.youkuaiyun.com/feliciafay/article/details/45415295

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值