Java 如何在不使用访问器的情况下访问超类中的私有变量?
假设您创建了一个新的PricedApt类,它是从RentalApt类派生的(因此它是从派生类派生的)。它增加了一个新的双重属性,price,它告诉我们公寓的月租金是多少。下面是该类的构造函数调用:Java 如何在不使用访问器的情况下访问超类中的私有变量?,java,inheritance,Java,Inheritance,假设您创建了一个新的PricedApt类,它是从RentalApt类派生的(因此它是从派生类派生的)。它增加了一个新的双重属性,price,它告诉我们公寓的月租金是多少。下面是该类的构造函数调用: public class RentalApt extends Apartment{ private String tenant; private boolean rented; public RentalApt(String owner, int size, boolean rented,
public class RentalApt extends Apartment{
private String tenant;
private boolean rented;
public RentalApt(String owner, int size, boolean rented, String who){
super(owner,size);
tenant = who;
this.rented = rented;
}
public boolean getRented(){
return rented;
}
public String getTenant(){
return tenant;
}
public void setRented(boolean isRented){
rented = isRented;
}
public void setTenant(String who){
tenant= who;
}
public String toString(){
String s = super.toString();
return (s + " occupied? " + rented + " tenant: " + tenant);
}
}
适当地使用super,为类编写一个toString方法,该方法除了说明jill作为所有者和jack作为租户之外,还向toString输出中添加短语“price:+price”。(注意:PricedApt中对toString的超级调用将使用其基类RentalApt中的toString方法
PricedApt p = new PricedApt("jill", 900, true, "jack", 1050.00);
所以我知道我需要在RentalApt中回收toString()方法,但是我在Renated和tenant中遇到了一个错误,因为它们是私有的。我已经尝试了我所知道的一切,但我还没有成功克服这个错误。到目前为止,我得到的是:
public class PricedApt extends RentalApt {
private double price;
public String toString() {
//code
我尝试了一些使用关键字super的方法,但没有成功。很抱歉,这个问题:我知道以前已经回答过了,但我从过去的答案中看到的任何东西都没有解决我的基本问题。如果超类没有为私有字段提供访问器方法,这就强烈表明您不应该尝试访问在子类中访问它 如果这些都是您可以更改的类,那么正确的解决方案可能是向超类添加访问器方法。如果您不希望该字段在公共API中公开,请将其设置为受保护的或包私有的 或者更改子类,使其不需要访问子类的超类
private
字段。(对于toString
方法,通常可以嵌入超类toString
的结果。这取决于您的格式要求等。)
但是,如果您想忽略该建议,可以使用反射来访问它。有关详细信息,请参阅链接的Q&A。如果超类没有为私有字段提供访问器方法,这强烈表明您不应该尝试在子类中访问它 如果这些都是您可以更改的类,那么正确的解决方案可能是向超类添加访问器方法。如果您不希望该字段在公共API中公开,请将其设置为受保护的或包私有的 或者更改子类,使其不需要访问子类的超类
private
字段。(对于toString
方法,通常可以嵌入超类toString
的结果。这取决于您的格式要求等。)
但是,如果您想忽略该建议,可以使用反射来访问它。有关详细信息,请参阅链接的Q&A。如果超类没有为私有字段提供访问器方法,这强烈表明您不应该尝试在子类中访问它 如果这些都是您可以更改的类,那么正确的解决方案可能是向超类添加访问器方法。如果您不希望该字段在公共API中公开,请将其设置为受保护的或包私有的 或者更改子类,使其不需要访问子类的超类
private
字段。(对于toString
方法,通常可以嵌入超类toString
的结果。这取决于您的格式要求等。)
但是,如果您想忽略该建议,可以使用反射来访问它。有关详细信息,请参阅链接的Q&A。如果超类没有为私有字段提供访问器方法,这强烈表明您不应该尝试在子类中访问它 如果这些都是您可以更改的类,那么正确的解决方案可能是向超类添加访问器方法。如果您不希望该字段在公共API中公开,请将其设置为受保护的或包私有的 或者更改子类,使其不需要访问子类的超类
private
字段。(对于toString
方法,通常可以嵌入超类toString
的结果。这取决于您的格式要求等。)
但是,如果您想忽略该建议,可以使用反射访问它。有关详细信息,请参阅链接的问答。PricedApt.toString()
应仅参考price
和RentalApt.toString()
您走对了,只需从PricedApt.toString()
PricedApt.toString()
中删除rented
和RentalApt.toString()
就可以了
您走对了,只需从PricedApt.toString()
PricedApt.toString()
中删除rented
和RentalApt.toString()
就可以了
您走对了,只需从PricedApt.toString()
PricedApt.toString()
中删除rented
和RentalApt.toString()
就可以了
您走对了,只需从
PricedApt.toString()中删除rendered
和tenant
您不必重复PricedApt的toString中的rendered和tenant字段,因为它们已经在super.toString返回的字符串中。只需将它们删除。您几乎就可以得到它。将其他字段保留到super类中,只需添加新内容。`return(s+“price:+price);这是一个很好的继承。您不必重复PricedApt的toString中的rendered和tenant字段,因为它们已经在super.toString返回的字符串中。只需将它们忽略即可。您几乎可以
public String toString() {
String s = super.toString();
return (s + " occupied? " + rented + " tenant: " + tenant+ " price: " + price);
}