
public class NewClass {
public static void main(String[] args) {
A a=new B();
a.ppp(); //錯誤 因為a是使用A的定義 A中沒定義ppp()
}
}
class A{
}
class B extends A {
void ppp(){
System.out.println("here");
}
}
public class NewClass {
public static void main(String[] args) {
A a=new B();
a.ppp(); // 因為是B的身體 所以會印出here
}
}
class A{
void ppp(){
System.out.println("there");
}
}
class B extends A {
void ppp(){
System.out.println("here");
}
}
public class NewClass {
public static void main(String[] args) {
B b=new B();
System.out.print(b.p);
A a=new B();
System.out.print(a.p);//override只是方法 變數不會被override
}
}
class A{
int p=90;
}
class B extends A {
int p=80;
}