Java 从重载类方法调用super()是否使用非参数化方法中的变量?

Java 从重载类方法调用super()是否使用非参数化方法中的变量?,java,Java,经过研究,我发现当子类扩展父类时,super()是有益的 但是如果我有一个带有两个方法的Foo.class private variable variable; private String variable2; public Foo() { this.variable = new variable(); } 及 在使用super() 只是想澄清我的问题。。有人能解释一下这段代码中发生了什么吗 public ProductDimensions() { } public ProductD

经过研究,我发现当子类扩展父类时,
super()
是有益的

但是如果我有一个带有两个方法的
Foo.class

private variable variable;
private String variable2;

public Foo()
{
   this.variable = new variable();
}

在使用
super()

只是想澄清我的问题。。有人能解释一下这段代码中发生了什么吗

public ProductDimensions() { }
public ProductDimensions(String sku, String size, double width, double depth, double height, double weight) {
    super();
    this.sku = sku;
    this.size = size;
    this.width = width;
    this.height = height;
    this.depth = depth;
    this.weight = weight;
}
当我的类没有扩展任何内容时,为什么要调用super?它只是没用吗?

构造函数中的super()总是调用基类中匹配的构造函数。在本例中,可能是
java.lang.Object
。因此,在代码中,这两个构造函数没有关系,只是它们在同一个类中

如果要在同一类中链接构造函数,必须使用
this()
(或必要的参数)。

super()
构造函数中的构造函数始终调用基类中匹配的构造函数。在本例中,可能是
java.lang.Object
。因此,在代码中,这两个构造函数没有关系,只是它们在同一个类中

如果要在同一类中链接构造函数,必须使用
this()
(或必要的参数)。

super()用于传递参数父类的构造函数和匹配参数。 即使您没有放置一个,编译器也会将它添加到构造函数的第一行

在构造函数链接的情况下,即使用this()匹配参数从另一个构造函数调用一个构造函数

在这种情况下,必须至少有一个构造函数调用super()

构造函数链接示例:

//Execute multiple constructors in chain
//Advantage: Allocation of multiple type of resources at initialization level. One constructor per resource example: GUI, Database, Network etc.
//Easy Debugging
//We use this() for constructor chaining.
//Can be achieved in any order.
class ConstructorChaining
{
ConstructorChaining()
{
this(10);//always the first line
System.out.println("Default Constructor Completed");
}
ConstructorChaining(int x)
{
this(x,20);//always the first line
System.out.println("X="+x);
System.out.println("Parameter 1 Constructor Completed");
}
ConstructorChaining(int x, int y)
{
// atleast one constructor without this() must be used. - here either you can write super() or compiler will add it for you.
System.out.println("X+Y="+(x+y));
System.out.println("Parameter 2 Constructor Completed");
}
public static void main(String... s)
{
new ConstructorChaining();
}
}
super()用于传递参数父类的构造函数和匹配参数。 即使您没有放置一个,编译器也会将它添加到构造函数的第一行

在构造函数链接的情况下,即使用this()匹配参数从另一个构造函数调用一个构造函数

在这种情况下,必须至少有一个构造函数调用super()

构造函数链接示例:

//Execute multiple constructors in chain
//Advantage: Allocation of multiple type of resources at initialization level. One constructor per resource example: GUI, Database, Network etc.
//Easy Debugging
//We use this() for constructor chaining.
//Can be achieved in any order.
class ConstructorChaining
{
ConstructorChaining()
{
this(10);//always the first line
System.out.println("Default Constructor Completed");
}
ConstructorChaining(int x)
{
this(x,20);//always the first line
System.out.println("X="+x);
System.out.println("Parameter 1 Constructor Completed");
}
ConstructorChaining(int x, int y)
{
// atleast one constructor without this() must be used. - here either you can write super() or compiler will add it for you.
System.out.println("X+Y="+(x+y));
System.out.println("Parameter 2 Constructor Completed");
}
public static void main(String... s)
{
new ConstructorChaining();
}
}

不,
Foo(String)
构造函数只是直接链接到超类构造函数。如果要链接到同一类中的另一个构造函数,请使用
this()
而不是
super()
。因此,只需使用
this()
切换
super()
即可调用这两个构造函数?将调用两个构造函数,从而调用超级构造函数(在第一个构造函数中调用
super()
)(String)构造函数直接链接到超类构造函数。如果要链接到同一类中的另一个构造函数,请使用
this()
而不是
super()
。因此只需使用
this()切换
super()
将同时调用这两个构造函数?将同时调用这两个构造函数,因此也将调用超级构造函数(在第一个构造函数中调用
super()
)是否必须调用super()?如果构造函数没有调用this(),编译器将添加super()作为构造函数的第一行。因此,每个构造函数的第一行始终是this()或super()。是否必须调用super()?即使省略,它不是隐式调用吗?如果构造函数不调用this(),编译器将添加super()作为构造函数的第一行。因此,每个构造函数的第一行总是this()或super()我应该删除这个问题,因为它被否决了吗?既然没有任何评论提示为什么这个问题应该被否决,我会说保持原样。我应该删除这个问题,因为它被否决了吗?既然没有任何评论提示为什么这个问题应该被否决,我会说保持原样。