6 changed files with 123 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||||
|
public abstract class Animal { |
||||
|
protected String name; |
||||
|
|
||||
|
public Animal(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public abstract void makeSound(); |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
public class Cat extends Animal { |
||||
|
|
||||
|
public Cat(String name) { |
||||
|
super(name); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void makeSound() { |
||||
|
System.out.println(name + ":喵喵喵!"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
public class Dog extends Animal implements Swimmable { |
||||
|
|
||||
|
public Dog(String name) { |
||||
|
super(name); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void makeSound() { |
||||
|
System.out.println(name + ":汪汪汪!"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void swim() { |
||||
|
System.out.println(name + "正在游泳!"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
Animal dog = new Dog("旺财"); |
||||
|
Animal cat = new Cat("咪咪"); |
||||
|
|
||||
|
System.out.println("=== 动物叫声测试 ==="); |
||||
|
dog.makeSound(); |
||||
|
cat.makeSound(); |
||||
|
|
||||
|
System.out.println("\n=== 多态调用测试 ==="); |
||||
|
testAnimal(dog); |
||||
|
testAnimal(cat); |
||||
|
|
||||
|
System.out.println("\n=== 游泳能力测试 ==="); |
||||
|
if (dog instanceof Swimmable) { |
||||
|
((Swimmable) dog).swim(); |
||||
|
} |
||||
|
|
||||
|
if (cat instanceof Swimmable) { |
||||
|
((Swimmable) cat).swim(); |
||||
|
} else { |
||||
|
System.out.println(cat.getName() + "不会游泳"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void testAnimal(Animal animal) { |
||||
|
System.out.print(animal.getName() + "的叫声:"); |
||||
|
animal.makeSound(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
# AI交互记录 - 动物叫声系统 |
||||
|
|
||||
|
## 需求分析 |
||||
|
1. 创建抽象类 Animal,包含抽象方法 makeSound() |
||||
|
2. 创建 Dog 和 Cat 继承 Animal |
||||
|
3. 创建接口 Swimmable,包含方法 swim() |
||||
|
4. 让 Dog 实现 Swimmable,Cat 不实现 |
||||
|
5. 在 main 方法中测试多态调用 |
||||
|
|
||||
|
## 实现步骤 |
||||
|
|
||||
|
### 步骤1:创建抽象类 Animal.java |
||||
|
- 定义抽象类 Animal |
||||
|
- 包含受保护属性 name |
||||
|
- 定义抽象方法 makeSound() |
||||
|
- 提供构造方法和 getter |
||||
|
|
||||
|
### 步骤2:创建接口 Swimmable.java |
||||
|
- 定义接口 Swimmable |
||||
|
- 包含方法 swim() |
||||
|
|
||||
|
### 步骤3:创建 Dog.java |
||||
|
- 继承 Animal 类 |
||||
|
- 实现 Swimmable 接口 |
||||
|
- 实现 makeSound() 输出"汪汪汪" |
||||
|
- 实现 swim() 方法 |
||||
|
|
||||
|
### 步骤4:创建 Cat.java |
||||
|
- 继承 Animal 类 |
||||
|
- 不实现 Swimmable 接口 |
||||
|
- 实现 makeSound() 输出"喵喵喵" |
||||
|
|
||||
|
### 步骤5:创建 Main.java |
||||
|
- 创建 Dog 和 Cat 对象 |
||||
|
- 测试多态调用 |
||||
|
- 使用 instanceof 判断游泳能力 |
||||
|
- 演示接口方法的调用 |
||||
|
|
||||
|
## 核心知识点 |
||||
|
- **抽象类**:Animal 作为基类,强制子类实现 makeSound() |
||||
|
- **接口**:Swimmable 定义可选行为,Dog 选择性实现 |
||||
|
- **多态**:通过父类引用调用子类方法 |
||||
|
- **类型判断**:使用 instanceof 检查接口实现 |
||||
|
|
||||
|
## 文件清单 |
||||
|
- Animal.java - 抽象基类 |
||||
|
- Swimmable.java - 游泳接口 |
||||
|
- Dog.java - 狗类(实现游泳) |
||||
|
- Cat.java - 猫类(不游泳) |
||||
|
- Main.java - 测试类 |
||||
@ -0,0 +1,3 @@ |
|||||
|
public interface Swimmable { |
||||
|
void swim(); |
||||
|
} |
||||
Loading…
Reference in new issue