Compare commits
2 Commits
a03abd339c
...
8755671981
| Author | SHA1 | Date |
|---|---|---|
|
|
8755671981 | 1 month ago |
|
|
9bcb7b216a | 1 month ago |
8 changed files with 125 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||||
|
.idea/ |
||||
|
|
||||
|
### IntelliJ IDEA ### |
||||
|
out/ |
||||
|
!**/src/main/**/out/ |
||||
|
!**/src/test/**/out/ |
||||
|
.kotlin |
||||
|
|
||||
|
### Eclipse ### |
||||
|
.apt_generated |
||||
|
.classpath |
||||
|
.factorypath |
||||
|
.project |
||||
|
.settings |
||||
|
.springBeans |
||||
|
.sts4-cache |
||||
|
bin/ |
||||
|
!**/src/main/**/bin/ |
||||
|
!**/src/test/**/bin/ |
||||
|
|
||||
|
### NetBeans ### |
||||
|
/nbproject/private/ |
||||
|
/nbbuild/ |
||||
|
/dist/ |
||||
|
/nbdist/ |
||||
|
/.nb-gradle/ |
||||
|
|
||||
|
### VS Code ### |
||||
|
.vscode/ |
||||
|
|
||||
|
### Mac OS ### |
||||
|
.DS_Store |
||||
@ -0,0 +1,10 @@ |
|||||
|
# 默认忽略的文件 |
||||
|
/shelf/ |
||||
|
/workspace.xml |
||||
|
# 基于编辑器的 HTTP 客户端请求 |
||||
|
/httpRequests/ |
||||
|
# 已忽略包含查询文件的默认文件夹 |
||||
|
/queries/ |
||||
|
# Datasource local storage ignored files |
||||
|
/dataSources/ |
||||
|
/dataSources.local.xml |
||||
@ -0,0 +1,6 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK"> |
||||
|
<output url="file://$PROJECT_DIR$/out" /> |
||||
|
</component> |
||||
|
</project> |
||||
@ -0,0 +1,8 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ProjectModuleManager"> |
||||
|
<modules> |
||||
|
<module fileurl="file://$PROJECT_DIR$/java-homework.iml" filepath="$PROJECT_DIR$/java-homework.iml" /> |
||||
|
</modules> |
||||
|
</component> |
||||
|
</project> |
||||
@ -0,0 +1,7 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="VcsDirectoryMappings"> |
||||
|
<mapping directory="" vcs="Git" /> |
||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" /> |
||||
|
</component> |
||||
|
</project> |
||||
@ -0,0 +1,11 @@ |
|||||
|
<?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$/src" isTestSource="false" /> |
||||
|
</content> |
||||
|
<orderEntry type="inheritedJdk" /> |
||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||
|
</component> |
||||
|
</module> |
||||
@ -0,0 +1,51 @@ |
|||||
|
package w7; |
||||
|
// Animal.java - 动物抽象类
|
||||
|
abstract class Animal { |
||||
|
public abstract void makeSound(); // 抽象方法:发出声音
|
||||
|
} |
||||
|
|
||||
|
// Swimable.java - 游泳接口
|
||||
|
interface Swimable { |
||||
|
void swim(); // 游泳方法
|
||||
|
} |
||||
|
|
||||
|
// Dog.java - 狗类继承动物并实现游泳接口
|
||||
|
class Dog extends Animal implements Swimable { |
||||
|
@Override |
||||
|
public void makeSound() { |
||||
|
System.out.println("狗叫:汪汪!"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void swim() { |
||||
|
System.out.println("狗在游泳。"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Cat.java - 猫类只继承动物,不实现游泳接口
|
||||
|
class Cat extends Animal { |
||||
|
@Override |
||||
|
public void makeSound() { |
||||
|
System.out.println("猫叫:喵喵!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Main.java - 测试多态调用
|
||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
// 多态:父类引用指向子类对象
|
||||
|
Animal myDog = new Dog(); |
||||
|
Animal myCat = new Cat(); |
||||
|
|
||||
|
// 调用 makeSound() - 展示不同动物的不同行为
|
||||
|
myDog.makeSound(); // 狗叫
|
||||
|
myCat.makeSound(); // 猫叫
|
||||
|
|
||||
|
// 测试游泳:只有狗能游泳(使用 instanceof 检查类型)
|
||||
|
if (myDog instanceof Swimable) { |
||||
|
((Swimable) myDog).swim(); // 狗游泳
|
||||
|
} |
||||
|
|
||||
|
// 猫不能游泳(Cat 没有实现 Swimable 接口)
|
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue