Javaリファレンス変数super
参照変数super
public class EX7_3 {
public static void main(String[] args) {
Child child = new Child();
child.method();
}
}
class Parent{
int x = 10; // super
}
class Child extends Parent{
int x = 20; // this
void method(){
System.out.println("x= " + x); // x= 20
System.out.println("this.x= " + this.x); // this.x= 20
System.out.println("super.x= " + super.x); // super.x= 10
}
}
super():祖先の創造者
class MyPoint{
int x, y;
MyPoint(int x, int y){
this.x = x;
this.y = y;
}
}
class MyPoint3D extends MyPoint{
int z;
MyPoint3D(int x, int y, int z){
this.x = x; // 조상의 멤버를 초기화
this.y = y; // 조상의 멤버를 초기화
this.z = z;
}
// There is no default constructor available in 'MyPoint'
// 조상의 멤버는 super()로 초기화 해주는게 맞다.
}
super()
を使用)class MyPoint{
int x, y;
MyPoint(int x, int y){
this.x = x;
this.y = y;
}
}
class MyPoint3D extends MyPoint{
int z;
MyPoint3D(int x, int y, int z){
super(x, y); // MyPoint(x, y)와 같은 의미
// this.x = x; // 조상의 멤버를 초기화
// this.y = y; // 조상의 멤버를 초기화
this.z = z;
}
// There is no default constructor available in 'MyPoint'
// 조상의 멤버는 super()로 초기화 해주는게 맞다.
}
super();
を挿入します.Reference
この問題について(Javaリファレンス変数super), 我々は、より多くの情報をここで見つけました https://velog.io/@nathan29849/Java-참조변수-superテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol