[英]Java - How to check if an object is an instance of a class in the class definition
Code: 码:
public class Composite extends Component implements Iterable<Component>{
private List<Component> caseComponents = new ArrayList<Component>();
@Override
public Iterator<Component> iterator(){
return new CompIterator();
}
private class CompIterator implements Iterator{
int index = 0;
@Override
public boolean hasNext(){
if(index<caseComponents.size()){
return true;
}
return false;
}
@Override
public Object next() {
if(this.hasNext()){
return caseComponents.get(index++);
}
return null;
}
}
public String breadthFirst() {
Queue<Component> q = new LinkedList<Component>();
StringBuilder stringRep = new StringBuilder();
q.add(this);
while(q.element()!=null){
Component first = q.remove();
System.out.println(first.name);
for(Component c : first.caseComponents){
q.add(c);
}
}
}
I'm trying to do a breadth first print of a tree of the Composite design pattern. 我正在尝试对Composite设计模式的树进行广度优先打印。 I have declared a method that is supposed to be called by the "root" of the tree.
我已经声明了应该由树的“根”调用的方法。 In the tree, there will be objects of the classes
Composite
and Leaf
. 在树中,将存在类
Composite
和Leaf
对象。 These both inherit from Component
, but are of course different. 这些都继承自
Component
,但是当然有所不同。 Composite
is a container objects as well as an object itself, while Leaf
is just one object. Composite
是容器对象以及对象本身,而Leaf
只是一个对象。 I want to change my code so that the line for(Component c : first.caseComponents)
is only executed if first
is an instance of Composite
, otherwise it would try to iterate over a Leaf
which is not possible. 我想更改代码,以便仅当
first
是Composite
的实例时才执行for(Component c : first.caseComponents)
行,否则它将尝试遍历Leaf
,这是不可能的。 How can I make it so that the for statement is only entered if first
is a Composite
? 我怎样才能使for语句仅在
first
是Composite
时才输入?
Use instanceof
to check if comp
is an instance of Composite
. 使用
instanceof
检查comp
是否为Composite
的实例。
eg: 例如:
if (comp instanceof Composite) {
//do something
}
You can restructure your code to apply the visitor pattern: 您可以重组代码以应用访问者模式:
public interface ComponentVisitor {
void visitComposite(Composite composite);
void visitLeaf(Leaf leaf);
}
public interface Component {
void acceptVisitor(ComponentVisitor visitor);
}
public class Composite implements Component {
@Override public void acceptVisitor(ComponentVisitor visitor) {
visitor.visitComposite(this);
}
}
public class Leaf implements Component {
@Override public void acceptVisitor(ComponentVisitor visitor) {
visitor.visitLeaf(this);
}
}
The visitor pattern is a compile-time safe alternative to runtime checks and casts and can also help static analysis of your code. 访问者模式是运行时检查和强制转换的一种编译时安全替代方法,它还可以帮助您对代码进行静态分析。 The program code will look like
程序代码看起来像
public class ComponentPrinter implements ComponentVisitor {
public void printComponent(Component component) {
component.acceptVisitor(this);
}
@Override public void visitComposite(Composite composite) {
for (Component child : composite) {
printComponent(component);
}
}
@Override public void visitLeaf(Leaf leaf) {
System.out.println("Found leaf: " + leaf);
}
}
Incidentally, the demo program does a depth-first print, but you can of course use a queue to accumulate components and then print breadth-first 顺便说一句,演示程序进行深度优先打印,但是您当然可以使用队列来累积组件,然后先进行广度优先打印
I would get rid of both Composite and Leaf altogether, and introduce a Node class that can have children. 我将完全摆脱Composite和Leaf,并介绍一个可以有孩子的Node类。 If it does, it's an intermediate node;
如果是这样,则它是一个中间节点。 if not, it's a leaf.
如果没有,那是一片叶子。 As this property can change any time, it is violently inappropriate to use different classes to express it.
由于此属性可以随时更改,因此使用不同的类来表达它是非常不合适的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.