Files
oop/lab/lab7/Question8/Bird.java
2022-04-03 15:15:13 +08:00

77 lines
1.6 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/04/03
* Description: This is the abstract class of Bird.
* The bird class implements the Flyer interface.
*/
public abstract class Bird extends Animal implements Flyer {
/**
* An integer indicate how many eggs the bird has.
*/
private int numOfEggs;
/**
* A constructor of Bird.
*
* @param name The name of the bird.
* @param numOfEggs The number of eggs the bird has.
*/
public Bird(String name, int numOfEggs) {
super(name);
this.numOfEggs = numOfEggs;
}
/**
* Getter of numOfEggs.
*
* @return The number of eggs the bird has.
*/
public int getNumOfEggs() {
return numOfEggs;
}
/**
* Bird have 2 legs.
*
* @return int
*/
@Override
public int getLegs() {
return 2;
}
/**
* Abstract method of canFly. Some birds can fly (for example magpies) and some
* birds cannot fly (for example ostriches).
*/
public abstract boolean canFly();
/**
* Test.
*/
public static void testBird() {
Bird b1 = new Bird("magpie", 1) {
@Override
public boolean canFly() {
return true;
}
};
System.out.println(b1.getName() == "magpie");
System.out.println(b1.getNumOfEggs() == 1);
System.out.println(b1.canFly() == true);
System.out.println(b1.getLegs() == 2);
Bird b2 = new Bird("ostriches", 2) {
@Override
public boolean canFly() {
return false;
}
};
System.out.println(b2.getName() == "ostriches");
System.out.println(b2.getNumOfEggs() == 2);
System.out.println(b2.canFly() == false);
System.out.println(b2.getLegs() == 2);
}
}