2017-02-01 18 views
0

內的對象屬性:如何引用我有一個類名爲<code>compte</code>對象

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 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; 
} 

假設iv'e已經在客戶端陣列「選項卡」(tab[0])的第一個插槽中設置了一個客戶端。我想參考'chequing'compte中的'somme'。我試過tab[0].chequing.somme,但這似乎不起作用。

在Java相對較新,所以很抱歉,如果這看起來超級愚蠢的xD。

感謝您的幫助!

+0

「但這似乎不起作用。」 - 你得到的錯誤是什麼?它告訴你什麼? –

回答

0

你沒有發佈你的代碼使用這些類,你沒有發佈錯誤,所以這裏是我的猜測:你宣佈這些字段是私人的。

閱讀有關訪問控制修飾符: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html 特別是,頁面頂部附近的「訪問級別」表。

您需要提供訪問方法(getXXX/setXXX)或更改修飾符,這取決於您打算如何使用它們。

0

由於chequingsommeprivate變量,它們對您調用它們的類不可見。你有兩個選擇:

  • gettersetter方法,如:

    public double getSomme(){ return this.somme; }

public void setSomme(double somme){ 
    this.somme = somme; 
} 
  • 讓所有的變量公衆,雖然它不是一個 良好的做法。
0

通過吸氣添加getter,setter方法在你的類和訪問屬性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; 
    } 
} 
0

我看到兩個問題與您的代碼:

  1. 所有字段是私有的,因此從外部不可訪問它們被定義的類,
  2. 類客戶端中的字段也是靜態的,這意味着它們在類的所有實例之間共享。

除此之外,類名通常以大寫字母開頭,但這當然只是一個約定。

相關問題