set和get方法的优点vs公共变量 [英] Advantage of set and get methods vs public variable

查看:324
本文介绍了set和get方法的优点vs公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么要使用getter和setter?




例如,第二种情况比第一种情况好?

  //案例1 
public class Shoe {
public int size;
}

//案例2
public class Shoe {
private int size;
public int getSize(){
return size;
}

public void setSize(int sz){
size = sz;
}

}


解决方案<

我有一天在SO上看到的回答(由@ ChssPly76写)为什么要使用getters和setter


(几个月,几年)从现在开始,当你意识到你的
安装程序需要做的不仅仅是设置的值,你还将意识到
的属性已被直接使用238其他类:-)


还有更多优点:


  1. getter和setter 可以有验证,字段不能

  2. 使用getter可以获取想要的类 。 / li>
  3. getters和setters 是多态,字段不是

  4. 调试

  5. 可以隐藏实施更改

before:

  private boolean alive = true; 

public boolean isAlive(){return alive; }
public void setAlive(boolean alive){this.alive = alive; }

之后:

  private int hp; // change! 

public boolean isAlive(){return hp> 0; } //旧签名
//方法看起来一样,客户端代码没有改变
public void setAlive(boolean alive){this.hp = alive? 100:0; }

EDIT :使用Eclipse时,可以在字段上创建观察点,但是如果你有setter你只需要一个断点,并且... 断点(例如在setter方法中)可以是有条件的,观察点(on field)不能。所以如果你想停止你的调试器只有当 x = 10 你可以做只有断点内setter。


Possible Duplicate:
Why use getters and setters?

Is there any advantage to making methods to access private variables in your class instead of making the variable public?

For example is the second case better than the first?

//Case 1
public class Shoe{
    public int size;
}

//Case 2
public class Shoe{
    private int size;
    public int getSize(){
        return size;
    }

    public void setSize(int sz){
        size = sz;
    }

}

解决方案

What I have seen someday on SO, as answer (written by @ChssPly76) why to use getters and setters

Because 2 weeks (months, years) from now when you realize that your setter needs to do more than just set the value, you'll also realize that the property has been used directly in 238 other classes :-)

there are much more advantages:

  1. getters and setter can have validation in them, fields can't
  2. using getter you can get subclass of wanted class.
  3. getters and setters are polymorphic, fields aren't
  4. debugging can be much simpler, because breakpoint can be placed inside one method not near many references of that given field.
  5. they can hide implementation changes:

before:

private boolean alive = true;

public boolean isAlive() { return alive; }
public void setAlive(boolean alive) { this.alive = alive; }

after:

private int hp; // change!

public boolean isAlive() { return hp > 0; } // old signature 
 //method looks the same, no change in client code
public void setAlive(boolean alive) { this.hp = alive ? 100 : 0; }

EDIT: one additional new advange when you are using Eclipse - you can create watchpoint on field, but if you have setter you need just a breakpoint, and... breakpoints (e.g. in setter method) can be conditional, watchpoints (on field) cannot. So if you want to stop your debugger only if x=10 you can do it only with breakpoint inside setter.

这篇关于set和get方法的优点vs公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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