Super 是一个关键字,用于调用超类中的函数或方法。这将在子类中定义。只能使用此关键字调用公共和受保护的方法。也就是说,私有方法和静态方法不能用this来调用。 java 中的 super 关键字也可以用于调用父类的构造函数。 super 关键字的语法、示例和更多详细信息将在以下部分中讨论。
语法
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
super.> or super([0 or more arguments]);
超级关键字在 Java 中如何工作?
正如已经提到的,super 可以在多种场合使用。
- 引用直接父类的实例变量。
- 引用直接父类的方法。
- 引用直接父类的构造函数。
1.引用直接父类的实例变量
如果父类和子类有相同的数据成员,可以使用Super关键字来访问父类的字段或数据成员。在这种情况下,Java 虚拟机可能会出现歧义。
示例:
代码:
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 中的超级关键字示例
以下是提到的不同示例:
示例#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 中的 Super 关键字的详细内容。更多信息请关注PHP中文网其他相关文章!

Java是平台独立的,因为其"一次编写,到处运行"的设计理念,依赖于Java虚拟机(JVM)和字节码。1)Java代码编译成字节码,由JVM解释或即时编译在本地运行。2)需要注意库依赖、性能差异和环境配置。3)使用标准库、跨平台测试和版本管理是确保平台独立性的最佳实践。

Java'splatFormIndenceIsnotsimple; itinvolvesComplexities.1)jvmCompatiblemustbeiblemustbeensurecensuredAcrospPlatForms.2)nativelibrariesandsycallsneedcarefulhandling.3)

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

thejvmistheruntimeenvorment forexecutingjavabytecode,Cocucialforjava的“ WriteOnce,RunanyWhere”能力

JavaremainsatopchoicefordevelopersduetoitsplatFormentence,对象与方向设计,强度,自动化的MememoryManagement和ComprechensivestAndArdArdArdLibrary

Java'splatFormIndependecemeansDeveloperScanWriteCeandeCeandOnanyDeviceWithouTrecompOlding.thisAcachivedThroughThroughTheroughThejavavirtualmachine(JVM),WhaterslatesbyTecodeDecodeOdeIntComenthendions,允许univerniverSaliversalComplatibilityAcrossplatss.allospplats.s.howevss.howev

要设置JVM,需按以下步骤进行:1)下载并安装JDK,2)设置环境变量,3)验证安装,4)设置IDE,5)测试运行程序。设置JVM不仅仅是让其工作,还包括优化内存分配、垃圾收集、性能调优和错误处理,以确保最佳运行效果。

toensurejavaplatFormIntence,lofterTheSeSteps:1)compileAndRunyOpplicationOnmultPlatFormSusiseDifferenToSandjvmversions.2)upureizeci/cdppipipelinelikeinkinslikejenkinsorgithikejenkinsorgithikejenkinsorgithikejenkinsorgithike forautomatecross-plateftestesteftestesting.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。