Browse Source

第7周作业:动物声音系统

main
pizza0105 2 months ago
parent
commit
b98bd6c61a
  1. 10
      .idea/.gitignore
  2. 10
      .idea/libraries/lib.xml
  3. 6
      .idea/misc.xml
  4. 8
      .idea/modules.xml
  5. 6
      .idea/vcs.xml
  6. 0
      AnimalTest.java
  7. 16
      Desktop.iml
  8. BIN
      out/production/Desktop/0d3a3902-d091-4b75-adf0-2bf4c4d77b21.png
  9. 1
      out/production/Desktop/AI协助记录.txt
  10. BIN
      out/production/Desktop/QQ20260329-232941.png
  11. 8
      out/production/Desktop/README.md
  12. BIN
      out/production/Desktop/程序运行截图.png
  13. BIN
      w7/Animal.class
  14. BIN
      w7/AnimalTest.class
  15. 79
      w7/AnimalTest.java
  16. BIN
      w7/Cat.class
  17. BIN
      w7/Dog.class
  18. BIN
      w7/Swimmable.class

10
.idea/.gitignore

@ -0,0 +1,10 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 已忽略包含查询文件的默认文件夹
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/

10
.idea/libraries/lib.xml

@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="lib">
<CLASSES>
<root url="jar://$PROJECT_DIR$/jdk-21.0.10/lib/jrt-fs.jar!/" />
<root url="jar://$PROJECT_DIR$/jdk-21.0.10/lib/src.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

6
.idea/misc.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="temurin-24" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Desktop.iml" filepath="$PROJECT_DIR$/Desktop.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

0
AnimalTest.java

16
Desktop.iml

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/W1-何欣蓉-202506050223" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/w1" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/w2" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/w4" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/w5" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/w7" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

BIN
out/production/Desktop/0d3a3902-d091-4b75-adf0-2bf4c4d77b21.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

1
out/production/Desktop/AI协助记录.txt

@ -0,0 +1 @@
AI为我协助完成了Python到Java的代码转换,解释了Java中Scanner类的使用方法。当编译出现"找不到符号"错误时,AI指导添加了import语句解决了问题。AI还提供了命令行参数解析和批量文件处理的实现方案,并帮助编写了README文档和运行说明。

BIN
out/production/Desktop/QQ20260329-232941.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

8
out/production/Desktop/README.md

@ -0,0 +1,8 @@
# 温度转换器 (TemperatureConverter)
这是一个Java版本的温度转换程序,支持摄氏度(°C)与华氏度(°F)之间的相互转换。
## 编译方法
```bash
javac TemperatureConverter.java

BIN
out/production/Desktop/程序运行截图.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
w7/Animal.class

Binary file not shown.

BIN
w7/AnimalTest.class

Binary file not shown.

79
w7/AnimalTest.java

@ -0,0 +1,79 @@
//定义抽象类 Animal
abstract class Animal {
// 抽象方法:没有方法体,子类必须实现
public abstract void makeSound();
}
//定义接口 Swimmable
interface Swimmable {
// 接口中的方法默认是 public abstract
void swim();
}
// Dog 类继承 Animal,并实现 Swimmable
class Dog extends Animal implements Swimmable {
// 实现抽象方法 makeSound()
@Override
public void makeSound() {
System.out.println("汪汪汪!");
}
// 实现接口中的 swim() 方法
@Override
public void swim() {
System.out.println("狗会游泳,正在狗刨...");
}
}
// Cat 类只继承 Animal,不实现 Swimmable
class Cat extends Animal {
// 实现抽象方法 makeSound()
@Override
public void makeSound() {
System.out.println("喵喵喵~");
}
}
// 主类(测试类)
public class AnimalTest {
public static void main(String[] args) {
System.out.println("=== 动物叫声系统 ===\n");
// 多态:父类引用指向子类对象
Animal animal1 = new Dog();
Animal animal2 = new Cat();
// 调用 makeSound() - 表现出不同的行为
System.out.println("狗的声音:");
animal1.makeSound(); // 输出:汪汪汪!
System.out.println("\n猫的声音:");
animal2.makeSound(); // 输出:喵喵喵~
// 测试游泳能力(需要向下转型)
System.out.println("\n=== 游泳测试 ===");
// Dog 实现了 Swimmable,可以游泳
if (animal1 instanceof Swimmable) {
Swimmable swimmer = (Swimmable) animal1; // 向下转型
swimmer.swim(); // 输出:狗会游泳,正在狗刨...
}
// Cat 没有实现 Swimmable,不能游泳
if (animal2 instanceof Swimmable) {
System.out.println("猫会游泳"); // 不会执行
} else {
System.out.println("猫不会游泳"); // 输出:猫不会游泳
}
// 演示直接使用子类引用
System.out.println("\n=== 直接调用 ===");
Dog dog = new Dog();
dog.makeSound(); // 汪汪汪!
dog.swim(); // 狗会游泳,正在狗刨...
Cat cat = new Cat();
cat.makeSound(); // 喵喵喵~
// cat.swim(); // 编译错误!Cat没有swim方法
}
}

BIN
w7/Cat.class

Binary file not shown.

BIN
w7/Dog.class

Binary file not shown.

BIN
w7/Swimmable.class

Binary file not shown.
Loading…
Cancel
Save