简体   繁体   English

super(variableName)之间的区别; 和super.variableName

[英]Difference between super(variableName) ; and super.variableName

在此处输入图片说明 What is the Difference between super(variable-name); super(variable-name); and super.variableName = something; super.variableName = something; in a constructor, when you want to initialize the parameters and you wanna assign one of them to a variable of a parent class? 在构造函数中,当您想初始化参数并想将其中一个分配给父类的变量时?

for example i want to implement the constructor of the "Zahnradfraese" and it takes the parameter "int Kennung" and this parameter should be assigned to the attribute "kennung" of the parent class "Produktionmittel" 例如,我要实现“ Zahnradfraese”的构造函数,并且需要参数“ int Kennung”,并且应将此参数分配给父类“ Produktionmittel”的属性“ kennung”

Should I always use super when I wanna call a variable from this parent class or I just use it if I have another variable with the same name in the child class? 我想从该父类中调用一个变量时是否应该始终使用super,或者如果子类中有另一个具有相同名称的变量,我应该只使用它吗?

What is the difference between super(variableName); super(variableName);什么不一样super(variableName); and super.variableName = something; super.variableName = something; ?

method() (here, super(variableName) ) is a method invocation (here, a parent's constructor invocation). method() (此处为super(variableName) )是方法调用(此处为父级的构造函数调用)。

super.variableName = something; is an assignment to a parent's field. 是对父级字段的分配。

Should I always use super when I wanna call a variable from this parent class or I just use it if I have another variable with the same name in the child class? 我想从该父类中调用一个变量时是否应该始终使用super ,或者如果子类中有另一个具有相同名称的变量,我应该只使用它吗?

super(variableName) can initialise the inner state of the parent, particularly super.variableName . super(variableName)可以初始化父级的内部状态,尤其是super.variableName It is reasonable to initialise a super.variableName before accessing it. 在访问super.variableName之前初始化它是合理的。 Both ways you listed can be utilised for that. 您列出的两种方式均可用于此目的。 Just make sure there is no code duplication. 只要确保没有代码重复即可。

I want to implement the constructor of the Zahnradfraese and it takes the parameter int Kennung and this parameter should be assigned to the attribute kennung of the parent class Produktionmittel . 我想实现Zahnradfraese的构造函数,它采用参数int Kennung并且应该将此参数分配给父类Produktionmittel的属性kennung

Add a constructor to Produktionmittel which takes an int Produktionmittel添加一个构造函数,该构造函数需要一个int

public Produktionmittel(int i) {
    kennung = i;
}

and call it from the child: 并从孩子那里叫它:

public Zahnradfraese(int kennung) {
    super(kennung);
}

So super(variableName) is invoking your parent class one arg constructor, and that logic gets executes 所以super(variableName)调用您的父类一个arg构造函数,并且该逻辑得到执行

super.variableName = something; is assigning something value to parent class variable variableName 被分配something价值父类变量variableName

super() is a keyword which is used to call the constructor in the parent class and it must be called from inside the constructor of the child class. super()是一个关键字,用于在父类中调用构造函数,并且必须从子类的构造函数内部调用它。 Also it must be the first statement. 同样,它必须是第一个声明。

Where as super.s is used to set the variable s (which is declared in the parent class) from the child class and it doesn't have restrictions as above. 使用as super.s来设置子类的变量s (在父类中声明),并且没有上述限制。

See below example: 请参见以下示例:

class Test {
    int s;

    Test(int d) {
    }
}

class T extends Test {
    T() {
        super(8);
        int d = 99;
        super.s = 00;
    }

    void ss() {
        super.s = 99;
    }
}

super(arg) invokes the constructor of the super class, setting the variable just sets the variable. super(arg)调用超类的构造函数,设置变量只是设置变量。 (The constructor might contain more logic than just assigning a variable, which you bypass with the second way) (构造函数可能包含的逻辑不只是分配变量,还可以通过第二种方法绕过)

Simple example: 简单的例子:

public class P{
  protected String variable1;
  private boolean variableInitialized = false;

  public P (String s){
     this.variable1 = s;
     this.variableInitialized=true;
  }
}

public class C extends P{

}

calling super("x") within C will also set the boolean flag, as the parent class "might expect" it. C调用super("x")也会设置布尔标志,因为父类“可能期望”它。 Calling super.variable1="x" will not affect the boolean flag, and you can't change it, cause it's private. 调用super.variable1="x"不会影响布尔标志,并且您不能更改它,因为它是私有的。

As a rule of the thumb i'd say: If there is a dedicated constructor for a certain variable, it seems worth using it, unless you exactly want to override that implementation. 根据经验,我会说:如果某个变量有专用的构造函数,则似乎值得使用它,除非您确实想覆盖该实现。

super(variable_name) represents a constructor call and should be first line in the constructor. super(variable_name)表示构造函数调用,应为构造函数的第一行。 Whereas super.variableName = something; super.variableName = something; means you are assigning a value to the instance variable of the parent class from the child class using super which is used to refer parent class objects. 表示您正在使用用于引用父类对象的super从子类为父类的实例变量分配一个值。

Now in your case: as per given class-diagram the class Zahnradfraese has a constructor which takes int Kennung argument. 现在,根据您的情况:按照给定的类图, Zahnradfraese类具有一个构造函数,该构造函数采用int Kennung参数。 Also, kennung is the parent-class and has no constructor and instead it has method setKennung() . 此外, kennung是父类,没有构造函数,而是具有setKennung()方法。 So you can do super.setKennung(kennung) from inside the constructor of Zahnradfraese class. 因此,您可以从Zahnradfraese类的构造函数内部执行super.setKennung(kennung) You can also declare a constructor inside kennung but that would mean deviating from the class-diagram which has setter and getter methods and no constructor. 您也可以在kennung声明一个构造kennung但这将意味着偏离具有setter和getter方法且没有构造函数的类图。

public class Zahnradfraese extends Kennung{
  public Zahnradfraese(int kennung){
    super.setKennung(kennung);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM