程序补充完整 interfaceRun{ voidrun(); } interfaceSwim{ (2分)//游泳抽象方法 } publicclassSportimplementsRun,Swim{ publicvoidrun(){ (2分)//参考运行结果补充输出流语句 }publicvoidswim(){ System.out.println("每周我都会去游泳馆游泳。"); } publicstaticvoidmain(String[]args){ Sporta=newSport(); a.run(); a.swim(); } }
时间: 2024-04-02 17:33:42 浏览: 98
程序补充完整:
interface Run {
void run();
}
interface Swim {
void swim();
}
public class Sport implements Run, Swim {
public void run() {
System.out.println("我每天都会去晨跑。");
}
public void swim() {
System.out.println("每周我都会去游泳馆游泳。");
}
public static void main(String[] args) {
Sport a = new Sport();
a.run();
a.swim();
}
}
相关问题
需要编写两个接口体现特殊能力: (1)定义一个游泳的接口Swimmable,在此接口中有抽象方法swim()。 (2)定义一个游泳的接口Flyable,在此接口中有抽象方法fly()。 需要编写三个子类兔子、青蛙和老鹰: (1) 定义一个子类Rabbit,兔子不会飞也不会游泳,继承动物类,并覆写shout()和eat()方法。 (2) 定义一个子类Frog,青蛙不会飞但会游泳,继承动物类并实现接口Flyable,并覆写shout()、eat()方法和swim()方法。 (3) 定义一个子类Eagle,老鹰会飞但不会游泳,继承动物类并实现接口Swimmable,并覆写shout()、eat()方法和fly()方法。
接口Swimmable的代码:
```java
public interface Swimmable {
void swim();
}
```
接口Flyable的代码:
```java
public interface Flyable {
void fly();
}
```
动物类Animal的代码:
```java
public abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public abstract void shout();
public abstract void eat();
}
```
子类Rabbit的代码:
```java
public class Rabbit extends Animal {
public Rabbit(String name) {
super(name);
}
@Override
public void shout() {
System.out.println(name + " is shouting: squeak squeak");
}
@Override
public void eat() {
System.out.println(name + " is eating: carrot");
}
}
```
子类Frog的代码:
```java
public class Frog extends Animal implements Swimmable {
public Frog(String name) {
super(name);
}
@Override
public void shout() {
System.out.println(name + " is shouting: croak croak");
}
@Override
public void eat() {
System.out.println(name + " is eating: insect");
}
@Override
public void swim() {
System.out.println(name + " is swimming");
}
}
```
子类Eagle的代码:
```java
public class Eagle extends Animal implements Flyable {
public Eagle(String name) {
super(name);
}
@Override
public void shout() {
System.out.println(name + " is shouting: screech screech");
}
@Override
public void eat() {
System.out.println(name + " is eating: mouse");
}
@Override
public void fly() {
System.out.println(name + " is flying");
}
}
```
编写程序,定义一个名为Swimmable(可游泳的)的接口,其中包含void swim()方法,定义另一个名为Flyable(可飞的)的接口,其中包含void fly()方法。 定义一个名为Animal的抽象类,其中包含一个抽象方法void eat()。定义一个名为WildDuck(野鸭)的类实现上述两个接口,并继承Animal类。 编写main()方法,在其中创建一个WildDuck实例,调用它的各种方法。将WildDuck实例分别赋值给一个Swimmalbe引用、Flyable引用和Animal引用,测试通过这些引用能否调用相应接口或抽象类中定义的方法
好的,以下是实现代码:
```java
// Swimmable接口
interface Swimmable {
void swim();
}
// Flyable接口
interface Flyable {
void fly();
}
// Animal抽象类
abstract class Animal {
abstract void eat();
}
// WildDuck类实现Swimmable、Flyable接口并继承Animal类
class WildDuck extends Animal implements Swimmable, Flyable {
@Override
void eat() {
System.out.println("Wild duck is eating.");
}
@Override
public void swim() {
System.out.println("Wild duck is swimming.");
}
@Override
public void fly() {
System.out.println("Wild duck is flying.");
}
}
// 测试程序
public class Test {
public static void main(String[] args) {
WildDuck wildDuck = new WildDuck();
// 将WildDuck实例分别赋值给Swimmable引用、Flyable引用和Animal引用
Swimmable swimmable = wildDuck;
Flyable flyable = wildDuck;
Animal animal = wildDuck;
// 测试调用相应接口或抽象类中定义的方法
swimmable.swim();
flyable.fly();
animal.eat();
}
}
```
程序输出:
```
Wild duck is swimming.
Wild duck is flying.
Wild duck is eating.
```
可以看到,通过Swimmable引用、Flyable引用和Animal引用都能成功调用WildDuck实例中实现的方法。
阅读全文