Java中的访问器方法 [英] Accessor Methods in Java

查看:369
本文介绍了Java中的访问器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对"setter"和"getter"方法以及它们的有用与否有疑问.

So I have a question on "setter" and "getter" methods and how useful they are or aren't.

比方说,我只是编写了一个非常基本的程序,如下所示:

Let's say I just write a very basic program like the following:

    public class Account
    {
     String name;
     String address;
     double balance; 
    }

然后,假设我编写了另一个使用此"Account"类的类,如下所示:

Then, let's say I write another class that uses this "Account" class, like the following:

    class UseAccount
    {
        public static void main(String[] args)
        {
            Account myAccount = new Account();
            Account yourAccount = new Account();

            myAccount.name = "Blah blah"
        }        
    }

等,等等.

当我写myAccount.name = "Blah blah"时,我正在更改"Account"类中的变量"name"的值.我可以按照自己喜欢的方式随意编写多次代码.但是,引起我注意的是,更好的做法是将"Account"类中的变量设为私有,然后使用"setter"和"getter"方法.因此,如果我写以下内容:

When I write myAccount.name = "Blah blah", I am changing the value of the variable "name" in the "Account" class. I am free to do this as many times as I like with the code written the way it is. It has come to my attention, however, that it's better practice to make the variables in the "Account" class private, and then use "setter" and "getter" methods. So if I write the following:

    public class Account
    {
       private String name;
       private String address;
       private String balance;

       public void setName(String n)
       {
          name = n;
       }

       public String getName()
       {
          return name;
       }
    }

我仍然可以通过创建另一个类似于以下内容的类来更改变量名称"的值:

I can still change the value of the variable "name" by just creating another class that has something like:

    class UseAccount
    {
       public static void main(String[] args)
       {
          Account myAccount = new Account();

          myAccount.setName("Blah blah");
       }
    }

我不知道使用此方法有何不同之处或应该防止人们使用此方法更改私有字段的值.有帮助吗?

I don't understand how using this method is any different or is supposed to prevent people form changing the value of a private field. Any help?

推荐答案

尝试此面向对象编程的黄金法则.

Try this Golden Rule of Object Oriented Programming.

1..创建私有实例变量.

2..创建公共获取器和设置器以访问这些Instance变量.

2. Create public getters and setters to access those Instance variable.

3..此方法称为封装.尽管封装可以在 方式,在设计模式中非常重要,就像那些不断变化的行为一样 封装在抽象类或接口中.

3. This methodology is called Encapsulation. Though Encapsulation can be used in a different way, that has importance in Design Patterns, Like Those Behaviors which keeps changing must be encapsulated in an Abstract Class or Interfaces.

4..现在回到主题Getter and Setter....

4. Well now back to the topic of Getter and Setter....

Getter and Setter帮助验证实例变量的输入.

Getter and Setter helps Validating the Input to the Instance Variable.

例如: 假设我有一种分配狗的年龄的方法,现在年龄不能为负,如果我没有设置方法,那么我将无法验证年龄的输入.

For eg: Assume i got a method to assign the Age of the Dog, Now the age can NoT be negative, If i dont have setter method, then i will not be able to Validate the Input of age.

private int age;

public void setDogAge(int age){ 
    if (age>0){
        this.age = age;
    } 
    else{
        System.out.println("Please Enter a Valid Age");
    }
}

这篇关于Java中的访问器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆