繼承
public class Animal
{
????int height;
????int weight;
????void animal()
????{
????????System.out.println("Animal constract");
????}
????void eat()
????{
????????System.out.println("Animal eat");
????}
????void sleep()
????{
????????System.out.println("Animal sleep");
????}
????void breathe()
????{
????????System.out.println("Animal breathe");
????}
}
/*
* 理解繼承是理解面向對象程序設計的關鍵
* 在java中,通過關鍵字extends繼承一個已有的類,被繼承的類稱為父類(超類,基類),新的類稱為子類(派生類)。
* * 在java中,不允許多繼承
*/
class Fish extends Animal
{
????void fish()
????{
????????
????????System.out.println("fish constract");
????}
????void breathe()
????{
????????//super.breathe();
????????//super.height=40;
????????System.out.println("fish boo");
????}
}
class Integration
{
????public static void main(String[]args)
????{
????????//Animal an=new Animal();
????????Fish fh=new Fish();
????????//an.breathe();
????????//fh.height=30;
????????fh.breathe();
????????
????}
}
/*
*在子類當中定義一個與父類同名,返回類型,參數類型均一致的方法,稱為方法的覆蓋
*方法的覆蓋發生在子類和父類之間。
*調用父類的方法使用super
*/
/*特殊變量super,提供了父類的訪問
* 可以使用super訪問被父類被子類隱藏的變量或覆蓋的方法
* 每個子類構造方法的第一句,都是隱藏的調用super(),如果父類沒有這種形式的構造函數,那么在編譯器中就會報錯。
*
*
*
*/