我有一个名为compte
的小组:
public class compte {
private double somme;
private double limit;
private double withdrawn;
public compte(double amt, double lmt, double wdr){
somme = amt;
limit = lmt;
withdrawn = wdr;
}
}
我的客户中有两个' compte'对象:
public class client {
private static String nom;
private static String prenom;
private static String adresse;
private static compte chequing;
private static compte savings;
private static client[] tab;
private static int pin;
private static String nomfich;
private static int accountNum;
private static int forVal;
public client(String adr, String nomF, String prn, compte ch, compte sav, int nip, int accNum){
adresse = adr;
nom = nomF;
prenom = prn;
chequing = ch;
savings = sav;
pin = nip;
accountNum =accNum;
}
我们假设已经在客户端阵列的第一个插槽中设置了一个客户端'标签'(tab[0]
)。我想参考一下' somme'在' chequing'孔特。我试过了tab[0].chequing.somme
,但这似乎不起作用。
相对较新的Java,如果这看起来非常愚蠢xD,那就很抱歉。
谢谢你的帮助!
答案 0 :(得分:0)
您没有使用这些类发布您的代码,并且您没有发布错误,所以这是我的猜测:您将字段声明为私有。
了解访问控制修饰符: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html 特别是,页面顶部附近的“访问级别”表。
您需要提供访问方法(getXXX / setXXX)或更改修饰符,具体取决于您打算如何使用它们。
答案 1 :(得分:0)
由于chequing
和somme
是private
个变量,因此您调用它们的类不会显示它们。你有两个选择:
撰写getter
和setter
方法,例如:
public double getSomme(){
return this.somme;
}
和
public void setSomme(double somme){
this.somme = somme;
}
答案 2 :(得分:0)
在类中添加getter,setter,并通过getter client.getTab()[0].getChequing().getSomme();
public class compte {
private double somme;
private double limit;
private double withdrawn;
public compte(double amt, double lmt, double wdr) {
somme = amt;
limit = lmt;
withdrawn = wdr;
}
public double getSomme() {
return somme;
}
public void setSomme(double somme) {
this.somme = somme;
}
public double getLimit() {
return limit;
}
public void setLimit(double limit) {
this.limit = limit;
}
public double getWithdrawn() {
return withdrawn;
}
public void setWithdrawn(double withdrawn) {
this.withdrawn = withdrawn;
}
}
public class client {
private String nom;
private String prenom;
private String adresse;
private compte chequing;
private compte savings;
private client[] tab;
private int pin;
private String nomfich;
private int accountNum;
private int forVal;
public client(String adr, String nomF, String prn, compte ch, compte sav, int nip, int accNum) {
adresse = adr;
nom = nomF;
prenom = prn;
chequing = ch;
savings = sav;
pin = nip;
accountNum = accNum;
}
public client() {
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public compte getChequing() {
return chequing;
}
public void setChequing(compte chequing) {
this.chequing = chequing;
}
public compte getSavings() {
return savings;
}
public void setSavings(compte savings) {
this.savings = savings;
}
public client[] getTab() {
return tab;
}
public void setTab(client[] tab) {
this.tab = tab;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public String getNomfich() {
return nomfich;
}
public void setNomfich(String nomfich) {
this.nomfich = nomfich;
}
public int getAccountNum() {
return accountNum;
}
public void setAccountNum(int accountNum) {
this.accountNum = accountNum;
}
public int getForVal() {
return forVal;
}
public void setForVal(int forVal) {
this.forVal = forVal;
}
}
答案 3 :(得分:0)
我发现您的代码有两个问题:
除此之外,课程名称通常以大写字母开头,但这当然只是一种惯例。