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

来源:互联网 发布:电国网络电视直播台 编辑:程序博客网 时间:2024/06/01 08:45

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.csdn.net/feliciafay/article/details/45415295

0 0