【发布时间】:2011-04-15 14:32:49
【问题描述】:
super()是用来调用父构造函数的吗?
请解释super()。
【问题讨论】:
-
必须更具体或只是阅读文档...
-
与 super() 调用有关,请检查此答案。 stackoverflow.com/questions/34349164/…
super()是用来调用父构造函数的吗?
请解释super()。
【问题讨论】:
super() 不带参数调用父构造函数。
它也可以与参数一起使用。 IE。 super(argument1),它将调用接受argument1(如果存在)类型的1个参数的构造函数。
它也可以用来调用父级的方法。 IE。 super.aMethod()
更多信息和教程here
【讨论】:
super(...) 只能用作构造函数中的第一条语句。
一些事实:
super() 用于调用直接父级。super() 可用于实例成员,即实例变量和实例方法。super()可以在构造函数中调用父类的构造函数。好的,现在让我们实际实现super()的这些点。
查看程序 1 和 2 之间的区别。这里,程序 2 证明了我们在 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 中,仅输出派生类。它无法打印基类和父类的变量。但是在程序 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。当调用派生类的Show()函数时,它首先调用父类的Show()函数,因为在派生类的Show()函数内部类,我们通过在函数名前加上super关键字来调用父类的Show()函数。
【讨论】:
super() 不是关键字。这是一个构造函数调用。 super 是一个关键字,#1 和 #2 仅在该定义下才有意义。
是的。 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(arg1),它将调用 1-argument 构造函数,依此类推。
super() 将不是有效调用。
super() 是用来调用父构造函数的吗?
是的。
请解释一下 Super()。
super() 是 super 关键字的特殊用法,您可以在其中调用无参数的父构造函数。一般来说,super 关键字可用于调用被覆盖的方法、访问隐藏字段或调用超类的构造函数。
【讨论】:
super() 用于调用父构造函数,super.myMethod() 用于调用被覆盖的方法。
调用无参数超级构造函数只是浪费屏幕空间和程序员时间。无论您是否编写,编译器都会生成完全相同的代码。
class Explicit() {
Explicit() {
super();
}
}
class Implicit {
Implicit() {
}
}
【讨论】:
是的,super()(小写)调用父类的构造函数。您可以包含参数:super(foo, bar)
还有一个super关键字,你可以在方法中使用它来调用超类的方法
“Java super”的快速谷歌搜索结果为this
【讨论】:
没错。 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 会为你生成一个没有实现的,保存 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.
}
}
【讨论】:
例如,在 selenium 自动化中,您有一个 PageObject 可以像这样使用其父级的构造函数:
public class DeveloperSteps extends ScenarioSteps {
public DeveloperSteps(Pages pages) {
super(pages);
}........
【讨论】:
我想与代码分享我理解的任何内容。
java中的super关键字是一个引用变量,用来引用父类对象。它主要用于以下情况:-
1.使用 super 和变量:
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
/* 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.在构造函数中使用 super:
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 来调用被覆盖的方法,例如super.methodName();
如果构造函数没有显式调用超类构造函数,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 对象, 必须执行类。在此过程中,Animal 中的两个参数构造函数 类被调用。同时,它显式调用同一个参数的构造函数 类和单参数构造函数隐式调用 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,它初始化变量 width、height和depth,通过使用值来引用它的相应参数。你只剩下初始化它的增值权重了。如有必要,您现在可以将 Box 类变量设置为 private。将 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;
}
}
【讨论】:
super 是一个关键字。它在子类方法定义中用于调用超类中定义的方法。不能调用超类的私有方法。 super 关键字只能调用公共和受保护的方法。类构造函数也使用它来调用其父类的构造函数。
查看here 以获得进一步解释。
【讨论】:
如上所述,在默认构造函数中,在构造函数的第一行调用了一个隐式 super()。
这个 super() 自动调用从类层次结构顶部开始的构造函数链,然后向下移动。
如果程序的类层次结构中有两个以上的类,则顶级类默认构造函数将被调用首先。
这是一个例子:
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() 可用于调用直接父类构造函数。
【讨论】: