簡體   English   中英

另一個類中的實例變量

[英]instance variable in another class

我正在做作業,我不會發布完整的代碼,但是我堅持的事情可能很簡單,我在書中找不到它,因此需要向正確的方向指出。 我正在使用類和接口。

基本上在我的主代碼中,我有這樣一行

    CheckingAccount checking = new CheckingAccount(1.0);  // $1 monthly fee

有人告訴我創建一個名為CheckingAccount的類,並告知該類“該類應包含一個實例變量,用於每月費用的初始化,該變量已初始化為傳遞給構造函數的值。

由於我是新手,所以這對我來說簡直不是英語,我假設這是要收取這1.00美元的費用,然后在CheckingAccount類中聲明它,以便我可以使用該變量創建一個方法來計算一些內容。

soooo ...我該怎么做? 我知道如何創建實例變量,就像

    public double  monthly fee =

但是那又怎樣呢? 否則我可能是錯的。 我真的在Java方面做得不好。 任何幫助表示贊賞。

我猜想另一種詢問方式是我是否只是將其聲明為1.0? 還是我“導入”該值,以防稍后它在某個時刻改變,而您不必在所有類中都通過代碼來更改它?

您的要求(如我所讀)是在構造函數中初始化實例變量,實例化( new CheckingAccount(1.0); )表明您處在正確的軌道上。

您的類將需要一個構造函數方法,該方法接收並設置該值1.0

// Instance var declaration
private double monthly_fee;

// Constructor receives a double as its only param and sets the member variable
public CheckingAccount(double initial_monthly_fee) {
  monthly_fee = inital_monthly_fee;
}

@傑里米:

您幾乎是正確的(至少,您被要求做的解釋與我的解釋相符); 雖然我不知道該類的實際設計,也不知道是否month_fee需要公開,但使用偽代碼,您將看到類似以下內容:

class CheckingAccount {  
    //Instance variable  
    double monthly_fee;    
    //Constructor
    CheckingAccount(double monthly_fee) {  
          this.monthly_fee = monthly_fee;  
    }    
    //Function to multiply instance variable by some multiplier
    //Arguments: Value to multiply the monthly fee by
    double multiply_fee(double a_multiplier) {  
          return monthly_fee*a_multiplier;  
    }  
}

你基本上是對的。 如果還沒有,則應該創建一個新類(它應該在自己的名為CheckingAccount的文件中),如下所示:

/** This is the class of Account with monthly fee. */
public class CheckingAccount {
    // This is the instance variable.
    // It should be 'private' for reasons you will surely learn soon.
    // And NOT static, since that would be a class variable, not an instance one.
    // The capitalization is called camelCase, google it up. Or, even better, find 'JavaBeans naming conventions'
    private double monthlyFee;

    // This is the constructor. It is called when you create the account.
    // It takes one parameter, the fee, which initializes our instance variable.
    // Keyword 'this' means 'this instance, this object'.
    public CheckingAccount(double monthlyFee) {
        this.monthlyFee = monthlyFee;
    }

    // Here will be your methods to calculate something...
}

不要將實例變量創建為public。 這是一種不好的做法,因為它違反了信息隱藏的原理(您的老師可能將其稱為“抽象”)。 相反,您可以創建一個實例變量為

public final class CheckingAccount {
    private double monthlyFee;

    // The rest of the class goes here

    public double getMonthlyFee() { // This method is called an accessor for monthlyFee
        return monthlyFee;
    }
}

請注意, monthly fee不是有效的變量名,因為它包含空格,變量名不能包含空格。 還要注意,其他類通過一種方法訪問monthlyFee 因為您定義方法而不是公開變量,所以您可以更好地控制對monthlyFee的訪問(另一個類不能只更改monthlyFee除非您定義進行此更改的方法)。

現在開始訪問monthlyFee 方法getMonthlyFee之所以被稱為訪問器是有原因的:它允許其他類訪問該變量。 因此,這些其他類可以僅調用該方法以從CheckingAccount獲取月費:

CheckingAccount checking = new CheckingAccount(1.0);
// A bunch of other code can go here
double fee = checking.getMonthlyFee(); // Now fee is 1.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM