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 contains an void fly() method. Design a concrete class named Chicken as subclass of Bird and override the sing() method to print "Chicken sings" Design a concrete class named Superman as subclass of Flyable and override the fly() method to print "Superman flies" Design a concrete class named Parrot as subclass of Flyable and Bird and override the methods fly() and sing() to print "Parrot flies" and "Parrot sings" separately. 裁判测试程序样例: import java.util.Scanner; class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); int i = in.nextInt(); Flyable[] f = { new Parrot(), new Superman() }; for (Flyable k : f) { k.fly(); } System.out.println(i); Bird[] a = { new Parrot(), new Chicken() }; for ( Bird b : a ) { b.sing(); } in.close(); } } /* 请在这里填写答案 */ 输入样例: 123 输出样例: Parrot flies Superman flies 123 Parrot sings Chicken sings
时间: 2024-01-17 11:06:00 浏览: 34
代码如下:
```java
interface Flyable {
void fly();
}
abstract class Bird {
void sing() {
System.out.println("This bird sings");
}
}
class Chicken extends Bird {
@Override
void sing() {
System.out.println("Chicken sings");
}
}
class Superman implements Flyable {
@Override
public void fly() {
System.out.println("Superman flies");
}
}
class Parrot extends Bird implements Flyable {
@Override
public void fly() {
System.out.println("Parrot flies");
}
@Override
void sing() {
System.out.println("Parrot sings");
}
}
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
Flyable[] f = { new Parrot(), new Superman() };
for (Flyable k : f) {
k.fly();
}
System.out.println(i);
Bird[] a = { new Parrot(), new Chicken() };
for (Bird b : a) {
b.sing();
}
in.close();
}
}
```
运行结果:
```
Parrot flies
Superman flies
123
Parrot sings
Chicken sings
```