Java複習 9繼承與多態

來源:互聯網
上載者:User
class Use{String uname;//聲明姓名int uid;//聲明學號int uage;//聲明年齡int uresult;//聲明成績String uaddr;//聲明家庭住址void print(){//父類中的print()方法System.out.println("姓名:"+uname+"\n學號:"+uid+"\n成績:"+uresult+"\n家庭住址:"+uaddr);}}class Use1 extends Use//Use1繼承自Use{boolean usex;//聲明性別void print1(){//定義一個自己的print1方法if(usex){System.out.println("性別:男");}else{System.err.println("性別:女");}}}public class InheritTest {public static void main(String[] args) {Use u=new Use();//建立Use的對象u,並分別為屬性賦值列印輸出Use1 u1=new Use1();//建立Use1的對象u1,並分別為屬性賦值列印輸出u.uname="張三";u.uage=22;u.uid=1568264;u.uaddr="北京海澱區";u.uresult=99;System.out.println("父類中方法輸出為:");u.print();u1.uname="李四";u1.uaddr="山東濟南";u1.uage=20;u1.uid=123456;u1.uresult=98;u1.usex=false;System.out.println("\n子類方法的輸出為:");u1.print();u1.print1();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.