Design an abstract class named Bird to represent a bird. The class contains an void sing() method to print This bird sings. Design an interface named Flyable to represent anything can fly and contai
时间: 2024-02-13 20:00:10 浏览: 107
abstract class
ns a void fly() method. The Bird class should implement the Flyable interface and override the fly() method to print This bird flies. Additionally, the Bird class should have a protected string field named color to represent the color of the bird. The class should have a constructor that takes a string parameter to set the color of the bird. Finally, the Bird class should have a public getColor() method that returns the color of the bird. Here's an example implementation in Java:
```
public interface Flyable {
void fly();
}
public abstract class Bird implements Flyable {
protected String color;
public Bird(String color) {
this.color = color;
}
public String getColor() {
return color;
}
@Override
public void fly() {
System.out.println("This bird flies.");
}
public abstract void sing();
}
public class Sparrow extends Bird {
public Sparrow(String color) {
super(color);
}
@Override
public void sing() {
System.out.println("This bird sings.");
}
}
```
阅读全文