java新手筆記14 類繼承樣本

來源:互聯網
上載者:User

標籤:

1.Person

package com.yfs.javase;public class Person {private String name;private int age;private char sex;private void privateMehtod() {System.out.println("call privateMehtod()...");}public Person() {//System.out.println("建立Perosn對象...");}public Person(String name) {this.name = name;}public Person(String name, int age, char sex) {this.name = name;this.age = age;this.sex = sex;}public void introduce() {System.out.println("I am Person....");}public String toString() {return "姓名:" + name + "  年齡 :" + age + "  性別:" + sex;}public void speak() {System.out.println(name + " 工作了嗎?");}public void sleep() {System.out.println(name + " 睡覺了嗎?");}public void eat() {System.out.println(name + " 吃了嗎?");}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}}

 2.Student

package com.yfs.javase;//實現代碼重用  單繼承public class Student extends Person {// 繼承Personprivate int score;@Overridepublic void introduce() {System.out.println("I am student...");}// 覆蓋 重寫父類的方法@Overridepublic String toString() {return super.toString() + "  成績:" + score;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}public Student() {System.out.println("建立Student對象...");}// 擴充方法public void study() {System.out.println(getName() + " 在學習...");}}

 3.Teacher

package com.yfs.javase;public class Teacher extends Person {private String job;public String getJob() {return job;}public void setJob(String job) {this.job = job;}@Overridepublic void introduce() {System.out.println("I am teacher...");}}

 4.test1

package com.yfs.javase;public class Demo1 {public static void main(String[] args) {Person p1 = new Person();p1.setName("張三");Student s1 = new Student();s1.setName("李四");Teacher t1 = new Teacher();t1.setName("王五");p1.speak();s1.speak();s1.study();t1.introduce();//s1 = t1;//類型不符  都是person子類//p1.study();  子類的方法屬性不能訪問//裡氏替換Person p2 = s1;//父類的引用指向子類對象p2 = new Student();p2.setName("Tom");p2.sleep();//對象操作由宣告類型決定//p2.study();//電腦技術人員//具體執行由對象決定  p2 中具體是什麼對象p2 = new Teacher();//多態  方法覆蓋p2.introduce();//父類聲明的方法 子類覆蓋父類的方法}}

 5.test2

package com.yfs.javase;public class Demo2 {public static void main(String[] args) {Person p1 = new Person();p1.setName("張三");Student s1 = new Student();s1.setName("李四");Teacher t1 = new Teacher();t1.setName("王五");Person p2 = new Student();p2.setName("Tom");//變回去 強制轉換Student s2 = (Student)p2;s2.study();//Teacher t2 = (Teacher)p2;//查看對象的類型 getClass()System.out.println(p2.getClass().getName());System.out.println(t1.getClass().getName());//s1 = p1;p1 = s1;System.out.println(p1);System.out.println(s1);s1.setAge(20);p1.setSex(‘男‘);System.out.println(p1);System.out.println(s1);//p1.study();}}

 

java新手筆記14 類繼承樣本

聯繫我們

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