class ParentClass { public static int a=2; public int b=3; { System.out.println("this is anonymity b="+b); } static { a=4; System.out.println("this is static and a="+a); } public ParentClass() { System.out.println("this is parent gozao"); this.s(); } public void s() { System.out.println("this is parent"); } } public class Son extends ParentClass { public Son(){ System.out.println("this is son gozao"); } public static void main(String[] args) { ParentClass d = new Son(); d.s(); } public void s() { //super.s(); System.out.println("this is son"); } }
this is static and a=4 this is anonymity this is parent gozao this is son this is son gozao this is son
public final class MyImmutableWrong { // 使用final关键字声明为强不可变类 private final int[] myArray; public MyImmutableWrong(int[] anArray) { this.myArray = anArray; // wrong } public String toString() { StringBuffer sb = new StringBuffer("Numbers are: "); for (int i = 0; i < myArray.length; i++) { sb.append(myArray[i] + " "); } return sb.toString(); } }由于int[] anArray是一个数组,属于引用类型。这样当再次去操作外部的一个引用时,其指向的共同内容,也就是数组的值。
public class dd { public static void main(String[] args) { int array[]={1,2,3,4}; MyImmutableWrong service=new MyImmutableWrong(array); array[2]=99; System.out.println(service.toString()); } }查看控制台的输出内容:Numbers are: 1 2 99 4
public final class MyImmutableCorrect { private final int[] myArray; public MyImmutableCorrect(int[] anArray) { this.myArray = anArray.clone(); } public String toString() { StringBuffer sb = new StringBuffer("Numbers are: "); for (int i = 0; i < myArray.length; i++) { sb.append(myArray[i] + " "); } return sb.toString(); } }测试后发现:Numbers are: 1 2 3 4
public class Author { private final String name; public String getName() { return name; } public Author(String name_) { name = name_; } @Override public String toString() { return "Author [name=" + name + "]"; } }
public final class Update { // 强不可变类 private final Author author; // 作者,是个引用变量 private final String updateText; // 更新内容 private final long createTime; // 更新时间 // 私有构造函数,防止外部实例化 private Update(Builder b_) { author = b_.author; updateText = b_.updateText; createTime = b_.createTime; } // 构建器 public static class Builder { private long createTime; private Author author; private String updateText; public Builder author(Author author_) { author = author_; return this; } public Builder updateText(String updateText_) { updateText = updateText_; return this; } public Builder createTime(long createTime_) { createTime = createTime_; return this; } public Update build() {// 更新外部类的值 return new Update(this); } } public Author getAuthor() { return author; } public String getUpdateText() { return updateText; } @Override public String toString() { return "Update [author=" + author + ", updateText=" + updateText + ", createTime=" + createTime + "]"; } }可以看到,静态内部类有与外部类同样的成员变量,但是静态内部类中的成员变量却是可以修改的。
public class Test { public static void main(String[] args) { final Update first = getUpdate("abc");// 获取不可变类的实例 System.out.println(first.toString()); } private static Update getUpdate(String s) { Update.Builder b = new Update.Builder(); b.updateText(s).author(new Author("mazhi")); return b.build();// 初始化不可变类并返回实例 } }运行后的结果如下:
Update.Builder b = new Update.Builder(); // 获取静态内部类的实例对象
由于不依赖于外部类的实例对象,所以能访问外部类的非static成员变量。
想了解更多的内部类,可以查阅其它资料。
原文:http://blog.csdn.net/mazhimazh/article/details/18910217