30 changed files with 258 additions and 0 deletions
@ -0,0 +1,4 @@ |
|||
package PACKAGE_NAME; |
|||
|
|||
public class Computer { |
|||
} |
|||
@ -0,0 +1,4 @@ |
|||
package PACKAGE_NAME; |
|||
|
|||
public class Keyboard { |
|||
} |
|||
@ -0,0 +1,4 @@ |
|||
package PACKAGE_NAME; |
|||
|
|||
public class Main { |
|||
} |
|||
@ -0,0 +1,4 @@ |
|||
package PACKAGE_NAME; |
|||
|
|||
public class Mouse { |
|||
} |
|||
@ -0,0 +1,4 @@ |
|||
package PACKAGE_NAME; |
|||
|
|||
public interface USB { |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,30 @@ |
|||
### 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 |
|||
# 已忽略包含查询文件的默认文件夹 |
|||
/queries/ |
|||
# Datasource local storage ignored files |
|||
/dataSources/ |
|||
/dataSources.local.xml |
|||
# 基于编辑器的 HTTP 客户端请求 |
|||
/httpRequests/ |
|||
@ -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$/USB_homework.iml" filepath="$PROJECT_DIR$/USB_homework.iml" /> |
|||
</modules> |
|||
</component> |
|||
</project> |
|||
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="VcsDirectoryMappings"> |
|||
<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> |
|||
|
After Width: | Height: | Size: 190 KiB |
@ -0,0 +1,16 @@ |
|||
public class Computer { |
|||
// 核心方法:插入 USB 设备
|
|||
public void useUSB(USB usb) { |
|||
// 1. 打开设备
|
|||
usb.open(); |
|||
|
|||
// 2. 模拟使用过程
|
|||
System.out.println("电脑正在使用这个 USB 设备..."); |
|||
|
|||
// 3. 关闭设备
|
|||
usb.close(); |
|||
|
|||
// 换行隔开,看着更清楚
|
|||
System.out.println("---------------------"); |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
public class Keyboard implements USB { |
|||
@Override |
|||
public void open() { |
|||
System.out.println("键盘已连接,准备输入"); |
|||
} |
|||
|
|||
@Override |
|||
public void close() { |
|||
System.out.println("键盘已断开连接,停止输入"); |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
// 1. 建造一台电脑
|
|||
Computer computer = new Computer(); |
|||
|
|||
// 2. 拿出一个鼠标(USB设备)
|
|||
USB mouse = new Mouse(); |
|||
|
|||
// 3. 把鼠标插进电脑
|
|||
computer.useUSB(mouse); |
|||
|
|||
// 4. 拿出一个键盘(USB设备)
|
|||
USB keyboard = new Keyboard(); |
|||
|
|||
// 5. 把键盘插进电脑
|
|||
computer.useUSB(keyboard); |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
public class Mouse implements USB { |
|||
@Override |
|||
public void open() { |
|||
System.out.println("鼠标已连接,准备工作"); |
|||
} |
|||
|
|||
@Override |
|||
public void close() { |
|||
System.out.println("鼠标已断开连接,收起工作"); |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
public interface USB { |
|||
// 打开设备
|
|||
void open(); |
|||
|
|||
// 关闭设备
|
|||
void close(); |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
### 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 |
|||
# 已忽略包含查询文件的默认文件夹 |
|||
/queries/ |
|||
# Datasource local storage ignored files |
|||
/dataSources/ |
|||
/dataSources.local.xml |
|||
# 基于编辑器的 HTTP 客户端请求 |
|||
/httpRequests/ |
|||
@ -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$/shape_homework.iml" filepath="$PROJECT_DIR$/shape_homework.iml" /> |
|||
</modules> |
|||
</component> |
|||
</project> |
|||
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="VcsDirectoryMappings"> |
|||
<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,6 @@ |
|||
public class Circle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制圆形"); |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
ShapeDrawer drawer = new ShapeDrawer(); |
|||
|
|||
Shape s1 = new Circle(); |
|||
Shape s2 = new Rectangle(); |
|||
|
|||
// 调用正确的方法名
|
|||
drawer.drawShape(s1); |
|||
drawer.drawShape(s2); |
|||
} |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
public class Rectangle extends Shape { |
|||
@Override |
|||
public void draw() { |
|||
System.out.println("绘制矩形"); |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
public abstract class Shape { |
|||
public abstract void draw(); |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
public class ShapeDrawer { |
|||
// 作业要求的方法名:drawShape
|
|||
public void drawShape(Shape s) { |
|||
s.draw(); |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 145 KiB |
Loading…
Reference in new issue