나는 두 개의 인스턴스 필드를 포함하는 Creator라고 부르겠습니다. 이 필드는 부모 클래스 A에 속합니다. 생성자의 생성자에서 클래스 A의 자식을 전달한 다음 전달 된 형식에서 두 개체를 만들고 두 필드에 참조를 할당하려고합니다. 나는 어떻게 그런 것을 할 수 있습니까? 나는이 일반화를 만드는 방법을 모른다.오브젝트 유형을 전달하여 많은 오브젝트를 작성하는 방법은 무엇입니까?
편집 : 클래스 작성자 만은 A 또는 A 자체의 하위 유형을 허용해야합니다. 그래서 다른 어떤 일반적인 유형. A는 인수가없는 생성자에게 그래서 이런
이없는, 여기 GeneralClassifier A는하고 인수가없는 생성자가 없습니다 :
public class TwoLevelClassifier <T> {
private GeneralClassifier firstCl, secondCl;
//The passed type shall *only* be a GeneralClassifier or a child of it
public TwoLevelClassifier(GeneralClassifier cl) {
firstCl = //create a new classifier that is of the type passed to the constructor
secondCl = //create a new classifier that is of the type passed to the constructor
}
}
잘 모르겠어요,하지만 어쩌면이 기능은 자바에서 제네릭이라고?
리플렉션을 사용해야합니다. 이것을 참조하십시오 : http://stackoverflow.com/questions/10470263/create-new-object-using-reflection – Massimo
제네릭을 사용하지 않는 이유는 무엇입니까? 'public class Creator {' –
crush
자식 유형의 인스턴스를 ctor의 매개 변수로 전달해야합니까? 당신은 그걸 가지고 아무 것도하지 않으므로, 나는 그럴 수 없다고 생각합니다. – crush