大家好,这是我在StackOverflow上的第一个问题
我是java的新手,我需要解决这个uml图。 我从我的一个同学那里得到了一个解决方案,但我不认为这是正确的,我是按照我的方式做到的。我的问题是哪一个解决方案是正确的?我知道关系的类型是关联类型。不是继承
她的代码
class Sensor {
protected int value;
protected String location;
public Sensor() { // default constructor
value = 0;
location = "North-West";
}
public Sensor(int value, String location) { // overridden constructor
this.value = value;
this.location = location;
}
protected int getValue() { // value getter
return value;
}
protected void setValue(int v) { // value setter
this.value = v;
}
protected void displaySenzorInfo() { // display information on the sensor
System.out.println("Temperature is " + value + ", located " + location + ".");
}
}
class Controller extends Sensor {
protected String name;
public Controller(String name) { // overridden constructor
this.name = name;
}
public Controller(String name, int value, String location) { // overridden
// instructor
this.name = name;
super.value = value;
super.location = location;
}
public Controller() { // default constructor, which creates a new Sensor()
//Sensor s = new Sensor();
}
protected void checkTemperature() { // checks temperature of sensor
System.out.println("Temperature of " + name + " is " + super.value + ", located at " + super.location + ".");
}
}
public class E3 {
public static void main(String[] args) {
Controller control = new Controller();
control.displaySenzorInfo();
Controller c = new Controller("Pizza", 30, "North");
c.checkTemperature();
}
}
我的代码
class Sensor{
int value;
String location;
Sensor(){
value=0;
location="Sibiu";
}
Sensor(int value,String location){
this.value=value;
this.location=location;
}
int getValue(){
return value;
}
void setValue(int v){
this.value=v;
}
void displaySenzorInfo(){
System.out.println("Temperature is " + value + ", located " + location + ".");
}
}
class Controller{
Sensor tempSensor;
String name;
Controller(){
name="Sibiu";
tempSensor=30;
}
Controller (String name,Sensor tempSensor){
this.name=name;
this.tempSensor=tempSensor;
}
void checkTemperature(Sensor tempSensor){
if (tempSensor>=30)
System.out.println("the temperature is too high!");
else
System.out.println("the temp is too low" );
}
}
public class E3{
public static void main(String []args){
Sensor s1=new Sensor();
Controller c1=new Controller();
c1.displaySenzorInfo();
Controller c2=new Controller(30,"Oliver");
}
}
拜托,伙计们。如果您有一些建议,或者您在m程序中发现任何问题,请告诉我。我知道我会有一些错误,因为我在任何IDE中都没有参加这个练习,因为我在工作而且我没有任何错误。谢谢!!!
答案 0 :(得分:1)
您的解决方案是正确的。正如您已经提到的,它是一个关联而不是继承。您可以在维基百科上看到继承的样子:https://en.wikipedia.org/wiki/Class_diagram
答案 1 :(得分:0)
虽然从给定图表的关系的整体编码(MyCode)是可以的,但我有以下观察。 (她的代码) - 继承不正确。单向关联是正确的。
如果这是图表仅用于练习目的,否则它会违反数据隐藏并鼓励客户端类违反封装(直接使用别人的数据)
tempSensor=30;
对于数据类型不正确。if (tempSensor>=30)
对于数据类型不正确,即使您更正,也会违反封装(对其他人的数据有效),这是第一次违反将实例变量设为非私有的效果。课程应该适用于他们自己的数据。如果图表无法更改,则只需删除checkTemperature()中的参数,并处理如上所示的数据类型。
但我建议在设计级别进行更改,以便更好地进行封装。
public class SensorNew {
private static final double UPPER_THRESHOLD = 25;
private static final double LOWER_THRESHOLD = 20;
private String location;
private Controller controller;
public SensorNew(String location, Controller controller) {
this.location = location;
this.controller = controller;
}
public int getCurrentTemp() {
// obtain from sensor hardware
return 10; // Just example
}
private void makePeriodicCheck(){
double currentTemp = getCurrentTemp();
if (currentTemp > UPPER_THRESHOLD){
controller.coolDown();
} else if (currentTemp < LOWER_THRESHOLD){
controller.heatUp();
} else {
controller.stopIfRunning();
}
}
public void displaySenzorInfo() { // replace by toString()
System.out.println("Temperature is " + getCurrentTemp()
+ ", located " + location + ".");
}
}
public class ControllerNew {
private String name;
// Need to maintain the state of Controller
// either by variable or State design pattern (preferred)
public ControllerNew(String name, Sensor tempSensor) {
this.name = name;
}
public void coolDown() {
// action depending upon current state of controller
}
public void heatUp() {
// action depending upon current state of controller
}
public void stopIfRunning() {
// action depending upon current state of controller
}
}
优点是我们不必为这些类提供公共getXX()setXX()方法。因此它保持封装。