java 将参数传递给超类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16868664/
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
Passing arguments to superclass
提问by Rrr
Hey guy's i'm trying to use a constructor to accept a ton of variables and then pass the relevant information to the superclass constructor.
嘿家伙,我正在尝试使用构造函数来接受大量变量,然后将相关信息传递给超类构造函数。
The error im getting is that when i use this.variable it's telling me to create that variable in the class, but i thought calling the super would allow me to use it this way.
我得到的错误是,当我使用 this.variable 时,它告诉我在类中创建该变量,但我认为调用 super 将允许我以这种方式使用它。
public class AuctionSale extends PropertySale {
private String highestBidder;
public AuctionSale(String saleID, String propertyAddress, int reservePrice, String highestBidder) {
super(saleID, propertyAddress, reservePrice);
this.saleID = saleID;
this.propertyAddress = propertyAddress;
this.reservePrice = reservePrice;
this.highestBidder = "NO BIDS PLACED";
}
As you can see i've called the superclass propertysale to get the variables.
如您所见,我已调用超类 propertysale 来获取变量。
Superclass -
超类——
public class PropertySale {
// Instance Variables
private String saleID;
private String propertyAddress;
private int reservePrice;
private int currentOffer;
private boolean saleStatus = true;
public PropertySale(String saleID, String propertyAddress, int reservePrice) {
this.saleID = saleID;
this.propertyAddress = propertyAddress;
this.reservePrice = reservePrice;
}
There is a lot more additional constructors but i believe they are irrelevant right now.
还有更多额外的构造函数,但我相信它们现在无关紧要。
回答by dasblinkenlight
The reason that you get the error is because the following variables have private
access in the PropertySale
class:
出现错误的原因是以下变量private
在PropertySale
类中具有访问权限:
saleID
propertyAddress
reservePrice
You cannot access them in the subclass AuctionSale
unless the superclass declares them protected
or public
. However, in this case it is not necessary: you pass these three variables to the super
constructor, so they get set in the base class. All you need in the constructor of the derived class is to call super
, and then deal with variables that the derived class has declared, like this:
您不能在子类中访问它们,AuctionSale
除非超类声明它们protected
或public
. 但是,在这种情况下没有必要:您将这三个变量传递给super
构造函数,以便在基类中设置它们。你只需要在派生类的构造函数中调用super
,然后处理派生类已经声明的变量,就像这样:
public AuctionSale(String saleID, String propertyAddress, int reservePrice, String highestBidder) {
super(saleID, propertyAddress, reservePrice);
this.highestBidder = "NO BIDS PLACED";
}
回答by pinkpanther
The private variables can only be accessed in the class where they are declared, no where else. Protected or public variables can be accessed in subclass.
私有变量只能在声明它们的类中访问,不能在其他地方访问。可以在子类中访问受保护或公共变量。
In any way what's the use of passing the variables of the class to its own constructor?
以任何方式将类的变量传递给它自己的构造函数有什么用?
Your saleID
,propertyAddress
,reservePrice
are all private variables in super class. That restricts the usage.
你saleID
,propertyAddress
,reservePrice
是在超类中的所有私有变量。这限制了使用。
You are however setting the variables through superclass' constructor so you do not have to set it yourself....
但是,您正在通过超类的构造函数设置变量,因此您不必自己设置......
public class AuctionSale extends PropertySale {
private String highestBidder;
public AuctionSale(String saleID, String propertyAddress, int reservePrice, String highestBidder) {
super(saleID, propertyAddress, reservePrice);//This should be sufficient
//this.saleID = saleID;
//this.propertyAddress = propertyAddress;
//this.reservePrice = reservePrice;
this.highestBidder = "NO BIDS PLACED";
}
If you want to access private variables, best practice is to write getter
and setter
methods in super class and use them wherever you want.
如果你想访问私有变量,最好的做法是在超类中编写getter
和setter
方法,并在任何你想要的地方使用它们。
回答by Sanjaya Liyanage
You have marked the variables in super class as private which means they won't be inherited.mark them as public,default,or protected and test.Private fields are only visible in the class itself.
您已将超类中的变量标记为私有,这意味着它们不会被继承。将它们标记为公共、默认或受保护和测试。私有字段仅在类本身中可见。