java繼承屬性相關介紹

來源:互聯網
上載者:User

標籤:[]   string   sys   繼承   pack   color   new   col   print   

這個只需要記住一點,父類的任何屬性(變數可以看做屬性),子類均可繼承並覆蓋,allType(father)——》changeAnyType(son)——》AnyType

這是父類的所有代表屬性(我能想到的),包含普通執行個體變數,修飾符,存取權限,資料類型,常量修飾符,靜態修飾符

package ExtendVariable;

package ExtendVariable;public class Ancestor {//父類            int ix=0;//普通執行個體成員屬性    double dx = 1;//不同類型執行個體成員變數    int bx=10000;        final int fy=2;//普通常量成員屬性        static int sz=3;//普通靜態成員屬性        private     int priVar = 4;//不同許可權的成員屬性                int defVar = 5;//預設許可權    protected     int proVar = 6;    public        int pubVar = 7;    }

 

 

子類通過extend繼承父類之後,通過更改相關屬性

package ExtendVariable;public class son extends Ancestor{        //將int型改為double,將double改為int,將int型改為布爾型    double ix = 0.1;    int    dx = 10;    boolean bx=true;        //將普通常量變數改為普通變數    int    fy = 20;        //將靜態成員變數改為普通執行個體成員變數    int     sz=30;        //將所有許可權顛倒過來    public         int priVar=40;    protected      int    defVar=50;                int proVar=60;    private     int pubVar=70;        public  void showAllMember() {        System.out.println("ix= "+this.ix);        System.out.println("dx= "+this.dx);        System.out.println("bx= "+this.bx);        System.out.println("fy= "+this.fy);        System.out.println("sz= "+this.sz);        System.out.println("priVar= "+this.priVar);        System.out.println("defVar= "+this.defVar);        System.out.println("proVar= "+this.proVar);        System.out.println("proVar= "+this.pubVar);    }        public static void main(String args[]) {        son oa = new son();        oa.showAllMember();    }    }

 

得出結果為:

ix= 0.1
dx= 10
bx= true
fy= 20
sz= 30
priVar= 40
defVar= 50

這裡說明父類的任何屬性均可被子類繼承並任意覆蓋

java繼承屬性相關介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.