In food.java:
public
class
food {
public
void
eat(){ →必須是public的,因為private不能使用
System.out.println("I
am the eat methods");
}
}
In
potpie.java and tuna.java:
public
class
tuna extends
food{
public
void
eat(){
System.out.println("I
am the new method of tuna");
→將覆寫方法
}
}
public
class
potpie extends
food{ →
extends的意思是繼承
food.java
}
In
apple.java:
class
apples{
public
static
void
main(String[] args){
tuna
tunaObject = new
tuna();
potpie
potObject = new
potpie();
tunaObject.eat();
potObject.eat();
}
}
這是運行時的結果:
I
am the new method of tuna
I
am the eat methods
沒有留言:
張貼留言