java - Java中的Set、Get方法

标签 java oop methods

我刚刚开始使用 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);
}

这个main方法的作用就是将信息显示在你的屏幕上。这就是此类实例可以做的一切。您无法共享或重复使用该信息。

您显示的第一段代码可让您创建一个对象,其中包含存储可重用数据的字段。编写 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中的Set、Get方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382983/

相关文章:

java - PdfPcell 阿拉伯字符串的空输出

javascript:这段代码需要解释

java - find WithinHorizo​​n 方法返回 null

java - 为什么我不能访问这个其他类的方法?

java - 在 Grails 中使用正则表达式进行密码验证

java - 为什么 Eclipse 在 Override 上给我这个注释错误

java - GlassFish 服务器上的独立线程

java - 不确定如何创建使用其他类方法的菜单

c++ - 什么时候应该使用 C++ 私有(private)继承?

ruby - 使属性始终为数组的最佳方法?