从 Java 中的子类构造函数调用超类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19326229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:08:24  来源:igfitidea点击:

Calling superclass from a subclass constructor in Java

javasubclasssuperclassaccessor

提问by user215732

I am trying to create a constructor that takes a field as a parameter, then puts it in a field that is stored in a superclass. Here is the code I am using

我正在尝试创建一个将字段作为参数的构造函数,然后将其放入存储在超类中的字段中。这是我正在使用的代码

public crisps(String flavour, int quantity) {
    this.flavour = super.getFlavour();
    this.quantity = quantity;
}

In the superclass I have initialised the field with

在超类中,我用

private String flavour;

and I have an accessor method

我有一个访问器方法

public String getFlavour() {
    return flavour;
}

I am getting an error "flavour has private access in the superclass", but I believe this shouldn't matter as I am calling the accessor method that returns it to the field?

我收到一个错误“风味在超类中具有私有访问权限”,但我相信这应该无关紧要,因为我正在调用将其返回到字段的访问器方法?

采纳答案by lpinto.eu

What you should do:

你应该做什么:

Add a constructor to your super class:

为您的超类添加一个构造函数:

public Superclass {
    public SuperClass(String flavour) {
       // super class constructor
       this.flavour = flavour;
    }
}

In the Crisps class:

在薯片类中:

public Crisps(String flavour, int quantity) {
    super(flavour); // send flavour to the super class constructor
    this.quantity = quantity;
}

 

 

Comments

注释

Some comments to your question:

对你的问题的一些评论:

"In the superclass I have initialised the field with "

“在超类中,我用”初始化了该字段

private String flavour;

This is not an initialization, it is a declaration. An initialization is when you set a value.

这不是初始化,而是声明。初始化是当您设置一个值时。

"I am getting an error " flavour has private access in the superclass" but I believe this shouldn't matter as I am calling the accessor method that returns it to the field?"

“我收到一个错误”风味在超类中具有私有访问权限,但我相信这应该无关紧要,因为我正在调用将其返回到字段的访问器方法?”

When you call a accessor (aka getter), it is ok - depends on the getter visibility. The problem in you code is the:

当您调用访问器(又名 getter)时,没问题 - 取决于 getter 的可见性。您代码中的问题是:

this.flavour = 

because flavour is not a field declared on Crisps class, but on the supper class, so you can't do a direct access like that. you should use my suggestion or declare a setter on the super class:

因为 flavor 不是在 Crisps 类上声明的字段,而是在 supper 类上声明的,所以你不能像那样直接访问。您应该使用我的建议或在超类上声明一个 setter :

public void setFlavour(String flavour) {
    this.flavour = flavour;
}

Then you can use it on the child class:

然后你可以在子类上使用它:

public Crisps(String flavour, int quantity) {
    this.quantity = quantity;
    super.setFlavour(flavour);
}

回答by Cruncher

flavouris private. Although you're reading it from the public method, you're assigning it to a private field, and you likely didn't declare it in this class.

flavour是私人的。尽管您是从公共方法读取它,但您将它分配给一个私有字段,并且您可能没有在此类中声明它。

You could set flavour to protectedin the parent class or define a setter for it

您可以protected在父类中设置风味或为它定义一个 setter

Ultimately your code doesn't really make sense though. Even if it did compile, it would be more or less: flavour = flavour. Perhaps you should rethink what you're trying to do a little bit

最终,您的代码并没有真正意义。即使它确实编译了,它也或多或少是:flavour = flavour。也许你应该重新考虑一下你想要做什么

I think you may need a tighter grasp on Java and Object Oriented Programming.

我认为您可能需要更深入地掌握 Java 和面向对象编程。

http://docs.oracle.com/javase/tutorial/java/concepts/

http://docs.oracle.com/javase/tutorial/java/concepts/

You should start here.

你应该从这里开始。

回答by HansB

public crisps(String flavour, int quantity)
{
    super(flavour);
    this.quantity = quantity;
}

This should work as see Docs

这应该像看文档一样工作

回答by Aida Isay

make

制作

    private String flavour;

public,otherwise your subclasses won't have access to this String. Your superclass doesn't know about existence of any subclass. According to Java documentation, "private" makes any variable and method available within that class,where private variable or method was declared, no any class has access to it,even subclasses. Once you chance your access modifier, you won't get any errors.

public,否则您的子类将无法访问此字符串。您的超类不知道任何子类的存在。根据 Java 文档,“私有”使该类中的任何变量和方法都可用,其中声明了私有变量或方法,没有任何类可以访问它,甚至子类。一旦您使用访问修饰符,您就不会收到任何错误。