0
点赞
收藏
分享

微信扫一扫

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


对初学者来说,两个比较好的例子,写出JAVA下的输出结果
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);
 }
}

举报

相关推荐

0 条评论