天天看點

Java 實驗案例(多态)

實驗任務

  • 任務一:圖形面積周長計算小程式
  • 任務二:飼養員喂養動物程式

實驗内容:

任務一: 圖形面積周長計算

任務目的:

  1. 掌握多态的含義及應用場合
  2. 掌握上轉型對象和多态的實作
  3. 掌握abstract關鍵字的使用

任務描述:

  設計一個小程式,可以計算圓形和長方形的面積及周長,其中

定義抽象類 圖形類為圓形和長方形的父類,在圖形類中定義抽象方

法擷取面積方法和擷取周長方法。定義面積和周長電腦,可以計

算不同圖形的面積和周長。程式要具備良好的可擴充性和可維護性

程式運作結果參考如下圖檔:

Java 實驗案例(多态)

實施步驟:

任務分析:

  定義父類Shape作為抽象類,并在類中定義抽象方法求周長和面積

  定義Shape子類圓形(circle),具有半徑屬性和常量PI,同時必須實作父類中的抽象方法

  定義Shape子類長方形(rectangle),具有長和寬的屬性,同時必須實作父類的抽象方法

  建立圖形面積周長電腦(ShapeCalculate),具有計算不同圖形面積和周長的方法。

  建立測試類TestShape類,在其main()方法中對ShapeCalculate計算面積和周長方法進行測試

代碼實作:

public abstract class Shape {

    // 抽象方法: 擷取面積
    public abstract double getArea();
    
    // 抽象方法:擷取周長
    public abstract double getPerimeter();
}      
public class Circle extends Shape {
    
    private double radius = 0;    // 圓的半徑
    private final static double PI = 3.14;    // 常量,圓周率
    
    // 有參構造,初始化圓半徑
    public Circle(double radius) {   
        this.radius = radius;
    }

    // 求圓面積
    public double getArea() {
        return PI*radius*radius;
    }

    // 求圓周長
    public double getPerimeter() {
        return 2*radius*PI;
    }

}      
public class Rectangle extends Shape {

    private double length = 0;    // 長方形的長
    private double width = 0;     // 長方形的寬
    
    // 有參構造,初始化長方形的長和寬
    public Rectangle(double length, double width) {
        super();
        this.length = length;
        this.width = width;
    }

    public double getArea() {
        return this.length*this.width;
    }

    public double getPerimeter() {
        return 2*(this.length+this.width);
    }

}      
public class ShapeCaculate {

    // 可以計算任意shape子類的面積
    public void calArea (Shape shape) {
        System.out.println(shape.getArea());
    }
    
    // 可以計算任意shape子類的周長
    public void calPerimeter(Shape shape) {
        System.out.println(shape.getPerimeter());
    }
}      
public class TestShape {

    public static void main(String[] args) {
        
        // 建立圖形電腦
        ShapeCaculate sc = new ShapeCaculate();
        
        // 建立長方形和圓形對象
        Shape rectangle = new Rectangle(3, 4);         // <-------多态
        Circle circle = new Circle(3);
        
        // 求長方形和圓形的面積
        System.out.println("求長方形的面積:");
        sc.calArea(rectangle);
        System.out.println("求圓形的面積:");
        sc.calArea(circle);
        
        // 求長方形和圓形的周長
        System.out.println("求長方形的周長:");
        sc.calPerimeter(rectangle);
        System.out.println("求圓形的周長:");
        sc.calPerimeter(circle);
    }
}      

任務二:飼養員喂養動物

任務目的:

  1. 掌握多态的含義及應用場合。
  2. 掌握上轉型對象和多态的實作
  3. 體會多态帶來的好處
  4. 掌握instanceof運算符的使用

實施步驟:

  在Java實驗案例(繼承)任務三中,飼養員每拿一種食物喂養相應的動物都需要

建立相應的方法,程式的可擴充性和可維護性較差,通過多态可以對程式進行優化,

修改feed()方法的參數為父類的類型,使程式具有較好的可擴充性和可維護性。

代碼實作:

動物父類

public class Animal {

    public void eat(Food food) {
        System.out.println("吃飯時間到了!");
        System.out.println(this + "喜歡吃" +food);
    }
}      

Animal子類Dog類

public class Dog extends Animal {

    // 重寫Animal從Object中繼承的toString()方法
    public String toString() {
        return "Dog";
    }
    
    // 新增算算術方法
    public void arithmetic() {
        System.out.println(this+"算算術表演!");
    }
}      

Animal子類Cat類

public class Cat extends Animal {

    // 從寫Animal從Object中繼承的toString方法
    public String toString() {
        return "Cat";
    }
    
    // 新增跳環方法
    public void jumpRing() {
        System.out.println(this+"開始表演跳環!");
    }
}      

食物Food類

public class Food {

    private int weight;  // 食物重量
    public Food(int weight) {
        this.weight = weight;
    }
    
    public int getWeight() {
        return weight;
    }
}      

Bone子類

public class Bone extends Food {

    public Bone(int weight) {    // 調用父類的構造方法
        super(weight);
    }
    
    public String toString() {   
        return "Bone";
    }
}      

Fish子類

public class Fish extends Food {

    public Fish(int weight) {
        super(weight);
    }
    
    public String toString() {
        return "Fish";
    }
}      

飼養員Feeder類

public class Feeder {

    private String name;
    public Feeder(String name) {
        this.name = name;
    }
    
    public void feed(Animal a, Food f) {
        a.eat(f);
        System.out.println("飼養員"+name+"拿着"+f.getWeight()+"克"+f+"喂養"+a+"。");
    }
    
    public void perform(Animal a) {
        if (a instanceof Dog){
            ((Dog) a).arithmetic();
        }
        
        if(a instanceof Cat) {
            Cat c = (Cat) a;
            c.jumpRing();
        }
    }
}      

測試類

public class TestFeed {

    public static void main(String[] args) {
        Feeder fd = new Feeder("張三");
        Dog dog = new Dog();
        Bone bone = new Bone(500);
        fd.feed(dog, bone);
        System.out.println("-----------------");
        fd.feed(new Cat(), new Fish(100));
        
        fd.perform(dog);
        fd.perform(new Cat());
    }
}      

 運作結果如下圖:

Java 實驗案例(多态)

轉載于:https://www.cnblogs.com/yoke/p/7520034.html