다음 중첩 클래스를 고려하십시오.다른 클래스의 내부 클래스 객체 배열 만들기
class Outerclass {
class innerclass {
}
}
class util {
//how to declare an array of innerclass objects here?
}
다음 중첩 클래스를 고려하십시오.다른 클래스의 내부 클래스 객체 배열 만들기
class Outerclass {
class innerclass {
}
}
class util {
//how to declare an array of innerclass objects here?
}
이렇게 innerclass
개체의 배열을 선언 할 수 있습니다. 그리고
class util {
Outerclass.innerclass[] inner = new Outerclass.innerclass[10];
}
당신이
util
클래스 내에서 같은 것을 할 수있는 그것들을 초기화합니다.
void test() {
Outerclass outer = new Outerclass();
inner[0] = outer.new innerclass();
}
를 참조 MyClass의 []는 = 새로운 MyClass에 [5] 우리는 다시 초기화하지 않습니까? 그래서 우리가 새로운 Outerclass.innerclass [10]를 말할 때; 배열에 메모리를 할당하기에 충분하지 않습니까? 우리는 왜해야합니까? Outerclass outer = new Outerclass(); inner [0] = outer.new innerclass(); ? – Namratha
'innerclass'의 배열을 만들었습니다. 배열의 각 요소를 초기화해야하지 않습니까? 이것이 'int'와 같은 원시 데이터 유형이라면, 모든 요소는 기본적으로'0'으로 채워질 것입니다. 하지만이 경우에는 'null'이됩니다. 모든 배열 요소를 초기화해야합니다. 방금 배열을 선언하고 초기화했지만 배열의 요소를 초기화하지 않았습니다. – SudoRahul
은 새로운 Outerclass.innerclass [10]가 아닙니다. 외부 클래스와 내부 클래스 모두의 생성자를 호출 하시겠습니까? 내부 클래스의 생성자가 호출되면 자체적으로 초기화하지 않아도됩니다. – Namratha
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerArray[] = new OuterClass.InnerClass[3];
// Creating Objects of Inner Class
OuterClass.InnerClass innerObject1 = outerObject.new InnerClass();
OuterClass.InnerClass innerObject2 = outerObject.new InnerClass();
OuterClass.InnerClass innerObject3 = outerObject.new InnerClass();
// Adding the Objects to the Array
innerArray[0] = innerObject1;
innerArray[0] = innerObject2;
innerArray[0] = innerObject3;
당신이 말할 때 http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class – link64