Java의 슈퍼 키워드
Super는 슈퍼클래스에서 함수나 메소드를 호출하는 데 사용되는 키워드입니다. 이는 하위 클래스 내부에서 정의됩니다. public 및 protected만 있는 메서드는 이 키워드를 사용하여 호출할 수 있습니다. 즉, 이를 이용하여 Private 메소드와 static 메소드를 호출할 수 없습니다. 상위 클래스의 생성자를 호출하기 위해 Java의 super 키워드를 사용할 수도 있습니다. super 키워드의 구문, 예 및 자세한 내용은 다음 섹션에서 설명합니다.
구문
무료 소프트웨어 개발 과정 시작
웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등
super.<<method-name>> or super([0 or more arguments]);
Java에서 Super 키워드는 어떻게 작동하나요?
이미 언급했듯이 super는 여러 경우에 사용될 수 있습니다.
- 직계 상위 클래스의 인스턴스 변수를 참조합니다.
- 직계 상위 클래스의 메서드를 참조합니다.
- 직속 상위 클래스의 생성자를 참조합니다.
1. 직계 상위 클래스의 인스턴스 변수를 참조하려면
상위 클래스와 하위 클래스의 데이터 멤버가 동일한 경우 Super 키워드를 사용하여 상위 클래스의 필드 또는 데이터 멤버에 액세스할 수 있습니다. 이 경우 Java Virtual Machine에 대한 모호성이 발생할 수 있습니다.
예:
코드:
class A { protected String name="ann"; } class B extends A { public String name="Anna"; public void hello() { System.out.println("I am " + name); System.out.println("I am " + super.name); } }
여기서 두 클래스 A와 B에는 공통 필드 이름이 있습니다. 하위 클래스 내의 printType() 함수는 super 키워드를 사용하여 상위 클래스의 필드를 참조합니다.
2. 직계 부모 클래스 참조 방법
메서드 재정의는 상위 클래스에서 이미 사용할 수 있는 동일한 함수나 메서드를 하위 클래스에서 선언하는 프로세스입니다. 자식 클래스의 객체에서 메서드 호출이 발생하면 자식 클래스의 메서드만 호출된다고 가정해 보겠습니다. 상위 메소드에 액세스하려면 super 키워드를 사용할 수 있습니다.
예:
코드:
class A { protected String name="ann"; public void hello() { System.out.println("I am " + name); } } class B extends A { public String name="Anna”; public void hello() { System.out.println("I am " + name); } public void test() { hello(); super.hello(); } }
여기서 두 클래스 A와 B는 동일한 메소드 hello()를 가지고 있습니다. test() 함수의 super 키워드를 사용하면 상위 클래스의 hello() 메소드에 액세스할 수 있습니다.
3. 직계 부모 클래스 생성자를 추천하려면
클래스의 객체가 생성될 때 생성자(기본값)가 자동으로 호출된다는 것은 이미 알려져 있습니다. super 키워드를 사용하면 하위 클래스 생성자에서 상위 클래스 생성자를 명시적으로 호출할 수 있습니다. super가 서브클래스의 생성자 내부에서만 사용되고 그 내부의 첫 번째 명령문인지 확인하세요.
예:
코드:
class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } } //child class class B extends A { //constructor of child class B() { super(); System.out.println("I am Dileep Menon"); } }
Java의 Super 키워드 예
다음은 언급된 다양한 예입니다.
예시 #1
다음 프로그램에는 공통 변수 이름이 존재하며 super를 사용하여 상위 클래스의 변수를 호출합니다.
코드:
//Java program to illustrate Super keyword to refer instance variable //parent class class A { protected String name="ann"; } //child classs class B extends A { public String name="Anna";//variable which is same in parent class //sample method public void hello() { System.out.println("I am " + name); System.out.println("I am " + super.name); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class objb.hello();//call the method in child class } }
출력:
예시 #2
이 프로그램은 상위 클래스에서 동일한 메서드를 참조하면서 super 키워드를 시연하는 데 도움이 됩니다. 여기서 hello()는 두 클래스 모두에서 사용 가능한 메소드입니다.
코드:
//Java program to illustrate Super keyword to refer same method in parent class //parent class class A { protected String name="ann"; public void hello() { System.out.println("I am " + name); } } //child classs class B extends A { public String name="Anna";//variable which is same in parent class //sample method which is same in parent class public void hello() { System.out.println("I am " + name); } //method to call the hello() method in parent and child class public void test() { hello(); super.hello(); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class objb.test();//call the method in child class } }
출력:
예시 #3
이 프로그램은 super 키워드를 사용하여 상위 클래스의 생성자를 호출합니다.
코드:
//Java program to illustrate Super keyword to refer constructor in parent class //parent class class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } } //child class class B extends A { //constructor of child class B() { super(); System.out.println("I am Dileep Menon"); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class } }
출력:
예시 #4
이 프로그램은 상위 클래스의 매개변수화된 생성자를 참조하기 위해 super 키워드의 사용법을 보여줍니다.
코드:
//Java program to illustrate Super keyword to refer parameterised constructor in parent class //parent class class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } //parameterised constructor A(String name) { System.out.println("I am " + name); } } //child class class B extends A { //constructor of child class B() { super("Renuka"); System.out.println("I am Dileep Menon"); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class } }
출력:
결론
Super는 상위 클래스의 메소드나 함수, 인스턴스 변수 또는 속성과 생성자를 참조하는 데 사용되는 Java의 키워드입니다. 생성자가 선언되지 않으면 컴파일러는 자동으로 기본 생성자를 만듭니다. 마찬가지로 컴파일러는 super()가 선언되지 않은 경우 자동으로 호출합니다. 이 문서에서는 super 키워드의 여러 측면을 자세히 설명합니다.
위 내용은 Java의 슈퍼 키워드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Java 8 Stream foreach에서 나누거나 돌아 오시겠습니까?
