如何使用 UML class 图实现从父 class 派生的 class 对象类型
How to implement class object types that derive from parent class using UML class diagram
我正在通过使用 classes 创建简单的交通灯逻辑来学习 C++。我在其中找到了这个 UML 图,我想以此为基础创建对象类型。但是,我创建了 TrafficLight class 对象,努力理解控制 class 逻辑部分。
编辑
对我来说,我只想为 N、W、S 和 E 创建交通灯。我可以单独设置灯。也许我想偏离 UML。我只想学习一种有效的方法来做到这一点,任何帮助表示赞赏。我看到 UML 操作与构造函数重叠。
这是我用 C++ 实现的 UML 图像
我不明白控制对象部分。我的理解是控制与 TrafficLight class.
直接关联
添加了详细的对象图
其中每个UML箭头都显示1.1 不知道是什么意思?
我需要 class 接口名称为 N,W,E,S 的控件还是只需要四个 class 实例就足够了?
我如何根据这个 UML 实现具有四个对象 N、W、E、S 的控件 class?
这是我的代码,使用 -std=c++11 编译器标志。
#include <iostream>
using namespace std;
class TrafficLight {
public:
enum light { red, amber, green};
// Setting the light by default red
TrafficLight(light=red){
cout << "constructed" << '\n';
} // Default constructor
// light state() {
// std::cout << "Setting the t light" << '\n';
// }
void CreateLight(light l) {
std::cout << "Creating lights" << '\n';
// show on console which lights are turned on.
if (l == light::green)
cout << "Green ligh is ON" << '\n';
else if (l == light::amber)
cout << "Amber ligh is ON" << '\n';
else
cout << "Red ligh is ON" << '\n';
}
void SwitchLight(light l) {
// switchLight operations executed
}
private:
light _state;
};
// Wondering how to proceed here? Create Light control for west, east, north and south.
class Control : public TrafficLight {
};
int main(void) {
/* code */
std::cout << " Welcome to Trafficlight signaling system " << '\n';
TrafficLight x(TrafficLight::green);
TrafficLight::light l;
// l = x.state();
l = TrafficLight::green;
x.CreateLight(TrafficLight::green);
x.CreateLight(TrafficLight::red);
x.CreateLight(TrafficLight::amber);
x.CreateLight(TrafficLight::green);
// run the derived class instance to create N,S,W,E
// Control c_obj;
return 0;
}
根据您的图表,您的 Control
将有 4 个 TrafficLight
实例。
虽然它没有说明具体如何工作,但我假设打算从 switchControl
和 createLights
调用 switchLight
和 createLight
,并且您不需要直接使用 switchLight
和 createLight
。
一些代码看起来有点像:
class TrafficLight
{
public:
enum lightColor {red, amber, green};
TrafficLight(lightColor color) : color(color){}
void SwitchLight()
{
// Change to the next state.
if(color == green) color = red;
else ++color;
}
private:
lightColor color;
}
class Control
{
public:
Control() : N(green), W(red), S(green), E(red){}
void SwitchControl()
{
// All lights change to the next state.
N.SwitchLight();
W.SwitchLight();
E.SwitchLight();
S.SwitchLight();
}
private:
TrafficLight N, W, S, E;
}
int main()
{
Control c; // N and S are green, W and E are red.
c.SwitchControl(); // N and S are red, W and E are amber.
c.SwitchControl(); // N and S are amber, W and E are green.
}
您唯一要调用的函数是 Control
和 SwitchControl()
.
的构造函数
请注意,我没有为 lightColor
调用全名,您实际上需要这样做才能使代码正常工作。我还删除了 Create
函数,因为它们与 ctors 有点重叠。
我正在通过使用 classes 创建简单的交通灯逻辑来学习 C++。我在其中找到了这个 UML 图,我想以此为基础创建对象类型。但是,我创建了 TrafficLight class 对象,努力理解控制 class 逻辑部分。
编辑
对我来说,我只想为 N、W、S 和 E 创建交通灯。我可以单独设置灯。也许我想偏离 UML。我只想学习一种有效的方法来做到这一点,任何帮助表示赞赏。我看到 UML 操作与构造函数重叠。
这是我用 C++ 实现的 UML 图像
我不明白控制对象部分。我的理解是控制与 TrafficLight class.
直接关联添加了详细的对象图
我需要 class 接口名称为 N,W,E,S 的控件还是只需要四个 class 实例就足够了?
我如何根据这个 UML 实现具有四个对象 N、W、E、S 的控件 class?
这是我的代码,使用 -std=c++11 编译器标志。
#include <iostream>
using namespace std;
class TrafficLight {
public:
enum light { red, amber, green};
// Setting the light by default red
TrafficLight(light=red){
cout << "constructed" << '\n';
} // Default constructor
// light state() {
// std::cout << "Setting the t light" << '\n';
// }
void CreateLight(light l) {
std::cout << "Creating lights" << '\n';
// show on console which lights are turned on.
if (l == light::green)
cout << "Green ligh is ON" << '\n';
else if (l == light::amber)
cout << "Amber ligh is ON" << '\n';
else
cout << "Red ligh is ON" << '\n';
}
void SwitchLight(light l) {
// switchLight operations executed
}
private:
light _state;
};
// Wondering how to proceed here? Create Light control for west, east, north and south.
class Control : public TrafficLight {
};
int main(void) {
/* code */
std::cout << " Welcome to Trafficlight signaling system " << '\n';
TrafficLight x(TrafficLight::green);
TrafficLight::light l;
// l = x.state();
l = TrafficLight::green;
x.CreateLight(TrafficLight::green);
x.CreateLight(TrafficLight::red);
x.CreateLight(TrafficLight::amber);
x.CreateLight(TrafficLight::green);
// run the derived class instance to create N,S,W,E
// Control c_obj;
return 0;
}
根据您的图表,您的 Control
将有 4 个 TrafficLight
实例。
虽然它没有说明具体如何工作,但我假设打算从 switchControl
和 createLights
调用 switchLight
和 createLight
,并且您不需要直接使用 switchLight
和 createLight
。
一些代码看起来有点像:
class TrafficLight
{
public:
enum lightColor {red, amber, green};
TrafficLight(lightColor color) : color(color){}
void SwitchLight()
{
// Change to the next state.
if(color == green) color = red;
else ++color;
}
private:
lightColor color;
}
class Control
{
public:
Control() : N(green), W(red), S(green), E(red){}
void SwitchControl()
{
// All lights change to the next state.
N.SwitchLight();
W.SwitchLight();
E.SwitchLight();
S.SwitchLight();
}
private:
TrafficLight N, W, S, E;
}
int main()
{
Control c; // N and S are green, W and E are red.
c.SwitchControl(); // N and S are red, W and E are amber.
c.SwitchControl(); // N and S are amber, W and E are green.
}
您唯一要调用的函数是 Control
和 SwitchControl()
.
请注意,我没有为 lightColor
调用全名,您实际上需要这样做才能使代码正常工作。我还删除了 Create
函数,因为它们与 ctors 有点重叠。