1. 程式人生 > >java學習之路 之 高階類特性-抽象類-練習題

java學習之路 之 高階類特性-抽象類-練習題

/**
    具體類 : 對現實世界事物的抽象定義
    抽象類 : 對某一型別的不同種事物的抽象定義
*/
//編寫抽象類Pet, 屬性:名字,年齡,體重;  抽象方法speak,eat
public abstract class Pet {

    // 可以包含屬性, 構造器,普通方法, 抽象方法
    private String name;
    private int age;
    private double weight;

    public Pet() {}

    public Pet(String name,int age,double weight) {
        this
.name = name; this.age = age; this.weight = weight; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return
age; } public void setWeight(double weight) { this.weight = weight; } public double getWeight() { return weight; } // 抽象方法 : 只有方法簽名沒有方法體, 被abstract修飾 public abstract void speak();//抽象方法 public abstract void eat();//抽象方法 @Override public String toString
() { return "姓名:" + name + ",年齡:" + age + ",體重:" + weight + "Kg"; } } //編寫Bird類繼承Pet,特有屬性:flySpeed public class Bird extends Pet { private int flySpeed; public Bird() {} public Bird(String name,int age,double weight,int flySpeed) { super(name,age,weight); this.flySpeed = flySpeed; } public void setFlySpeed(int flySpeed) { this.flySpeed = flySpeed; } public int getFlySpeed() { return flySpeed; } // 方法實現!! 去掉abstract 加上方法體 // 具體子類重寫並加上方法體抽象父類的抽象方法 @Override public void speak() { System.out.println("嘰嘰喳喳的叫個不停!!!!"); } @Override public void eat() { System.out.println("最喜歡吃蟲子!!!!正在吃蟲子中......"); } @Override public String toString() {//父類中toString方法重寫 return super.toString() + ",飛行速度:" + flySpeed + "Km/小時!!!"; } } //編寫Cat類繼承Pet,特有屬性:顏色 public class Cat extends Pet { private String color; public Cat() {} public Cat(String name,int age,double weight,String color) { super(name,age,weight); this.color = color; } public void setColor(String color) { this.color = color; } public String getColor() { return color; } @Override public void speak() {// System.out.println("喵喵喵的叫個不停!!!!"); } @Override public void eat() {// System.out.println("最喜歡魚!!!!正在吃魚中......"); } @Override public String toString() {//父類中toString方法重寫 return super.toString() + ",顏色:" + color; } } //編寫Dog類繼承Pet,特有屬性:品種 public class Dog extends Pet { private String variety; public Dog() {} public Dog(String name,int age,double weight,String variety) { super(name,age,weight); this.variety = variety; } public void setVariety(String variety) { this.variety = variety; } public String getVariety() { return variety; } @Override public void speak() { System.out.println("汪汪汪的叫個不停!!!!"); } @Override public void eat() { System.out.println("最喜歡骨頭!!!!正在吃骨頭中......"); } @Override public String toString() {//父類中toString方法重寫 return super.toString() + ",品種:" + variety; } } /* 建立6個元素的陣列,分別存放2個Cat、2個Dog和2個Bird物件 將陣列元素按重量倒序排序,然後列印各物件的字串表示(toString) */ class PetTest { public static void main(String[] args) { //建立6個元素的陣列,分別存放2個Cat、2個Dog和2個Bird物件 Pet[] ps = new Pet[6]; //public Cat(String name,int age,double weight,String color) ps[0] = new Cat("小咪",2,2.5,"黑色"); ps[1] = new Cat("二咪",3,3.5,"白色"); //public Dog(String name,int age,double weight,String variety) ps[2] = new Dog("大汪",3,5.5,"泰迪"); ps[3] = new Dog("二汪",2,4.5,"哈巴"); //public Bird(String name,int age,double weight,int flySpeed) ps[4] = new Bird("小叼",2,1.5,30); ps[5] = new Bird("大叼",3,3.0,40); //遍歷輸出陣列 for (Pet p : ps) { System.out.println(p); } System.out.println("-----------------------排序後------------------"); //冒泡法按體重倒序重新排序 for (int i = 0;i < ps.length - 1;i++) { for (int j = 0;j < ps.length - 1 - i;j++){ if (ps[j].getWeight() < ps[j + 1].getWeight()) { Pet tmp = ps[j]; ps[j] = ps[j + 1]; ps[j + 1] = tmp; } } } //遍歷輸出陣列 for (Pet p : ps) { System.out.println(p); //p.speak(); //p.eat(); } } } public abstract class Frock { private int size; private String color; private double price; public Frock() {} public Frock(int size, String color, double price{ this.size = size; this.color = color; this.price = price; } public void setSize(int size) { this.size = size; } public int getSize() { return size; } public void setColor(String color) { this.color = color; } public String getColor() { return color; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } @Override public String toString() { return "尺寸:" + size + ",顏色:" + color + ",價格:" + price; } public abstract double calcArea(); } class Shirt extends Frock { public Shirt() {} public Shirt(int size, String color, double price{ super(size, color, price); } @Override public double calcArea() { return getSize() * 1.3; } } class FrockTest { public static void main(String[] args) { Frock f = new Shirt(180, "白色", 200); System.out.println(f); System.out.println(f.calcArea()); Frock f2 = new Shirt(); System.out.println(f2); System.out.println(f2.calcArea()); } }