Java 中的 super()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3767365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
super() in Java
提问by Mohan
Is super()
used to call the parent constructor?
Please explain super()
.
是super()
用来调用父构造函数的吗?请解释super()
。
采纳答案by pakore
super()
calls the parent constructor with no arguments.
super()
不带参数调用父构造函数。
It can be used also with arguments. I.e. super(argument1)
and it will call the constructor that accepts 1 parameter of the type of argument1
(if exists).
它也可以与参数一起使用。即super(argument1)
,它将调用接受 1 个类型参数的构造函数argument1
(如果存在)。
Also it can be used to call methods from the parent. I.e. super.aMethod()
它还可以用于从父级调用方法。IEsuper.aMethod()
More info and tutorial here
更多信息和教程在这里
回答by Heinzi
Is super() is used to call the parent constructor?
super() 是用来调用父构造函数的吗?
Yes.
是的。
Pls explain about Super().
请解释一下 Super()。
super()
is a special use of the super
keyword where you call a parameterless parent constructor. In general, the super
keyword can be used to call overridden methods, access hidden fields or invoke a superclass's constructor.
super()
是super
关键字的特殊用途,您可以在其中调用无参数父构造函数。通常,super
关键字可用于调用覆盖方法、访问隐藏字段或调用超类的构造函数。
Here's the official tutorial
这是官方教程
回答by Bozho
Yes, super()
(lowercase) calls a constructor of the parent class. You can include arguments: super(foo, bar)
是的,super()
(小写)调用父类的构造函数。您可以包含参数:super(foo, bar)
There is also a super
keyword, that you can use in methods to invoke a method of the superclass
还有一个super
关键字,您可以在方法中使用它来调用超类的方法
A quick google for "Java super" results in this
快速谷歌在“Java的超级”结果这
回答by aioobe
Source article: Java: Calling super()
来源文章:Java:调用 super()
Yes. super(...)
will invoke the constructor of the super-class.
是的。super(...)
将调用超类的构造函数。
Illustration:
插图:
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();
}
}
Prints:
印刷:
Constructing an animal: From Dog constructor
Constructing a dog.
回答by Sagar V
That is correct. Super is used to call the parent constructor. So suppose you have a code block like so
那是正确的。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;
}
}
Then you can assign a value to the member variable n.
然后就可以给成员变量n赋值了。
回答by SimonGates
Some facts:
一些事实:
super()
is used to call the immediate parent.super()
can be used with instance members, i.e., instance variables and instance methods.super()
can be used within a constructor to call the constructor of the parent class.
super()
用于调用直接父级。super()
可以与实例成员一起使用,即实例变量和实例方法。super()
可以在构造函数中使用以调用父类的构造函数。
OK, now let's practically implement these points of super()
.
好的,现在让我们实际实现super()
.
Check out the difference between program 1 and 2. Here, program 2 proofs our first statement of super()
in Java.
查看程序 1 和 2 之间的区别。这里,程序 2 证明了我们super()
在 Java 中的第一条语句。
Program 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();
}
}
Output:
输出:
200
200
200
200
Now check out program 2 and try to figure out the main difference.
现在查看程序 2 并尝试找出主要区别。
Program 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();
}
}
Output:
输出:
100
200
100
200
In program 1, the output was only of the derived class. It couldn't print the variable of neither the base class nor the parent class. But in program 2, we used super()
with variable a
while printing its output, and instead of printing the value of variable a
of the derived class, it printed the value of variable a
of the base class. So it proves that super()
is used to call the immediate parent.
在程序 1 中,输出仅是派生类的。它无法打印基类和父类的变量。但是在程序2中,我们在打印输出时使用super()
了变量a
,而不是打印a
派生类的变量值,而是打印a
基类的变量值。所以它证明super()
是用来调用直接父级的。
OK, now check out the difference between program 3 and program 4.
好,现在看看程序3和程序4的区别。
Program 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();
}
}
Output:
输出:
200
200
Here the output is 200. When we called Show()
, the Show()
function of the derived class was called. But what should we do if we want to call the Show()
function of the parent class? Check out program 4 for the solution.
这里的输出是200。当我们调用时Show()
,Show()
派生类的函数就被调用了。但是如果我们要调用Show()
父类的函数怎么办呢?查看程序 4 以获取解决方案。
Program 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();
}
}
Output:
输出:
100
200
100
200
Here we are getting two outputs, 100 and 200. When the Show()
function of the derived class is invoked, it first calls the Show()
function of the parent class, because inside the Show()
function of the derived class, we called the Show()
function of the parent class by putting the super
keyword before the function name.
这里我们得到两个输出,100和200。当Show()
调用派生类的函数时,它首先调用Show()
父类的Show()
函数,因为在派生类的函数内部,我们Show()
通过把调用父类的函数super
函数名前的关键字。
回答by Olathe
Constructors
In a constructor, you can use it without a dot to call another constructor. super
calls a constructor in the superclass; this
calls a constructor in this class :
构造函数
在构造函数中,您可以不带点使用它来调用另一个构造函数。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
is useful if the superclass needs to initialize itself. this
is useful to allow you to write all the hard initialization code only once in one of the constructors and to call it from all the other, much easier-to-write constructors.
super
如果超类需要初始化自己,则很有用。this
对于允许您在一个构造函数中只编写一次所有硬初始化代码并从所有其他更容易编写的构造函数调用它是很有用的。
Methods
In any method, you can use it with a dot to call another method. super.method()
calls a method in the superclass; this.method()
calls a method in this class :
方法
在任何方法中,您都可以将它与点一起使用来调用另一个方法。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
is useful in a certain scenario: if your class has the same method as your superclass, Java will assume you want the one in your class; super
allows you to ask for the superclass's method instead. this
is useful only as a way to make your code more readable.
super
在某些情况下很有用:如果你的类和你的超类有相同的方法,Java 会假设你想要你的类中的方法;super
允许您改为请求超类的方法。this
仅作为一种使您的代码更具可读性的方法有用。
回答by Luchnik
The superkeyword can be used to call the superclass constructor and to refer to a member of the superclass
该超级关键字可以用于调用超类构造函数和来指代超类的一个成员
When you call super()with the right arguments, we actually call the constructor Box, which initializes variables width, heightand depth, referred to it by using the values of the corresponding parameters. You only remains to initialize its value added weight. If necessary, you can do now class variables Box as private. Put down in the fields of the Box class private modifier and make sure that you can access them without any problems.
当您使用正确的参数调用super()时,我们实际上调用了构造函数Box,它初始化了变量width、height和depth,并通过使用相应参数的值来引用它。您只需要初始化其增值权重。如有必要,您现在可以将类变量 Box 设为private。放下 Box 类私有修饰符的字段,并确保您可以毫无问题地访问它们。
At the superclass can be several overloaded versions constructors, so you can call the method super()with different parameters. The program will perform the constructor that matches the specified arguments.
在超类中可以有多个重载版本的构造函数,因此您可以使用不同的参数调用方法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;
}
}
回答by TheArchon
Just super(); alone will call the default constructor, if it exists of a class's superclass. But you must explicitly write the default constructor yourself. If you don't a Java will generate one for you with no implementations, save super(); , referring to the universal Superclass Object, and you can't call it in a subclass.
只是超级(); 如果类的超类存在,则单独调用默认构造函数。但是您必须自己显式地编写默认构造函数。如果你没有 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.
}
}
回答by Todd Zhang
For example, in selenium automation, you have a PageObject which can use its parent's constructor like this:
例如,在 selenium 自动化中,您有一个 PageObject 可以像这样使用其父级的构造函数:
public class DeveloperSteps extends ScenarioSteps {
public DeveloperSteps(Pages pages) {
super(pages);
}........