Javase—多態習題__Java

來源:互聯網
上載者:User

/** * 【練習題】1.多態練習   1.定義一個Animal父類,方法有eat();2.定義三個子類;Cat,Dog,Eagle;每個子類增加新的方法,並重寫eat();3.利用多態性定義一個Animal類型的變數a,並將不同子類的執行個體賦給a;調用eat();觀察結果後,並理解多態4.思考,如果a的引用指向一個Cat,如何調用Cat的新增方法; */package com.Oracle.oop4;public class Animal {public void eat() {System.out.println("吃吃");}}package com.Oracle.oop4;public class Cat extends Animal{@Overridepublic void eat() {System.out.println("咦,這是誰掉落的小魚乾(= =+)");}public void scorn() {System.out.println("蔑視爾等凡人");}}package com.Oracle.oop4;public class Dog extends Animal {public void eat() {System.out.println("汪汪汪,我什麼都吃噠~");}public void adhere() {System.out.println("不粘人的狗不是好狗");}}package com.Oracle.oop4;public class Eagle extends Animal {public void eat() {System.out.println("作為一隻鷹,吃肉不是理所當然的嗎");}public void fly() {System.out.println("想飛上天,和太陽肩並肩");}}package com.Oracle.oop4;public class Polymorphic1 {public static void main(String[] args) {Animal a=new Cat();a.eat();a=new Dog();a.eat();a=new Eagle();a.eat();if(a instanceof Cat) {Cat c=(Cat)a;c.scorn();}else if(a instanceof Dog) {Dog d=(Dog)a;d.adhere();}else if(a instanceof Eagle) {Eagle e=(Eagle)a;e.fly();}}}

 /** * 【練習題】2.多態練習定義一個Animal類,方法有sing方法,定義這個類的三個子類(Dog,Cat,Bird),分別重寫這個方法。利用多態,定義一個Animal類型的對象,Animal a;分別引用三個子類的對象,調用sing方法。為每個子類,增加額外的方法。通過此例,練習upCast,downCast,及instanceof操作符。 */package com.Oracle.oop4;public class Animal2 {public void sing() {System.out.println("啦啦,唱歌");}}package com.Oracle.oop4;public class Dog2 extends Animal2{@Overridepublic void sing() {System.out.println("我只會說:汪汪,會讓你更旺哦");}public void adhere() {System.out.println("不粘人的狗不是好狗");}}package com.Oracle.oop4;public class Cat2 extends Animal2 {@Overridepublic void sing() {System.out.println("喵喵,陪我玩啊");}public void scorn() {System.out.println("蔑視爾等凡人");}}package com.Oracle.oop4;public class Bird2 extends Animal2{@Overridepublic void sing() {System.out.println("你去打聽,十裡八村數我小鳥唱歌最好聽");}public void fly() {System.out.println("想飛上天,和太陽肩並肩");}}package com.Oracle.oop4;public class Polymorphic2 {public static void main(String[] args) {Animal2 a=new Cat2();a.sing();a=new Dog2();a.sing();a=new Bird2();a.sing();if(a instanceof Cat2) {Cat2 c=(Cat2)a;c.scorn();}else if(a instanceof Dog2) {Dog2 d=(Dog2)a;d.adhere();}else if(a instanceof Bird2) {Bird2 b=(Bird2)a;b.fly();}}}

/** * 【練習題】3.多態練習類比天天酷跑遊戲;定義一個(寵物)Pet類,類中有屬性name,方法是follow(跟隨);再定義三個子類,Cat,Dog,Eagle,分別重寫follow方法;   再定義一個Hero類,這個類中有兩個屬性name和Pet類型的pet,一個方法run(),再定義兩個構造方法,Hero(String name,);   Hero(String name,Pet pet); run()方法的代碼是Hero跑,並且他的寵物也跟著跑;編寫測試類別來操作Hero */package com.Oracle.oop4_2;public class Pet {String name;public void follow() {System.out.println(this.name+"一直跟隨著您呦~");}}package com.Oracle.oop4_2;public class Hero {String name;Pet pet;public Hero(String name) {this.name=name;}public Hero(String name, Pet pet) {this.name = name;this.pet = pet;}public void run() {System.out.println("您的英雄 "+this.name+" 在跑,並且您的寵物 "+this.pet.name+"也跟著您的英雄跑");}}package com.Oracle.oop4_2;public class Cat extends Pet {public void follow() {System.out.println(this.name+"可以萌化兇狠的對手");}}package com.Oracle.oop4_2;public class Dog extends Pet{public void follow() {System.out.println(this.name+"保護您一路前行");}}package com.Oracle.oop4_2;public class Eagle extends Pet{public void follow() {System.out.println(this.name+"飛上了天,並和您肩並肩");}}package com.Oracle.oop4_2;public class Polymorphic3 {public static void main(String[] args) {Pet c=new Cat();c.name="藍胖子";Hero h=new Hero("蓋倫",c);h.run();}}

/** * 定義一個測試類別Test,編寫equalsArea方法測試兩個對象的面積是否相等(注意方法的參數類型,利用動態綁定技術), * 編寫displayGeometricObject方法顯示對象的面積(注意方法的參數類型,利用動態綁定技術)。 */package com.Oracle.oop4_2;public class GeometricObject {static final double PI=3.1415926;String color;double weight;public String getColor() {return color;}public void setColor(String color) {this.color = color;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public GeometricObject(String color, double weight) {this.color = color;this.weight = weight;}public double findArea() {double a=0;return a;}public boolean equalsArea(GeometricObject c,GeometricObject m) {if(c.findArea()==m.findArea()) {return true;}return false;}public void displayGeometricObject(GeometricObject g) {System.out.println("該形狀的面積為:"+g.findArea());}}package com.Oracle.oop4_2;public class MyRectangle extends GeometricObject {double width;double height;public double getWidth() {return width;}public void setWidth(double width) {this.width = width;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public MyRectangle(double width,double height,String color,double weight) {super(color,weight);this.width=width;this.height=height;}@Overridepublic double findArea() {double s=width*height;return s;}}package com.Oracle.oop4_2;public class Circle extends GeometricObject{//static final double PI=3.1415926;double radius;public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;}public Circle(double radius,String color,double weight) {super(color,weight);this.radius=radius;}public double findArea() {double s=GeometricObject.PI*radius*radius;return s;}}package com.Oracle.oop4_2;public class Polymorphic4 {public static void main(String[] args) {GeometricObject g1=new Circle(5.2,"blue",4.3);GeometricObject g2=new MyRectangle(8.7,9.6,"yellow",5.1);System.out.println(g1.equalsArea(g1, g2));g1.displayGeometricObject(g1);g2.displayGeometricObject(g2);}}




聯繫我們

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