Java 如何在对象中引用对象属性

Java 如何在对象中引用对象属性,java,object,Java,Object,我有一个名为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 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;
}

假设iv'e已经在客户端数组“tab”(选项卡[0])的第一个插槽中设置了一个客户端。我想引用“chequing”compte中的“somme”。我尝试了
选项卡[0]。chequing.somme
,但似乎不起作用

在相对较新的Java中,如果这看起来超级愚蠢的xD,那么很抱歉


谢谢你的帮助

您没有使用这些类发布代码,也没有发布错误,因此我猜测:您将字段声明为private

了解访问控制修改器: 特别是靠近页面顶部的“访问级别”表


您将需要提供访问方法(getXXX/setXXX)或更改修饰符,这取决于您打算如何使用它们。

由于
chequing
somme
私有的
变量,因此调用它们的类将看不到它们。你有两个选择:

  • 编写
    getter
    setter
    方法,如:

    public-double-getSomme(){
    把这个还给我;
    }

  • 公开所有变量,尽管这不是一个好做法

在类中添加getter、setter,并通过getter
client.getTab()[0].getChequing().getSomme()访问属性


我发现您的代码存在两个问题:

  • 所有字段都是私有的,因此在定义它们的类之外不可访问
  • 类客户端中的字段也是静态的,这意味着它们在类的所有实例之间共享

  • 除此之外,类名通常以大写字母开头,但这当然只是一种惯例。

    “但这似乎不起作用。”--您遇到了什么错误?它告诉你什么?
    public void setSomme(double somme){
        this.somme = somme;
    }
    
    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;
        }
    }