<strong><span style="font-size:32px;color:#3333ff;">Java多态及静态方法对动态方法的调用</span></strong>
例子:
public class FirstJava {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
aBird a = new aBird();
bBird b = new bBird();
dosomething(a);
dosomething(b);
}
static void dosomething(Bird bird){
bird.fly();
}
}
abstract class Bird{<span style="white-space:pre"> </span>//创建一个父类
void fly() { //必须带括号
}
}
class aBird extends Bird{<span style="white-space:pre"> </span>//子类继承父类
void fly(){
System.out.print("i am a abird; ");
}
}
class bBird extends Bird{<span style="white-space:pre"> </span>//子类继承父类
void fly(){
System.out.print("i am a bbird; ");
}
}