您的位置:首页 > 其它

小心下面两个程序的输出结果

2005-11-02 09:29 302 查看
对初学者来说,两个比较好的例子,写出J***A下的输出结果
1)
public class Test {
public static void main(String[] args) {
Child child = new Child();
}
}

class Parent {
Parent() {
System.out.println("to construct Parent.");
}
}

class Child extends Parent {
Child() {
System.out.println("to construct Child.");
}

Delegatee delegatee = new Delegatee();
}

class Delegatee {
Delegatee() {
System.out.println("to construct Delegatee.");
}
}

2)
class Base {
Base() {
System.out.println("Base() before print()");
print();
System.out.println("Base() after print()");
}
public void print() {
System.out.println("Base.print()");
}
}
class Derived extends Base {
int value;
Derived(int val) {
value = val;
System.out.println("Derived() with " + value);
}
public void print() {
System.out.println("Derived.print() with " + value);
}
}
public class Polymorphism {
public static void main(String[] args) {
new Derived(123);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐