This article we discusses about Java Method and Class. I will use a Java class to be the example.

这篇文章我们说的是Java方法与类。我将使用一个Java类来作为例子。

 

public class Bank {

private String myPassword;

private double myBalance;

Here we write two variables myPassword and myBalance but did not define them, these are the attributes for this Bank class.

这里我们写了两个变量 myPasswordmyBalance,但没有赋予它们值,这些是这个 Bank 类的属性。

 

public Bank() {

myPassword = “”;

myBalance = 0;

}

Here we write a default constructor Bank() to defines the two variables. A constructor enables you to provide any custom initialization that must be done before any other methods can be called. Default means the constructor has no parameters.

这里我们写了一个默认构造函数 Bank() 来定义这两个变量。构造函数使你能够提供任何自定义的初始化,这些初始化必须在任何其他方法被调用之前完成。 默认意味着构造函数没有参数。

 

public Bank(String password, double balance) {

myPassword = password;

myBalance = balance;

}

Compare to above, this constructor encompasses two parameters and allows the user to modify.

与上面相比,这个构造函数包含了两个参数,并允许用户进行修改。

 

public String getPassword() {

return myPassword;

}

Right here, we have the first method getPassword. This method is intended to get the password for an account, we called it “Accessor”, because it is trying to access to a attribute (variable). Notice that if there is a data type beside the method name, there is always a return statement in its code.

就在这里,我们有了第一个方法 getPassword。这个方法的目的是获取一个账户的密码,我们称它为“访问器(Accessor)”,因为它要访问一个属性(变量)。请注意,凡是方法名称旁边有数据类型,那么这个方法的代码块里总是有一个 return 语句。

 

public double getBalance() {

return myBalance;

}

Same as above, this method is also a accessor, but this one is intended to return the attribte myBalance.

和上面一样,这个方法也是一个访问器(Accessor),但这个方法的目的是返回变量 myBalance

 

public void setPassword(String password) {

myPassword = password;

}

This method is different than the one above, it is called a “Modifier”, because it is intended to modify the attribute. Notice that there are no data type but “void” beside the name and there is no return statment in the code, this is because we don’t need to return anything. Therefore we write void to represent no return. Additionally, there is a parameter in the parentheses, this means the user need to input a value in order to modify the attribute.

这个方法与上面的方法不同,它被称为 “修改器(Modifier)”,因为它是用来修改变量的。请注意,在代码中没有数据类型,只有 “void”,也没有 return 语句,这是因为我们不需要返回什么。因此我们用 void 来表示不返回。此外,括号里有参数,这意味着用户需要输入一个值来修改属性。

 

public void deposit(double amount) {

myBalance += amount;

}

Same as the one above, this one is a modifyer. This method is adding up myBalance and the number user inputed.

和上面那个一样,这个是一个修改器(Modifier)。这个方法是将 myBalance 和用户输入的数字相加。

 

public void withdraw(double amount) {

myBalance -= amount;

}

Similar to the above, but this modifier is subtracting the number entered by the user from myBalaence.

与上方相似,但这个修改器(Modifier)是在用 myBalaence 减去用户输入的数字。

 

public String toString() {

return “Bank Account: $” + myBalance;

}

}

toString() is a built-in Java method. So, why we still write it again at here? This is because the defult return is not the way we want it to be. So, we rewrite it at here. We make it to return myBalance.

toString() 是一个内置的 Java 方法。那么,为什么我们还要在这里再写一遍呢?这是因为默认的返回值不是我们想要的方式。所以,我们在这里重写它。我们把它改成返回myBalance

If you are having difficulties learning or writing Java, feel free to email 22aye@ncpachina.org or 2240400@ncpachina.org (<— This guy is the author).

如果您对 Java 的学习或编写有任何问题,可以向 2aye@ncpachina.org 或 2240400@ncpachina.org(<— 这个是作者)发邮件询问。