super()用于调用父构造函数吗? 请解释super()。
当前回答
super关键字可用于调用超类构造函数和引用超类的成员
当您使用正确的参数调用super()时,实际上调用了构造函数Box,它通过使用相应参数的值来初始化变量width、height和depth。你只剩下初始化它的增值权重。如果有必要,现在可以将类变量Box设为私有。放在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()可以用于实例成员,即实例变量和实例方法。 可以在构造函数中使用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中,我们在输出变量a时使用了super(),它打印的不是派生类变量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()时,实际上调用了构造函数Box,它通过使用相应参数的值来初始化变量width、height和depth。你只剩下初始化它的增值权重。如果有必要,现在可以将类变量Box设为私有。放在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()是Super关键字的一种特殊用法,用于调用无参数的父构造函数。通常,super关键字可用于调用被重写的方法、访问隐藏字段或调用超类的构造函数。
下面是官方教程
我们可以用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中的空构造函数 类。
是的,super()(小写)调用父类的构造函数。可以包含参数:super(foo, bar)
还有一个超级关键字,您可以在方法中使用它来调用超类的方法
“Java super”的快速谷歌会导致这样的结果
推荐文章
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?