실제로 자바 디자인 패턴에 대한 책을 읽고 그리고 난 내가, 추상 클래스에 캐스트 나 퍼즐 코드를 건너 왔어요 복합 패턴에 대한 장에서 초보자 :추상 클래스로 캐스트 ... 어떻게 가능합니까?
http://www.amazon.com/Design-Patterns-Java-TM-Software/dp/0321333020/ 오전, 나는 또한이 하위 클래스가 추상 수퍼 클래스의 생성자를 호출 할 때 어떤 일이 발생하는지 잘 이해하지 못한다면 제발 나를 도와주세요 !!
내가 (설정 방문) 우리가 isTree
슈퍼 클래스 메소드가있는 동안 그의 추상 슈퍼 클래스 에 주조 후 서브 클래스의 isTree
메서드를 호출 할 수있는 방법
MachineComponent c = (MachineComponent) i.next();
if (visited.contains(c) || !c.isTree(visited))
isTree에 대해서 이야기 캐스트 추상?
package com.oozinoz.machine;
/*
* Copyright (c) 2001, 2005. Steven J. Metsker.
*/
import java.util.*;
import com.oozinoz.iterator.ComponentIterator;
/**
* Objects of this class represent either individual machines or composites of
* machines.
*/
public abstract class MachineComponent {
/*
* Subclasses implement this to support the isTree() algorithm.
*/
protected abstract boolean isTree(Set s);
// rest of class omitted
}
: 2 :
package com.oozinoz.machine;
/*
* Copyright (c) 2001, 2005. Steven J. Metsker.
*/
import java.util.*;
import com.oozinoz.iterator.ComponentIterator;
import com.oozinoz.iterator.CompositeIterator;
/**
* Represent a collection of machines: a manufacturing line, a bay, or a
* factory.
*/
public class MachineComposite extends MachineComponent {
protected List components = new ArrayList();
/**
* @param visited a set of visited nodes
* @return true if this composite is a tree
* @see MachineComponent#isTree()
*/
protected boolean isTree(Set visited) {
visited.add(this);
Iterator i = components.iterator();
while (i.hasNext()) {
MachineComponent c = (MachineComponent) i.next();
if (visited.contains(c) || !c.isTree(visited))
return false;
}
return true;
}
// rest of class omitted
}
당신은 단지에 전체 파일을 덤프 거라면 제대로 코드를 포맷하십시오 –