gpt4 book ai didi

java - Java中的设置、获取方法

转载 作者:行者123 更新时间:2023-11-30 08:40:55 25 4
gpt4 key购买 nike

我刚刚开始接触 Java 中的面向对象编程。我很好奇下面两段代码之间的区别(如果有的话)。

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

这是第二段代码;

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

我更喜欢这里的第二段代码,因为它更容易理解,但我已经阅读了很多关于方法和对象的内容,但我无法弄清楚实际的区别是什么。 set 和 get 方法是输入值的安全方式吗?

最佳答案

让我们从您认为更简单的代码开始:

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

关于java - Java中的设置、获取方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com