2024-12-14 08:00:05

Java中的super()

super()用于调用父构造函数吗? 请解释super()。


当前回答

还有其他一些用途。

引用继承接口的默认方法:

import java.util.Collection;
import java.util.stream.Stream;

public interface SkipFirstCollection<E> extends Collection<E> {

    @Override
    default Stream<E> stream() {
        return Collection.super.stream().skip(1);
    }
}

还有一种很少使用的情况,在实例化静态子类时,使用限定的超类为超类构造函数提供一个外部实例:

public class OuterInstance {

    public static class ClassA {

        final String name;

        public ClassA(String name) {
            this.name = name;
        }

        public class ClassB {
            public String getAName() {
                return ClassA.this.name;
            }
        }
    }

    public static class ClassC extends ClassA.ClassB {
        public ClassC(ClassA a) {
            a.super();
        }
    }

    public static void main(String[] args) {
        final ClassA a = new ClassA("jeff");
        final ClassC c = new ClassC(a);
        System.out.println(c.getAName());
    }
}

然后:

$ javac OuterInstance.java && java OuterInstance
jeff

其他回答

我愿意与代码分享我所了解的一切。

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

super with的使用方法:

/* 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()是Super关键字的一种特殊用法,用于调用无参数的父构造函数。通常,super关键字可用于调用被重写的方法、访问隐藏字段或调用超类的构造函数。

下面是官方教程

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

还有一个超级关键字,您可以在方法中使用它来调用超类的方法

“Java super”的快速谷歌会导致这样的结果

Super()不带参数地调用父构造函数。

它也可以与参数一起使用。例如super(argument1),它将调用接受一个argument1类型形参的构造函数(如果存在的话)。

它还可以用于从父类调用方法。即super.aMethod ()

更多信息和教程在这里

我们可以用SUPER做什么?

访问超类成员

如果你的方法覆盖了它的一些超类的方法,你可以通过使用关键字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();
    }
}

When creating an object,the JVM always first execute the constructor in the class of the top layer in the inheritance tree.And then all the way down the inheritance tree.The reason why this is possible to happen is that the Java compiler automatically inserts a call to the no-argument constructor of the superclass.If there's no non-argument constructor in the superclass and the subclass doesn't explicitly say which of the constructor is to be executed in the superclass,you'll get a compile-time error.

类的构造函数,在上面的代码中,如果我们想成功地创建Wolf对象 类必须执行。在这个过程中,Animal中的双参数构造函数 类被调用。同时,它显式地调用同一个参数中的单参数构造函数 类和单参数构造函数隐式调用生物中的非参数构造函数 类和非参数构造函数再次隐式调用Object中的空构造函数 类。