super()_Java中,super()在Java中

super()调用没有参数的父构造函数。

它也可以用于参数。 即super(argument1) ,它会调用接受参数1types的argument1 (如果存在)的构造函数。

也可以用来从父级调用方法。 ie super.aMethod()

更多信息和教程在这里

1. super()用于调用直接父对象。

2. super()可以用于实例成员,即实例variables和实例方法。

3.可以在构造函数中使用super()来调用父类的构造函数。

好吧,现在让我们来实践一下super()这个点。

检查程序1和2之间的区别。这里程序2certificate了我们在Java中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中,输出只是派生类。 它不能打印基类或父类的variables。 但是在程序2中,我们在打印输出时使用了带variablesa的super() ,而不是打印派生variablesa的值,而是打印出基类variablesa的值。 所以它certificate了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函数被调用。 但是如果我们想要调用父类的显示函数,我们该怎么做。 检查程序4的解决scheme。

计划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.当派生类的show函数被调用时,它首先调用父类的show函数,因为在派生类的show函数中,我们通过把super关键字调用父类的show函数之前的函数名称。

是。 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关键字可用于调用重写的方法,访问隐藏的字段或调用超类的构造函数。

这是官方教程

是的, super() (小写)调用父类的构造函数。 你可以包含参数: super(foo, bar)

还有一个super关键字,你可以在方法中用来调用超类的方法

一个“Java超级”的快速谷歌在这个结果

调用无参数超级构造函数只是浪费屏幕空间和程序员的时间。 无论您是否编写,编译器都会生成完全相同的代码。

class Explicit() { Explicit() { super(); } } class Implicit { Implicit() { } }

那是对的。 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; } }

然后你可以赋值给成员variablesn。

如果你的方法覆盖了它的一个超类的方法,你可以通过使用关键字super来调用被重写的方法。 你也可以使用super来引用一个隐藏的字段(虽然不鼓励隐藏字段)。 考虑这个类,超类:

public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } }

这里是一个名为Subclass的子类,它覆盖了printMethod():

public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }

在Subclass中,简单名称printMethod()是指在Subclass中声明的一个,它覆盖了Superclass中的一个。 因此,要引用从Superclassinheritance的printMethod(),Subclass必须使用限定名称,如图所示使用super。 编译和执行子类打印以下内容:

超级印刷。 在子类子类构造函数中打印

以下示例说明如何使用super关键字来调用超类的构造函数。 回想一下自行车的例子,MountainBike是自行车的一个子类。 这里是调用超类构造函数的MountainBike(子类)构造函数,然后添加自己的初始化代码:

public MountainBike(int startHeight, int startCadence,int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; }

调用超类的构造函数必须是子类构造函数的第一行。

调用超类构造函数的语法是

超();

或者:super(参数列表); 用super(),超类无参数的构造函数被调用。 用super(参数列表),调用具有匹配参数列表的超类构造函数。

刚超(); 如果它存在一个类的超类,单独将调用默认的构造函数。 但是你必须自己写一个默认的构造函数。 如果你没有一个Java会为你生成一个没有实现,保存super(); ,指的是通用的超类对象,你不能在子类中调用它。

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. } }

我已经看到了所有的答案。 但是每个人都忘了提一个非常重要的观点:

应该在构造函数的第一行调用或使用super()。

构造函数

在一个构造函数中,你可以使用它没有点来调用另一个构造函数。 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 ,它使用相应参数的值来初始化variableswidth , height和depth 。 你只需要初始化它的增值重量。 如果有必要,你现在可以把类variablesBox作为私有的 。 放在Box类私人修改器的领域,并确保您可以访问他们没有任何问题。

在超类可以是几个重载版本的构造函数,所以你可以用不同的参数调用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; } }

例如,在selenium自动化中,你有一个可以使用父类的构造函数的PageObject对象:

public class DeveloperSteps extends ScenarioSteps { public DeveloperSteps(Pages pages) { super(pages); }........

super是一个关键字。 它在子类方法定义中用于调用在超类中定义的方法。 超类的私有方法不能被调用。 super关键字只能调用public和protected方法。 它也被类构造函数用来调用其父类的构造函数。

检查这里进一步的解释。

java中超级关键字

超级关键字是用于引用父类对象的引用variables。

为什么在java中使用超级关键字?

每当将基类数据inheritance到派生类中就是机会得到歧义,因为可能是基类和派生类的数据相同所以对于这些数据需要使用超级关键字

超级关键字在方法级?

Super关键字也用于调用父类方法。 在方法覆盖的情况下应该使用超级关键字。

在方法级别使用超级关键字?

当基类方法名称和派生类方法名称具有相同的名称时使用Super关键字。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值