Java의 경우 super()
나나?super()모컨 컨컨 ?출 출? ?? ??? 부탁드립니다.super().
super()는 인수 없이 부모 컨스트럭터를 호출합니다.
즉, '아예라는 뜻합니다.super(argument1), 「1」합니다.argument1(일부러)
또한 부모로부터할 때도 할 수 .super.aMethod()
자세한 내용은 이쪽
몇 가지 사실:
super()는 직계 부모 호출에 사용됩니다.super()인스턴스 멤버, 즉 인스턴스 변수 및 인스턴스 메서드와 함께 사용할 수 있습니다.super()는 컨스트럭터 내에서 사용하여 부모 클래스의 컨스트럭터를 호출할 수 있습니다.
이제 이 요?super().
1을 2로 하다 첫 2를 해 줍니다.super()★★★★★★★★★★★★★★★★★★.
프로그램 1
class Base
{
int a = 100;
}
class Sup1 extends Base
{
int a = 200;
void Show()
{
System.out.println(a);
System.out.println(a);
}
public static void main(String[] args)
{
new Sup1().Show();
}
}
출력:
200
200
이제 프로그램 2를 확인하고 주요 차이점을 알아보겠습니다.
프로그램 2
class Base
{
int a = 100;
}
class Sup2 extends Base
{
int a = 200;
void Show()
{
System.out.println(super.a);
System.out.println(a);
}
public static void main(String[] args)
{
new Sup2().Show();
}
}
출력:
100
200
프로그램 1에서 출력은 파생 클래스일 뿐입니다.기본 클래스나 상위 클래스의 변수를 인쇄할 수 없습니다.에서는 '2번 프로그램'을 super() '''를 a중 변수 값을 인쇄합니다.a클래스의 했습니다.a본본클클 클다다다다다 그것은 ''라는 것을 한다.super()는 직계 부모 호출에 사용됩니다.
자, 이제 프로그램 3과 프로그램 4의 차이를 확인합니다.
프로그램 3
class Base
{
int a = 100;
void Show()
{
System.out.println(a);
}
}
class Sup3 extends Base
{
int a = 200;
void Show()
{
System.out.println(a);
}
public static void Main(String[] args)
{
new Sup3().Show();
}
}
출력:
200
200파운드했을 때Show() , . . . . . . . .Show()이치노 이 전화를 요?Show()해 주세요.4번
프로그램 4
class Base
{
int a = 100;
void Show()
{
System.out.println(a);
}
}
class Sup4 extends Base
{
int a = 200;
void Show()
{
super.Show();
System.out.println(a);
}
public static void Main(String[] args)
{
new Sup4().Show();
}
}
출력:
100
200
여기서 100과 200의 2개의 출력을 얻을 수 있습니다.?Show()로 「」를 합니다.Show()입니다. 부모 클래스의 이기 때문입니다.왜냐하면 내부에서는Show()클래스의 , 파생된 클래스의 함수, 파생된 클래스의 .Show()('는 부모 클래스의 함수)를 지정합니다.super키워드를 지정합니다.
출처 문서: Java: Calling super()
ㅇㅇ.super(...)슈퍼클래스의 컨스트럭터를 호출합니다.
★★★★★★★★★★★★★★★★★★:
class Animal {
public Animal(String arg) {
System.out.println("Constructing an animal: " + arg);
}
}
class Dog extends Animal {
public Dog() {
super("From Dog constructor");
System.out.println("Constructing a dog.");
}
}
public class Test {
public static void main(String[] a) {
new Dog();
}
}
인쇄:
Constructing an animal: From Dog constructor
Constructing a dog.
super()는 부모 컨스트럭터 호출에 사용됩니까?
네.
Super()에 대해 설명하겠습니다.
super() 사용하는 용법입니다.super파라미터가 없는 부모 컨스트럭터를 호출하는 키워드입니다. 「」의super키워드를 사용하면 오버라이드된 메서드를 호출하거나 숨겨진 필드에 액세스하거나 슈퍼클래스의 생성자를 호출할 수 있습니다.
여기 공식 튜토리얼이 있습니다.
이론이 없는 슈퍼 컨스트럭터를 호출하는 것은 화면 공간과 프로그래머의 시간을 낭비하는 것에 불과합니다.컴파일러는 작성 여부에 관계없이 동일한 코드를 생성합니다.
class Explicit() {
Explicit() {
super();
}
}
class Implicit {
Implicit() {
}
}
ㅇㅇ.super()(임의) 부모 클래스의 컨스트럭터를 호출합니다.할 수 있습니다.super(foo, bar)
요.super할 수 입니다.
"Java super"의 빠른 구글을 통해 다음과 같은 결과를 얻을 수 있습니다.
그것이 맞아요.Super는 상위 생성자를 호출하는 데 사용됩니다.이런 코드 블록이 있다고 가정해 봅시다.
class A{
int n;
public A(int x){
n = x;
}
}
class B extends A{
int m;
public B(int x, int y){
super(x);
m = y;
}
}
그런 다음 멤버 변수n에 값을 할당할 수 있습니다.
나는 모든 답을 보았다.하지만 모두가 한 가지 중요한 점을 언급하는 것을 잊었다.
super()는 생성자의 첫 번째 행에서 호출하거나 사용해야 합니다.
클래스의 슈퍼클래스에 디폴트컨스트럭터가 존재하는 경우, super()만으로 디폴트컨스트럭터만 호출됩니다.단, 기본 생성자는 사용자가 직접 작성해야 합니다.Java가 구현되지 않은 경우 save super();를 저장하여 유니버설 Superclass 객체를 참조합니다.이 오브젝트는 서브클래스로 호출할 수 없습니다.
public class Alien{
public Alien(){ //Default constructor is written out by user
/** Implementation not shown…**/
}
}
public class WeirdAlien extends Alien{
public WeirdAlien(){
super(); //calls the default constructor in Alien.
}
}
예를 들어 셀레늄 자동화에서는 PageObject를 사용하여 상위 생성자를 다음과 같이 사용할 수 있습니다.
public class DeveloperSteps extends ScenarioSteps {
public DeveloperSteps(Pages pages) {
super(pages);
}........
제가 이해한 것을 코드와 공유하고 싶습니다.
Java의 super 키워드는 부모 클래스 개체를 참조하기 위해 사용되는 참조 변수입니다.주로 다음 컨텍스트에서 사용됩니다.
1. 변수와 함께 슈퍼 사용:
class Vehicle
{
int maxSpeed = 120;
}
/* sub class Car extending vehicle */
class Car extends Vehicle
{
int maxSpeed = 180;
void display()
{
/* print maxSpeed of base class (vehicle) */
System.out.println("Maximum Speed: " + super.maxSpeed);
}
}
/* Driver program to test */
class Test
{
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}
출력:-
Maximum Speed: 120
- 메서드와 함께 super 사용:
/* Base class Person */
class Person
{
void message()
{
System.out.println("This is person class");
}
}
/* Subclass Student */
class Student extends Person
{
void message()
{
System.out.println("This is student class");
}
// Note that display() is only in Student class
void display()
{
// will invoke or call current class message() method
message();
// will invoke or call parent class message() method
super.message();
}
}
/* Driver program to test */
class Test
{
public static void main(String args[])
{
Student s = new Student();
// calling display() of Student
s.display();
}
}
출력:-
This is student class
This is person class
3. 시공자와의 슈퍼 사용:
class Person
{
Person()
{
System.out.println("Person class Constructor");
}
}
/* subclass Student extending the Person class */
class Student extends Person
{
Student()
{
// invoke or call parent class constructor
super();
System.out.println("Student class Constructor");
}
}
/* Driver program to test*/
class Test
{
public static void main(String[] args)
{
Student s = new Student();
}
}
출력:-
Person class Constructor
Student class Constructor
SUPER는 어디에 사용할 수 있습니까?
Superclass 멤버 접근
인 메서드가 슈퍼클래스의 중 """를 할 수 .supersuper.methodName();
Superclass 컨스트럭터 호출
컨스트럭터가 명시적으로 슈퍼클래스 컨스트럭터를 호출하지 않으면 Java 컴파일러는 자동으로 슈퍼클래스의 인수 없는 컨스트럭터에 콜을 삽입합니다.슈퍼 클래스에 인수 없는 컨스트럭터가 없는 경우 컴파일 시 오류가 발생합니다.
다음 코드를 참조하십시오.
class Creature {
public Creature() {
system.out.println("Creature non argument constructor.");
}
}
class Animal extends Creature {
public Animal (String name) {
System.out.println("Animal one argument constructor");
}
public Animal (Stirng name,int age) {
this(name);
system.out.println("Animal two arguments constructor");
}
}
class Wolf extends Animal {
public Wolf() {
super("tigerwang",33);
system.out.println("Wolf non argument constructor");
}
public static void main(string[] args) {
new Wolf();
}
}
개체를 생성할 때 JVM은 항상 먼저 상속 트리의 최상위 계층 클래스에서 생성자를 실행합니다.그리고 유전 트리를 따라 내려갑니다.이것이 가능한 이유는 Java 컴파일러가 자동으로 슈퍼클래스의 인수 없는 컨스트럭터에 콜을 삽입하기 때문입니다.슈퍼클래스에 논인수 컨스트럭터가 없고 서브클래스가 슈퍼클래스에서 실행할 컨스트럭터를 명시적으로 지정하지 않으면 컴파일 시 오류가 발생합니다.
위 코드에서 Wolf 객체를 성공적으로 만들려면 클래스의 생성자가 실행되어야 합니다.그리고 그 과정에서 동물 클래스의 두 개의 논거 구성자가 호출됩니다.동시에 같은 클래스의 원 인수 컨스트럭터를 명시적으로 호출하고 원 인수 컨스트럭터는 Creature 클래스의 비 인수 컨스트럭터를 암묵적으로 호출하고 비 인수 컨스트럭터는 오브젝트 클래스의 빈 컨스트럭터를 암묵적으로 호출합니다.
생성자에서는 점 없이 사용하여 다른 생성자를 호출할 수 있습니다. super를 this합니다.
public MyClass(int a) {
this(a, 5); // Here, I call another one of this class's constructors.
}
public MyClass(int a, int b) {
super(a, b); // Then, I call one of the superclass's constructors.
}
super는 슈퍼클래스가 자신을 초기화할 필요가 있는 경우에 편리합니다. this는 어느 하나의 컨스트럭터에 모든 하드 초기화 코드를 한 번만 쓰고 다른 모든 컨스트럭터에서 호출할 때 유용합니다.
어떤 방법으로든 점으로 다른 메서드를 호출할 수 있습니다. super.method()합니다.this.method()합니다.
public String toString() {
int hp = this.hitpoints(); // Calls the hitpoints method in this class
// for this object.
String name = super.name(); // Calls the name method in the superclass
// for this object.
return "[" + name + ": " + hp + " HP]";
}
super는 특정를 사용하는 내의 가 필요한 합니다.클래스와 슈퍼클래스가 같은 메서드를 사용하는 경우 Java는 클래스 내의 메서드를 필요로 하는 것으로 간주합니다.super대신 슈퍼클래스의 메서드를 요구할 수 있습니다. this이 기능은 코드를 보다 읽기 쉽게 하기 위한 방법으로만 유용합니다.
super 키워드는 슈퍼클래스 컨스트럭터를 호출하여 슈퍼클래스 멤버를 참조하기 위해 사용할 수 있습니다.
올바른 인수를 사용하여 super()를 호출하면 실제로 변수 너비, 높이 및 깊이를 초기화하고 대응하는 파라미터 값을 사용하여 참조하는 컨스트럭터 Box를 호출합니다.남은 것은 부가가치 가중치 초기화뿐입니다.필요한 경우 변수 Box를 비공개로 클래스할 수 있습니다.[ Box class private ]수식자 필드에 입력하고 문제없이 접근할 수 있는지 확인합니다.
슈퍼클래스에는 오버로드된 버전의 컨스트럭터가 여러 개 있을 수 있으므로 다른 파라미터로 메서드 super()를 호출할 수 있습니다.프로그램은 지정된 인수와 일치하는 생성자를 수행합니다.
public class Box {
int width;
int height;
int depth;
Box(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
public static void main(String[] args){
HeavyBox heavy = new HeavyBox(12, 32, 23, 13);
}
}
class HeavyBox extends Box {
int weight;
HeavyBox(int w, int h, int d, int m) {
//call the superclass constructor
super(w, h, d);
weight = m;
}
}
super는 키워드입니다.서브클래스 메서드 정의 내에서 슈퍼클래스에 정의된 메서드를 호출하기 위해 사용됩니다.슈퍼클래스의 개인 메서드를 호출할 수 없습니다.super 키워드로 호출할 수 있는 것은 퍼블릭 메서드 및 보호 메서드뿐입니다.또한 클래스 생성자가 상위 클래스의 생성자를 호출하는 데 사용됩니다.
자세한 내용은 여기를 참조하십시오.
앞서 설명한 바와 같이 기본 생성자 내부에는 생성자의 첫 번째 줄에 호출된 암묵적인 super()가 있습니다.
이 super()는 클래스 계층의 맨 위에서 시작하는 컨스트럭터 체인을 자동으로 호출하고 계층 아래로 이동합니다.
프로그램의 클래스 계층에 3개 이상의 클래스가 있는 경우 상위 클래스 기본 생성자가 먼저 호출됩니다.
그 예를 다음에 나타냅니다.
class A {
A() {
System.out.println("Constructor A");
}
}
class B extends A{
public B() {
System.out.println("Constructor B");
}
}
class C extends B{
public C() {
System.out.println("Constructor C");
}
public static void main(String[] args) {
C c1 = new C();
}
}
위의 출력은 다음과 같습니다.
Constructor A
Constructor B
Constructor C
The super keyword in Java is a reference variable that is used to refer to the immediate parent class object.
Java super 키워드 사용방법
super는 직접 부모 클래스 인스턴스 변수를 참조하기 위해 사용할 수 있습니다.
super를 사용하여 즉시 부모 클래스 메서드를 호출할 수 있습니다.
super()를 사용하여 직접 부모 클래스 컨스트럭터를 호출할 수 있습니다.
언급URL : https://stackoverflow.com/questions/3767365/super-in-java
'programing' 카테고리의 다른 글
| VueJs: 텍스트 영역 입력 바인딩 (0) | 2022.08.14 |
|---|---|
| Nuxt VueJ에서 직접 소품 변환 방지 (0) | 2022.08.13 |
| main.js가 Vue-cli 웹 팩 템플릿의 Index.html에 링크되는 방법 (0) | 2022.08.13 |
| Mixins Vuejs에서 기능을 찾을 수 없습니다. (0) | 2022.08.13 |
| Vue 어플리케이션에서 Vuex를 사용하여 대용량 데이터셋을 효율적으로 처리 (0) | 2022.08.13 |
