-1
我想從另一個類中抽象類(MainAbstract.java)中訪問和使用3個私有變量,該類已經從之前擴展(SubAbstract.java)提到的抽象類。將值插入抽象類中的非抽象方法使用子非抽象類
從子類我想訪問主類的getters()和setters()。
在主類(這是一個抽象類)中有一個名爲ShowInfo()的抽象方法。
這個ShowInfo()抽象方法應該做一些事情來查看子類的每個實例。
以下是MainClass(摘要)和Sub Class SubAbstract的源代碼。請參考他們。
MainAbstract.java
package abstractionexample;
public abstract class MainAbstract {
private String sName;
private String sType;
private int iQty;
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsType() {
return sType;
}
public void setsType(String sType) {
this.sType = sType;
}
public int getiQty() {
return iQty;
}
public void setiQty(int iQty) {
this.iQty = iQty;
}
public abstract void showInfo();
public static void main(String[] args) {
}
}
SubAbstract.java
package abstractionexample;
public class SubAbstract extends MainAbstract{
@Override
public void showInfo() {
}
//This is an instance and the getters() and setters() should use each instance of this kind of to get values and set values.
SubAbstract nSubAbs = new SubAbstract();
}
我不確定你的問題是什麼。但是你不能訪問包括子類在內的任何外部類的'private'成員。 –
你是對的!這就是爲什麼我使用getters()和setters()。我正試圖找到一個可能性來做到這一點。 – ChiranthakaJ
只要你的showInfo()方法保持爲空,當然什麼都不顯示。你能指望什麼? – Heri