类的多态
调用属性: 编译看左边,运行也看左边
调用方法: 编译看左边,运行看右边
class A {
int m = 10;
public void hello() {
System.out.println("BBB hello");
}
public void sayHi() {
System.out.println("BBB hi");
}
}
class B extends A {
int m = 20;
public void hello() {
System.out.println("hello");
}
public void sayHi() {
System.out.println("hi");
}
public void sayHi2() {
System.out.println("hi2");
}
public static void main(String[] args) {
//子类的引用赋值给 父类的变量
A f = new B();
//调用属性: 编译看左边,运行也看左边
System.out.println(f.m);
//调用方法: 编译看左边,运行看右边
f.sayHi();
}
}
接口的多态
调用方法: 编译看左边,运行看右边
interface AA{
public void hello();
public void sayHi();
}
class B implements AA {
int m = 20;
public void hello() {
System.out.println("hello");
}
public void sayHi() {
System.out.println("hi");
}
public void sayHi2() {
System.out.println("hi2");
}
public static void main(String[] args) {
//子类的引用赋值给 父类的变量
AA f = new B();
//调用方法: 编译看左边,运行看右边
f.sayHi();
}
}