2013年7月20日 星期六

多型



class ClassA {
void method1() {
System.out.println("method1() in ClassA");
}
void method2() {
System.out.println("method2() in ClassA");
}
}

class ClassB extends ClassA {
void method1() {
System.out.println("method1() in ClassB");
}
void method3() {
System.out.println("method3() in ClassB");
}
}

public class ClassC {
public static void main(String[] args) {
ClassA a = new ClassB();
a.method1(); // method1() in ClassB
a.method2(); // method2() in ClassA
a.method3(); // compile-time error
}
}


object a reference type is ClassA

object a obect type is ClassB

沒有留言:

張貼留言