我刚刚开始使用Java进行面向对象编程。我很好奇下面两段代码之间的区别(如果有的话)。
SecondActivityFragment
这是第二段代码;
public class BuyAHouseInc
{
// Instance variables
private String firstName;
private String surname;
private String address;
private int budget;
// method to set the first name in the object
public void setFirstName(String firstName)
{
this.firstName = firstName; // stores the first name
}
// method to retrieve the first name from the object
public String getFirstName()
{
return firstName; // return value of first name to caller
}
// method to set the surname in the object
public void setSurname(String surname)
{
this.surname = surname; // stores the surname
}
// method to retrieve the surname from the object
public String getSurname()
{
return surname; // return the value of surname to caller
}
// method to set the address in the object
public void setAddress(String address)
{
this.address = address; // stores the address
}
// method to retrieve the address from the object
public String getAddress()
{
return address; // return the value of address to caller
}
// method to set the budget in the object
public void setBudget(int budget)
{
this.budget = budget; // store the budget
}
// method to retrieve the budget from the object
public int getBudget()
{
return budget; // return the value of address to caller
}
}
我更喜欢这里的第二段代码,因为它更清楚地理解,但我已经阅读了很多方法和对象,我无法弄清楚实际的差异是什么。设置和获取方法是否可以安全地输入值?
答案 0 :(得分:2)
让我们从您认为更简单的代码开始:
public class BuyAHouseInc
{
public void displayClient(String firstName, String surname, String address, int budget)
{
System.out.println("Client Name: " + firstName + " " + surname);
System.out.println("Address: " + address);
System.out.println("Budget: " + "€" + budget);
}
}
我们可以实例化这个类并像这样使用它:
public static void main(String[] args) {
BuyAHouseInc buyAHouseInc = new BuyAHouseInc();
buyAHouseInc.displayClient("jane", "doe", "123 main street", 100000);
}
此主要方法的效果是在屏幕上显示信息。那个这个类的所有实例都可以。您无法共享信息或重复使用信息。
您显示的第一段代码允许您创建一个对象,其中包含可以重复使用的数据的字段。编写getter和setter,以便您可以访问这些字段以在程序中的其他位置使用。
我们还可以将displayClient方法添加到此类中,如下所示:
public class BuyAHouseInc {
private String firstName;
private String surname;
private String address;
private int budget;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
...
public void displayClient() {
System.out.println("Client Name: " + this.firstName + " " + this.surname);
System.out.println("Address: " + this.address);
System.out.println("Budget: " + "€" + this.budget);
}
}
那么我可以编写这样的程序:
public class Solution {
public static void main(String[] args) {
BuyAHouseInc jane = new BuyAHouseInc("jane", "doe", "123 main street", 100000);
BuyAHouseInc john = new BuyAHouseInc("john", "doe", "123 main street", 50000);
System.out.println("The following clients can afford to buy a house");
if (canAffordTheHouse(jane)) {
jane.displayClient();
}
if (canAffordTheHouse(john)) {
john.displayClient();
}
}
public static boolean canAffordTheHouse(BuyAHouseInc client) {
return client.getBudget() > 50000;
}
}
答案 1 :(得分:0)
如果您询问getter / setter vs直接访问,那么getter / setter相比直接访问有许多优点。 基本上是:
这是另一个answer,它更详细地展示了我所说的内容。
答案 2 :(得分:0)
您可以组合代码块
public class BuyAHouseInc
{
// Instance variables
private String firstName;
private String surname;
private String address;
private int budget;
public void displayClient()
{
System.out.println("Client Name: " + this.firstName + " " + this.surname);
System.out.println("Address: " + this.address);
System.out.println("Budget: " + "€" + this.budget);
}
// method to set the first name in the object
public void setFirstName(String firstName)
{
this.firstName = firstName; // stores the first name
}
// method to retrieve the first name from the object
public String getFirstName()
{
return firstName; // return value of first name to caller
}
// method to set the surname in the object
public void setSurname(String surname)
{
this.surname = surname; // stores the surname
}
// method to retrieve the surname from the object
public String getSurname()
{
return surname; // return the value of surname to caller
}
// method to set the address in the object
public void setAddress(String address)
{
this.address = address; // stores the address
}
// method to retrieve the address from the object
public String getAddress()
{
return address; // return the value of address to caller
}
// method to set the budget in the object
public void setBudget(int budget)
{
this.budget = budget; // store the budget
}
// method to retrieve the budget from the object
public int getBudget()
{
return budget; // return the value of address to caller
}
}
或者,您可以将BuyAHouseInc
的整个对象传递给显示功能。
public void displayClient(BuyAHouseInc b)
{
System.out.println("Client Name: " + b.getFirstName()+ " " + b.getSurname());
System.out.println("Address: " + b.getAddress());
System.out.println("Budget: " + "€" + b.getBudget());
}
答案 3 :(得分:0)
public void displayClient(String firstName, String surname, String address, int budget)
{
//........
}
只是另一种方法。附在{
和}
中的内容定义了调用displayClient()
方法时的操作。 displayClient()
在执行任务之前需要3个参数。参数是()
中public void displayClient(String firstName, String surname, String address, int budget)
内的内容。第二段代码可以放在public class BuyAHouse
块或{ }
内。您的 setters()和 getters()也类似于displayClient()
,但参数较少。
public class BuyAHouse
的{}内部是成员或方法。这些方法可以访问类变量
private String firstName;
private String surname;
private String address;
private int budget;
这就是为什么在setters()
的大多数语法中,您可以看到它是设置/分配/存储(无论您喜欢什么)值到类变量。所以基本上set()
方法用于修改变量firstname, surname,address
和budget
getters()用于返回变量的值。
例如,
String name; //this has no string value yet
//function definition - you tell what you want this method to do
public void setMyName(String yourName){
name = yourName; //you store the value of yourName to name
}
//method call
setMyName("whatever name you like"); // whatever name you like will be passed to the yourName variable