第二次博客作业
一.前言
关于面向对象编程的一些重要概念和特点:
1.类(Class):类是面向对象编程的基础,它是一种抽象的数据类型,用于描述具有相似属性和行为的对象。类定义了对象的属性(成员变量)和行为(方法),是创建对象的模板。
2.对象(Object):对象是类的实例化结果,它是具体的、可以操作的实体。每个对象都有自己的状态(属性值)和行为(方法调用),对象之间通过消息传递进行交互。
3.封装(Encapsulation):封装是将数据和操作封装在类中,通过访问修饰符控制对类成员的访问权限。封装可以隐藏实现细节,提供对外的接口,实现数据的安全性和代码的模块化。
4.继承(Inheritance):继承是一种机制,允许一个类继承另一个类的属性和方法。通过继承,子类可以继承父类的特性,并可以在此基础上进行扩展或修改。继承实现了代码的重用和层次化的组织。
5.多态(Polymorphism):多态是指同一类型的对象在不同的情况下表现出不同的行为。通过多态,可以在父类引用指向子类对象,并根据实际对象的类型调用相应的方法,实现灵活和可扩展的代码。
二.设计与分析:
在模拟小型测试系列中涉及了几个重要的知识点:
-
面向对象编程:
- 使用类(
Question,TestPaper,AnswerSheet)来组织相关数据和行为,使代码更加模块化、可扩展和易于维护。 - 在
Question类中封装了题目的相关信息和方法,如获取题目内容、获取题目答案以及检查用户答案是否正确等。 TestPaper类用于管理题目,可以添加题目并获取题目数量和具体题目对象。AnswerSheet类用于管理答题信息,包括添加答案、检查答案并输出判题结果。
- 使用类(
-
集合类的使用:
- 使用
List集合类来存储题目对象和答题信息,以便动态添加和访问数据。 - 在
TestPaper类中使用List<Question>来存储题目对象,便于管理多个题目。 - 在
AnswerSheet类中使用List<String>来存储用户的答案,以及使用List<Boolean>来存储判题结果。
- 使用
-
字符串操作:
- 使用
String类的split()方法来解析输入的字符串,提取出题目内容、题号和答案等信息。 - 使用
String类的equals()方法来比较用户答案和标准答案是否相等。
- 使用
-
输入输出:
- 使用
Scanner类来从标准输入读取用户输入的数据。 - 使用
System.out.println()方法来输出判题结果。
- 使用
-
条件语句和循环语句:
- 使用
for循环和if条件语句来遍历题目列表、检查答案和输出判题结果。
- 使用
以下为演示代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Question {
private int number;
private String content;
private String answer;
public Question(int number, String content, String answer) {
this.number = number;
this.content = content;
this.answer = answer;
}
public int getNumber() {
return number;
}
public String getContent() {
return content;
}
public String getAnswer() {
return answer;
}
public boolean checkAnswer(String userAnswer) {
return answer.equals(userAnswer);
}
}
class TestPaper {
private List
public TestPaper() {
questions = new ArrayList<>();
}
public void addQuestion(Question question) {
questions.add(question);
}
public int getQuestionCount() {
return questions.size();
}
public Question getQuestion(int index) {
return questions.get(index);
}
}
class AnswerSheet {
private TestPaper testPaper;
private List
private List
public AnswerSheet(TestPaper testPaper) {
this.testPaper = testPaper;
answers = new ArrayList<>();
results = new ArrayList<>();
}
public void addAnswer(String answer) {
answers.add(answer);
}
public void checkAnswers() {
for (int i = 0; i < answers.size(); i++) {
Question question = testPaper.getQuestion(i);
String userAnswer = answers.get(i);
boolean result = question.checkAnswer(userAnswer);
results.add(result);
}
}
public void printResults() {
for (int i = 0; i < results.size(); i++) {
Question question = testPaper.getQuestion(i);
boolean result = results.get(i);
System.out.println(question.getContent() + " ~" + question.getAnswer());
System.out.println(result);
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int questionCount = Integer.parseInt(scanner.nextLine());
TestPaper testPaper = new TestPaper();
for (int i = 0; i < questionCount; i++) {
String line = scanner.nextLine();
String[] parts = line.split(" ");
int number = Integer.parseInt(parts[0].split(":")[1]);
String content = parts[1].split(":")[1];
String answer = parts[2].split(":")[1];
Question question = new Question(number, content, answer);
testPaper.addQuestion(question);
}
AnswerSheet answerSheet = new AnswerSheet(testPaper);
String line = scanner.nextLine();
while (!line.equals("end")) {
String answer = line.split(":")[1];
answerSheet.addAnswer(answer);
line = scanner.nextLine();
}
answerSheet.checkAnswers();
answerSheet.printResults();
}
}
在设计智能家居强电电路模拟系统的Java代码时,涉及到了以下几个知识点:
- 类和对象:使用类来描述不同类型的设备和电路,通过创建对象来表示具体的设备和电路实例。
- 继承和多态:通过继承和多态来实现设备和电路之间的关系,例如
ControlDevice继承自Device,ParallelCircuit和SeriesCircuit继承自ControlCircuit。 - 集合类:使用
List和Map来存储和管理设备和引脚的信息。 - 输入和输出:根据题目要求,需要逐行读取输入信息,并进行解析和处理,最后按照指定格式输出设备状态或参数。
- 字符串处理:使用字符串的分割、拼接和替换等操作来解析输入信息和构建电路连接关系。
- 控制逻辑:根据控制信息对设备进行状态或参数的调节,例如开关的状态切换、调速器档位的增加或减少、连续调速器档位参数的设置等。
- 设计模式:可以使用适当的设计模式来提高代码的可扩展性和可维护性,例如工厂模式用于创建设备对象,观察者模式用于更新设备状态。
Device类:描述电路设备的公共特征,包括设备标识、引脚信息、电阻等。
ControlDevice类:继承自Device类,表示控制设备,包括开关、分档调速器和连续调速器。它具有状态或档位属性,可以进行状态或档位的调节。
ControlSwitch类:继承自ControlDevice类,表示开关。它具有开关状态属性,可以切换开关的状态。
GearSpeedController类:继承自ControlDevice类,表示分档调速器。它具有档位属性,可以增加或减少档位。
ContinuousSpeedController类:继承自ControlDevice类,表示连续调速器。它具有档位参数属性,可以设置档位参数。
ControlSystem类:表示控制系统,包含多个控制设备。它可以根据输入的控制信息对设备进行调节。
ControlCircuit类:表示电路,包含多个设备的连接关系。它可以根据输入的连接信息构建电路。
ParallelCircuit类:继承自ControlCircuit类,表示并联电路。
SeriesCircuit类:继承自ControlCircuit类,表示串联电路。
Light类:继承自Device类,表示灯具。它具有亮度属性。
Fan类:继承自Device类,表示风扇。它具有转速属性。
以下为演示代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
// 设备类
class Device {
private String identifier;
private Map<Integer, String> pins;
private int resistance;
public Device(String identifier, int resistance) {
this.identifier = identifier;
this.resistance = resistance;
this.pins = new HashMap<>();
}
public String getIdentifier() {
return identifier;
}
public int getResistance() {
return resistance;
}
public void addPin(int pinNumber, String pinType) {
pins.put(pinNumber, pinType);
}
public String getPinType(int pinNumber) {
return pins.get(pinNumber);
}
}
// 控制设备类
class ControlDevice extends Device {
public ControlDevice(String identifier, int resistance) {
super(identifier, resistance);
}
}
// 开关类
class ControlSwitch extends ControlDevice {
private int state;
public ControlSwitch(String identifier, int resistance) {
super(identifier, resistance);
this.state = 0;
}
public void toggleState() {
state = (state == 0) ? 1 : 0;
}
public int getState() {
return state;
}
}
// 分档调速器类
class GearSpeedController extends ControlDevice {
private int gear;
public GearSpeedController(String identifier, int resistance) {
super(identifier, resistance);
this.gear = 0;
}
public void increaseGear() {
if (gear < 3) {
gear++;
}
}
public void decreaseGear() {
if (gear > 0) {
gear--;
}
}
public int getGear() {
return gear;
}
}
// 连续调速器类
class ContinuousSpeedController extends ControlDevice {
private double gearValue;
public ContinuousSpeedController(String identifier, int resistance) {
super(identifier, resistance);
this.gearValue = 0.0;
}
public void setGearValue(double value) {
if (value >= 0.0 && value <= 1.0) {
gearValue = value;
}
}
public double getGearValue() {
return gearValue;
}
}
// 控制系统类
class ControlSystem {
private List
public ControlSystem() {
controlDevices = new ArrayList<>();
}
public void addControlDevice(ControlDevice device) {
controlDevices.add(device);
}
public ControlDevice getControlDevice(String identifier) {
for (ControlDevice device : controlDevices) {
if (device.getIdentifier().equals(identifier)) {
return device;
}
}
return null;
}
}
// 电路类
class ControlCircuit {
private List
public ControlCircuit() {
devices = new ArrayList<>();
}
public void addDevice(Device device) {
devices.add(device);
}
public Device getDevice(String identifier) {
for (Device device : devices) {
if (device.getIdentifier().equals(identifier)) {
return device;
}
}
return null;
}
}
// 并联电路类
class ParallelCircuit extends ControlCircuit {
public ParallelCircuit() {
super();
}
}
// 串联电路类
class SeriesCircuit extends ControlCircuit {
public SeriesCircuit() {
super();
}
}
// 灯类
class Light extends Device {
private int brightness;
public Light(String identifier, int resistance) {
super(identifier, resistance);
this.brightness = 0;
}
public void setBrightness(int brightness) {
this.brightness = brightness;
}
public int getBrightness() {
return brightness;
}
}
// 风扇类
class Fan extends Device {
private int speed;
public Fan(String identifier, int resistance) {
super(identifier, resistance);
this.speed = 0;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getSpeed() {
return speed;
}
}
public class Main {
public static void main(String[] args) {
// 在这里实现输入和输出的逻辑
}
}
三.总结
通过设计类和对象的方式来模拟真实世界中的设备和电路,需要考虑设备之间的关系、属性和行为,以及如何组织和管理它们。这种设计思维进一步培养了我的抽象能力和系统思维。使用面向对象的编程方式,将问题分解为类和对象,每个类负责特定的功能和属性。通过继承、多态等概念,可以更好地组织和复用代码,提高代码的可维护性和可扩展性。根据题目要求,需要逐行读取输入信息,并进行解析和处理,最后按照指定格式输出设备状态或参数。更加对输入输出的处理能力和字符串处理的技巧。根据控制信息对设备进行状态或参数的调节,需要编写相应的逻辑控制代码。熟悉了条件判断、循环和方法调用等编程结构,同时提升了我的问题解决能力。在代码实现中,我尝试使用适当的设计模式来提高代码的可扩展性和可维护性。例如,工厂模式用于创建设备对象,观察者模式用于更新设备状态。这些设计模式的应用使更好地理解和应用软件设计的原则和模式。
posted on 2024-06-09 22:36 23201836-高歌 阅读(27) 评论(0) 收藏 举报
浙公网安备 33010602011771号