As follows:
public class DataStructure {
//create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() {
//fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = i;
}
}
public void printEven() {
//print out values of even indices of the array
InnerEvenIterator iterator = this.new InnerEvenIterator();
while (iterator.hasNext()) {
System.out.println(iterator.getNext() + " ");
}
}
//inner class implements the Iterator pattern
private class InnerEvenIterator {
//start stepping through the array from the beginning
private int next = 0;
public boolean hasNext() {
//check if a current element is the last in the array
return (next <= SIZE - 1);
}
public int getNext() {
//record a value of an even index of the array
int retValue = arrayOfInts[next];
//get the next even element
next += 2;
return retValue;
}
}
public static void main(String s[]) {
//fill the array with integer values and print out only values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}问题:
红色部分,为什么用this.new, 与new的区别?
public class DataStructure {
//create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() {
//fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = i;
}
}
public void printEven() {
//print out values of even indices of the array
InnerEvenIterator iterator = this.new InnerEvenIterator();
while (iterator.hasNext()) {
System.out.println(iterator.getNext() + " ");
}
}
//inner class implements the Iterator pattern
private class InnerEvenIterator {
//start stepping through the array from the beginning
private int next = 0;
public boolean hasNext() {
//check if a current element is the last in the array
return (next <= SIZE - 1);
}
public int getNext() {
//record a value of an even index of the array
int retValue = arrayOfInts[next];
//get the next even element
next += 2;
return retValue;
}
}
public static void main(String s[]) {
//fill the array with integer values and print out only values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}问题:
红色部分,为什么用this.new, 与new的区别?
new 普通类的创建方法
假设一下,你把InnerEvenIterator类改成public的。
你在外面可以这样调用了 DataStructure ds = new DataStructure();
InnerEvenIterator =ds.new InnerEvenIterator();
这里的ds.new其实和上面那个this.new一样的,前面都指代对象
但是我还是有个小问题:
试了下用new,也可以正常运行得到结果。问题:
1.加this和不加编译,或者运行时,有什么区别。
2.可否这么理解:this.new看起来就是说每个类里面自己都有一个new,就像类似constructor?
比方2个不同的类里面,其实new是不一样。
3.如果2成立,又回到1,那么上面的代码不加this,难道不是也相当于this.new?
例如你这里的arrayOfInts属性,在一个对象里面,其实默认this.arrayOfInts也是可以的。方法也一样。this就是代表对象本身。所以在对象内部,加不加this都一样。2、这一点我在第一点中已经说明白了,任何对象内部的属性和方法都默认有this.前缀。3、之所以这里可以使用this.new是因为 这里InnerEvenIterator是this这个对象本身的一个内部类,所以this就是引用对象本身。加不加都可以。如果InnerEvenIterator这个内部类是其他的类的内部类,你在这里加了this也没用,会出错
谢谢lovingPrince哈。然后引申问题:如下:public class Test{
public static void main(String s[]) { DataStructure ds1 = new DataStructure();
ds1.printEven();
DataStructure.InnerEvenIterator Inner1 = ds1.new InnerEvenIterator();
DataStructure ds2 = new DataStructure();
ds2.printEven();
DataStructure.InnerEvenIterator Inner2 = ds2.new InnerEvenIterator();
}
}如你所说,我必须先instance DataStructure,才能用到innerclass问题:1.是否意味着:A:Inner1 属于ds1.Inner2 属于 ds2?
B:还是指这里的Inner1,Inner2只是借壳产生,产生后与ds1,ds2无关?
2.比较倾向于理解为A。那么销毁对象ds1,Inner1会被销毁了?
由于还没有看到后面的drop instance,你直接告诉我个答案哈,我以后再自己验证。谢谢。
2、如果你的Inner1没有销毁,那么ds1所引用的对象不可能销毁掉。反过来如果你的Inner1销毁了。ds1不一定销毁了。
结果Inner1和ds1无关(就是说Inner1看ds1和ds2是一样的结果)。
不过Inner1调用的时候并不需要ds1.Inner1.method,而是Inner1.method.又表示Inner1独立于ds1.2.我觉得你这里有问题。既然Inner1与ds1无关,就不可能:“如果你的Inner1没有销毁,那么ds1所引用的对象不可能销毁掉”
如果再当前类中初始化,要不要THIS都无所谓
public static void main(String s[]) {
DataStructure ds1 = new DataStructure();
DataStructure.InnerEvenIterator Inner1 = ds1.new InnerEvenIterator();
ds1 =null;
Inner1.PrintInnerclass(); //method in class:InnerEvenIterator
}
}问题:
1.没找到如何销毁对象,不知道ds1 =null;将指针置为null,是否实现了销毁对象?
2.如果ds1 = null,表示销毁对象,那么就表示不销毁Inner1可以销毁ds1.
3.如果ds1 = null不表示销毁对象,就是ds1的所占的内存还在,那么ds1 = null之后。
如何再找到原来的ds1所指向的内存。
想弄清楚,否则不舒服。真正用的时候自然不会这么用。要外部用就不用搞内部类了。
谢谢提示哈
如下:
public static void main(String s[]) {
DataStructure ds1 = new DataStructure();
DataStructure.InnerEvenIterator Inner1 = ds1.new InnerEvenIterator();
ds1 =null;
Inner1.PrintInnerclass(); //method in class:InnerEvenIterator
}
} 问题:
1.没找到如何销毁对象,不知道ds1 =null;将指针置为null,是否实现了销毁对象?
2.如果ds1 = null,表示销毁对象,那么就表示不销毁Inner1可以销毁ds1?
3.如果ds1 = null不表示销毁对象,就是ds1的所占的内存还在,那么ds1 = null之后。
如何再找到原来的ds1所指向的内存?
1)ds1 =null;将指针置为null,是否实现了销毁对象? 不会销毁
2)由于1),所以2)不成立
3)问题真正的核心是:每一个InnerEvenIterator内部类的对象,内部都有一个指针(即:引用),指向它的外部类的对象.由于这个引用已保在内部类的对象中,在内部类中如何访问这个引用?用 DataStructure.this 就行了(格式是:外部类的类名.this)
若在内部类之外要访问,可在内部类中暴露出一个public方法来返回这个DataStructure.this 就行了.
结论:在内部类的对象没有销毁之前,是不可能销毁外部类的对象的(因为:内部类的对象还有一个引用正拉住它呢.)
感谢大家的耐心。