Java--父類的引用指向子類的對象詳解!

來源:互聯網
上載者:User

標籤:方法   xtend   ceo   通過   class   code   ring   string   sys   

例:

  第一步.建立一個Person類

package com.maya.ball;public class Person {    private int age;    private String name;        public Person(int age,String name){        this.age = age;        this.name = name;    }        public void speak(){            }    public int getAge(){        return age;    }    public void setAge(int age){        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

  第二步.建立American類繼承Person類。

package com.maya.basketball;import com.maya.ball.Person;public class American extends Person {    public double height;-----------------------------新增成員變數        public American(int age,String name,double height){        super(age, name);        this.height = height;    }    public void speak(){        System.out.println("我年齡:"+ this.getAge() +",我叫:"+getName()+",我身高:"+height);    }}

  第三步.建立Chinese類,同意繼承Person類

package com.maya.football;import com.maya.ball.Person;public class Chinese extends Person{    private String sex;-------------------新增成員變數        public Chinese(int age, String name,String sex) {        super(age, name);        this.sex = sex;    }        public void speak(){        System.out.println("我年齡:"+ getAge() +",我叫:"+ this.getName() +",我性別:"+sex);    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }}

  第四步.建立Test1類,通過show方法把Person的對象作為參數傳進去

package com.maya.test;import com.maya.ball.Person;import com.maya.basketball.American;import com.maya.football.Chinese;public class Test1 {    public void show(Person per){        if(per instanceof American){            American am = (American) per;            System.out.println(am.height);        }        if(per instanceof Chinese){            Chinese ch = (Chinese) per;            System.out.println(ch.getSex());        }    }}

  第五步.建立Main方法進行調用

package com.maya.test;import com.maya.ball.Person;import com.maya.basketball.American;import com.maya.football.Chinese;public class Main1 {    public static void main(String[] args){        Person per = new American(32,"小明",175);        American american = (American) per;        double s = american.height;        System.out.println(s);                Person ame = new American(32,"小明",180);        ame.speak();        Person chinese = new Chinese(23,"小李","女");        chinese.speak();        Test1 t = new Test1();        t.show(ame);        t.show(chinese);            }}

 

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.