主题:求助 java初始化的问题
package Test13;
abstract class Base{
Base(){
print();
}
abstract void print();
}
class Derived extends Base{
int i=11;
void print(){
System.out.println("The Derived i:"+i);
}
}
class Test13{
public static void main(String[] args)
{
Derived d=new Derived();
d.print();
}
}
输出的是
The Derived i:0
The Derived i:11
这个我理解,因为在Base类中Print()是抽象方法,Base的构造函数print()就唤醒了子类中Derived的print()方法
但是为什么这么写也和上面得到的结果一样了:
package Test13;
class Base{
Base(){
print();
}
void print(){
system.out.println("This is Base Print()");
}
}
class Derived extends Base{
int i=11;
void print(){
System.out.println("The Derived i:"+i);
}
}
class Test13{
public static void main(String[] args)
{
Derived d=new Derived();
d.print();
}
}
输出的是
The Derived i:0
The Derived i:11
而不是:
This is Base Print()
The Derived i:11
这是为什么了,那个Base类中明明有一个print()为什么不调用
反去调用Derived()
哪位懂的大虾请指点下,谢谢了[em1]
abstract class Base{
Base(){
print();
}
abstract void print();
}
class Derived extends Base{
int i=11;
void print(){
System.out.println("The Derived i:"+i);
}
}
class Test13{
public static void main(String[] args)
{
Derived d=new Derived();
d.print();
}
}
输出的是
The Derived i:0
The Derived i:11
这个我理解,因为在Base类中Print()是抽象方法,Base的构造函数print()就唤醒了子类中Derived的print()方法
但是为什么这么写也和上面得到的结果一样了:
package Test13;
class Base{
Base(){
print();
}
void print(){
system.out.println("This is Base Print()");
}
}
class Derived extends Base{
int i=11;
void print(){
System.out.println("The Derived i:"+i);
}
}
class Test13{
public static void main(String[] args)
{
Derived d=new Derived();
d.print();
}
}
输出的是
The Derived i:0
The Derived i:11
而不是:
This is Base Print()
The Derived i:11
这是为什么了,那个Base类中明明有一个print()为什么不调用
反去调用Derived()
哪位懂的大虾请指点下,谢谢了[em1]