【发布时间】:2014-12-17 03:40:07
【问题描述】:
在下面的代码sn-p中,我知道在Counter c = new Counter();这个语句中,
声明了一个新的引用变量,并且在调用类构造函数时,new 运算符将一个内存位置放入其中,但我不明白其余部分!
实际上是否可以在其中定义构造函数主体,在调用时使用{ }!
是否可以在构造函数中定义方法?
public class Counter {
private int counter = 0;
public int count() {
return counter++;
}
}
然后在另一个类中我们有:
Counter c = new Counter() {
public int count() {
super.count();
return super.count();
}
}
【问题讨论】:
标签: java oop constructor initialization