/定义接口 【】 IPet{ void beFriendly(); void beAccompanied(); } //定义抽象类 【】 class Animal{ private String name; //定义抽象方法 public 【】 void move(); public Animal(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } //继承animal 实现IPet class Cat 【】{ public Cat(String name){ 【】 } //实现beFriendly方法 //实现be beAccompanied方法 @Override public void move() { // TODO Auto-generated method stub System.out.println("cat move quiently"); } } //继承animal 实现IPet class Dog 【】{ public Dog(String name){ 【】 } //实现beFriendly方法 //实现be beAccompanied法 @Override public void move() { // TODO Auto-generated method stub System.out.println("dog move quickly"); } }
时间: 2024-01-27 22:02:50 浏览: 110
答案如下:
定义接口:
```java
interface IPet {
void beFriendly();
void beAccompanied();
}
```
定义抽象类:
```java
abstract class Animal {
private String name;
public Animal(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void move();
}
```
继承Animal类实现IPet接口的Cat类:
```java
class Cat extends Animal implements IPet {
public Cat(String name) {
super(name);
}
@Override
public void beFriendly() {
System.out.println("Cat is friendly");
}
@Override
public void beAccompanied() {
System.out.println("Cat likes to be accompanied");
}
@Override
public void move() {
System.out.println("Cat moves quietly");
}
}
```
继承Animal类实现IPet接口的Dog类:
```java
class Dog extends Animal implements IPet {
public Dog(String name) {
super(name);
}
@Override
public void beFriendly() {
System.out.println("Dog is friendly");
}
@Override
public void beAccompanied() {
System.out.println("Dog likes to be accompanied");
}
@Override
public void move() {
System.out.println("Dog moves quickly");
}
}
```
阅读全文