我在将此UML Diagram设置转换为代码时遇到问题。我理解Class是最重要的部分,后跟变量和方法。我是Java的新手,这可能是我遇到的最令人困惑的事情。我不是100%肯定如何创建"模型"或者主要代码部分,但这是我到目前为止为Domain类启动/尝试的代码。我是在正确的轨道上吗?或者我完全迷失了。任何提示,或者向我发送正确方向的东西都将非常感激。
//This is the main .java file
public class Assignment3 extends Application {
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root);
Canvas canvas = new Canvas(400, 300); // Set canvas Size in Pixels
stage.setTitle("FXGraphicsTemplate"); // Set window title
root.getChildren().add(canvas);
stage.setScene(scene);
GraphicsContext gc = canvas.getGraphicsContext2D();
Castle stateDomain = new Castle();
Castle drawDomain = new Castle();
stage.show();
}
public static void main (String[] args) {
launch ( args );
}
// .class file that is utterly awful and I don't know where to go
public class Castle {
private String castleName;
private double castleSize;
private double castleX;
private double castleY;
private int domainX;
private int domainY;
private String castleColor;
private String domainName;
public void stateDomain ( String castleName, int domainX, int domainY, String castleColor ) {
this.castleName = castleName;
this.domainX = domainX;
this.domainY = domainY;
this.castleColor = castleColor;
}
public void drawDomain ( GraphicsContext gc ) {
}
我想指出我只是在寻找一些方向。在这一点上任何事情都将是非常有用的,无论它是指向另一篇文章的链接还是一段让我的大脑前进的代码。 谢谢。
答案 0 :(得分:3)
描述类之间静态关系的最抽象方法是使用Association链接,它简单地指出在两个或更多类之间存在某种链接或依赖关系。有弱者和强者协会。 对于你的情况,我会认为它是强大的联想。
public class Domain
{
private double x, y, size;
private String name;
public void state(String name, int x, int y, Color color) {
//TODO
}
public void draw(arguments) {
//TODO
}
}
public class Castle
{
//all arguments in diagram and now you need a reference to the class Domain
private Domain domain;
//The rest is just writing what is in the diagram
}
但我必须说你的图表非常不完整。请查看:https://en.wikipedia.org/wiki/Class_diagram