我正在尝试使用类创建一个类似“骨骼”的系统Bone,并让“主要”骨骼的孩子由所有其他连接的骨骼组成。
public class Main {
public static void main(String[] args) {
Bone body = new Bone("primary", null);
Bone leftArm1 = new Bone("left_arm_1", body);
Bone leftArm2 = new Bone("left_arm_2", leftArm1);
Bone rightArm1 = new Bone("right_arm_1", body);
Bone rightArm2 = new Bone("right_arm_2", rightArm1);
List<Bone> bones = new ArrayList<Bone>();
for(Bone child : body.getChildren()) {
System.out.println(child.getName());
}
}
}
public class Bone {
private Bone parent = null;
private String name = null;
private List<Bone> children = new ArrayList<Bone>();
public Bone(String name, Bone parent) {
this.name = name;
this.parent = parent;
if(parent != null) {
parent.addChild(this);
}
}
public String getName() {
return this.name;
}
public Bone getParent() {
return this.parent;
}
public boolean hasParent() {
if(this.parent != null) {
return true;
} else {
return false;
}
}
public List<Bone> getChildren() {
return this.children;
}
public boolean hasChildren() {
if(this.children.isEmpty()) {
return false;
} else {
return true;
}
}
public void addChild(Bone child) {
this.children.add(child);
}
}
当前程序正在输出...
left_arm_1
right_arm_1
什么时候应该输出...
left_arm_1
left_arm_2
right_arm_1
right_arm_2
如何让程序输出正确的字符串?
慕妹3146593
不负相思意
相关分类