Compare commits

...

2 Commits

Author SHA1 Message Date
SALAH ABDULLAH阿山 8755671981 w7 1 month ago
SALAH ABDULLAH阿山 9bcb7b216a w6 1 month ago
  1. 32
      .gitignore
  2. 10
      .idea/.gitignore
  3. 6
      .idea/misc.xml
  4. 8
      .idea/modules.xml
  5. 7
      .idea/vcs.xml
  6. 11
      java-homework.iml
  7. 0
      src/w6/Main.java
  8. 51
      src/w7/Main.java

32
.gitignore

@ -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

10
.idea/.gitignore

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

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_21" default="true" project-jdk-name="21" 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$/java-homework.iml" filepath="$PROJECT_DIR$/java-homework.iml" />
</modules>
</component>
</project>

7
.idea/vcs.xml

@ -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>

11
java-homework.iml

@ -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
src/w6/Main.java

51
src/w7/Main.java

@ -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…
Cancel
Save