feat(class33-35): 简单家具控制系统
- 新增 A 接口和 Test 类,实现默认方法和静态方法 - 创建 JD 类和 Switch 接口,用于智能家居设备控制 - 添加 Air、Lamp、TV、WashMachine 类继承 JD - 实现 SmartHomeControl 类作为智能家居控制器 - 编写 Test 类测试智能家居控制功能
This commit is contained in:
14
src/class33/A.java
Normal file
14
src/class33/A.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package class33;
|
||||
|
||||
public interface A {
|
||||
default void go(){
|
||||
System.out.println("===go方法执行了===");
|
||||
run();
|
||||
};
|
||||
private void run(){
|
||||
System.out.println("===run方法执行了===");
|
||||
};
|
||||
static void show(){
|
||||
System.out.println("===show方法执行了===");
|
||||
};
|
||||
}
|
||||
12
src/class33/Test.java
Normal file
12
src/class33/Test.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package class33;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
AImpl a = new AImpl();
|
||||
a.go();
|
||||
A.show();
|
||||
}
|
||||
}
|
||||
class AImpl implements A{
|
||||
|
||||
}
|
||||
7
src/class34/Test.java
Normal file
7
src/class34/Test.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package class34;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
10
src/class35/Air.java
Normal file
10
src/class35/Air.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package class35;
|
||||
|
||||
public class Air extends JD{
|
||||
public Air(String name, boolean status) {
|
||||
super(name, status);
|
||||
}
|
||||
|
||||
public Air() {
|
||||
}
|
||||
}
|
||||
19
src/class35/JD.java
Normal file
19
src/class35/JD.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package class35;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class JD implements Switch{
|
||||
private String name;
|
||||
private boolean status;
|
||||
@Override
|
||||
public void press()
|
||||
{
|
||||
status = !status;
|
||||
}
|
||||
|
||||
}
|
||||
10
src/class35/Lamp.java
Normal file
10
src/class35/Lamp.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package class35;
|
||||
|
||||
public class Lamp extends JD{
|
||||
public Lamp(String name, boolean status) {
|
||||
super(name, status);
|
||||
}
|
||||
|
||||
public Lamp() {
|
||||
}
|
||||
}
|
||||
22
src/class35/SmartHomeControl.java
Normal file
22
src/class35/SmartHomeControl.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package class35;
|
||||
|
||||
public class SmartHomeControl {
|
||||
private static final SmartHomeControl smartHomeControl = new SmartHomeControl();
|
||||
private SmartHomeControl() {
|
||||
}
|
||||
public static SmartHomeControl getInstance() {
|
||||
return smartHomeControl;
|
||||
}
|
||||
public void control(JD s) {
|
||||
System.out.println(s.getName() + "状态目前是:" + (s.isStatus()?"开":"关"));
|
||||
System.out.println("开始您的操作......");
|
||||
s.press();
|
||||
System.out.println(s.getName() + "状态目前是:" + (s.isStatus()?"开":"关"));
|
||||
|
||||
}
|
||||
public void printAllStatus(JD[] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
System.out.println( i+1 +","+ arr[i].getName() + "状态目前是:" + (arr[i].isStatus()?"开":"关"));
|
||||
}
|
||||
}
|
||||
}
|
||||
5
src/class35/Switch.java
Normal file
5
src/class35/Switch.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package class35;
|
||||
|
||||
public interface Switch {
|
||||
void press();
|
||||
}
|
||||
12
src/class35/TV.java
Normal file
12
src/class35/TV.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package class35;
|
||||
|
||||
|
||||
public class TV extends JD{
|
||||
public TV(String name, boolean status) {
|
||||
super(name, status);
|
||||
}
|
||||
public TV(){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
45
src/class35/Test.java
Normal file
45
src/class35/Test.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package class35;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
JD[] jds = new JD[4];
|
||||
jds[0] = new TV("TV",true);
|
||||
jds[1] = new WashMachine("WashMachine",true);
|
||||
jds[2] = new Lamp("Lamp",true);
|
||||
jds[3] = new Air("Air",true);
|
||||
|
||||
SmartHomeControl control = SmartHomeControl.getInstance();
|
||||
|
||||
control.control(jds[0]);
|
||||
|
||||
while (true) {
|
||||
control.printAllStatus(jds);
|
||||
System.out.println("请选择要控制的设备:");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String index = sc.next();
|
||||
switch (index){
|
||||
case "1":
|
||||
control.control(jds[0]);
|
||||
break;
|
||||
case "2":
|
||||
control.control(jds[1]);
|
||||
break;
|
||||
case "3":
|
||||
control.control(jds[2]);
|
||||
break;
|
||||
case "4":
|
||||
control.control(jds[3]);
|
||||
break;
|
||||
case "exit":
|
||||
System.out.println("退出");
|
||||
return;
|
||||
default:
|
||||
System.out.println("输入错误");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
10
src/class35/WashMachine.java
Normal file
10
src/class35/WashMachine.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package class35;
|
||||
|
||||
public class WashMachine extends JD{
|
||||
public WashMachine(String name, boolean status) {
|
||||
super(name, status);
|
||||
}
|
||||
|
||||
public WashMachine() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user