gpt4 book ai didi

java - 处理中的 OOP,我应该如何构建我的类?

转载 作者:行者123 更新时间:2023-12-01 18:36:08 26 4
gpt4 key购买 nike

目前我正在尝试构建一个应用程序,该应用程序将在处理中可视化 xml 树。我是Processing(以及Java)的新手,并且在将我的想法转化为对象时遇到一些问题。目前我有一个 Node 类:

class Node {

//DATA
color textColor;
color boxColor;
float xpos = width/2;
float ypos = 100;
float nodeWidth;
float nodeHeight;
boolean overBox = false;
boolean active = false;
PFont font;

//CONSTRUCTOR
Node(){
textColor = color(0);//sets text color
boxColor = color(244);//sets box color
font = createFont("Gil Sans", 16, true);
textFont(font,50);
nodeWidth = textWidth("Modernism");//INPUT TEXT
nodeHeight = textAscent();//?textDescent()?

rectMode(RADIUS);
}

void displayText(){
fill(textColor);
text("Modernism",xpos-nodeWidth/2,ypos+nodeHeight/2.3);
}

void displayBox(){
//stroke(boxColor);
noStroke();
noFill();
rect(xpos, ypos, nodeWidth/2, nodeHeight/2);

//FOR DEBUGGING OVERBOX
//stroke(135);
//point(300,200);
}

void overBox(){
if(mouseX > xpos-nodeWidth/2 && mouseX < xpos+nodeWidth/2 &&
mouseY > ypos-nodeHeight/2 && mouseY < ypos+nodeHeight/2) {
overBox = true;
}else{
overBox = false;
}
}

void clicked(){

if(active) {
//If box was already clicked, trigger response
textColor = color(0);
overBox = false;
active = false;
}

if(overBox) {
//checks to see if click happened within bounds of box, makes active
textColor = color(100);
active = true;
}
}

boolean activeCheck(){
if(active == true){
return true;
}else{
return false;
}
}

}

然后我想要在 XML 文档的父级和子级之间绘制一条连接电缆:

class Connectors{
//DATA
color lineColor;
int lineWeight;
int lineX1 = 12;
int lineY1 = 155;
int lineX2 = 12;
int lineY2 = 475;

//CONSTRUCTOR
Connectors(){
lineColor = color(0);
lineWeight = 2;
}

//FUNCTIONALITY
void displayConnection(){
stroke(lineColor);
strokeWeight(lineWeight);
line(lineX1,lineY1,lineX2,lineY2);
}
}

这一切都非常粗糙,但我想知道连接应该如何与节点相关。为了建立连接,它需要知道它的父节点位于哪里。连接应该是其各自节点的子类吗?

最佳答案

你应该青睐 aggregation在这种情况下,过度继承。也就是说,让Connector 保留对两个Node 的引用。

public class Connector {

// other members
private Node firstNode;
private Node secondNode;

public Connector(Node firstNode, Node secondNode) {
this.firstNode = firstNode;
this.secondNode = secondNode;
}

}

现在连接器知道它连接的两个节点

您想要这样做的原因是 NodeConnector 没有太多共同点。我们有关于如何将 OOP 关系翻译成英语的约定。

  • 使用“is a”来描述继承。
  • 使用“has a”来描述聚合和组合。

哪个更有意义? “连接器是一个节点”,或者“连接器有一个节点”?

现在,您可能想要为 ConnectorNode 创建一个抽象父类,或者更好的是一个接口(interface),如果它们确实共享一些共同点的话,例如绘制它们的能力。

public interface MyDrawable {

/**
* Draw this object on the "processing" canvas
* note: 2rs2ts does not know anything about the "processing" library
*/
public void draw();

}

// another file...

public class Connector implements MyDrawable {

// all of the other stuff

public void draw() {
stroke(lineColor);
// etc.
}
}

继承意味着将父类所做的所有事情拉到子类中。 (这就是关键字 extends 的原因。)如果有关 Node 的内容与有关 Connector 的内容完全分开,则不要这样做一个继承另一个。如果您确实想获得 OOP 抽象,请将共性分离到接口(interface)或公共(public)父类中。

关于java - 处理中的 OOP,我应该如何构建我的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21943856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com