获取和设置方法的重点是什么 [英] What's the point of get and set methods

查看:81
本文介绍了获取和设置方法的重点是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CS课上,我只是在学习课和OOP.

In my CS class I am just learning about classes and OOP.

因此,当您创建类时,会初始化一定数量的私有变量.

So when you create a class you initialize a certain number of private variable.

我知道您将它们设为私有,因为如果它们是公开的,它们很容易更改,并可能导致许多错误.

I know you make them private because if they were public they would be easily changeable and could lead to a lot of bugs.

因此,我们使用get和set方法来更改变量.但这又使变量非常容易更改,对吗?那么首先将它们设为私有的意义何在?

So we use get and set methods to change the variable. But that once again makes the variables very easy to change right? So whats the point of making them private in the first place?

推荐答案

使用getter和setter的一些好处(称为

Some benefits of using getters and setters (known as encapsulation or data-hiding):

1.可以将类的字段设置为只读(仅通过提供getter)或仅写(通过仅提供setter).这使该类可以完全控制谁可以访问/修改其字段.

示例:

class EncapsulationExample {
    private int readOnly = -1;  // this value can only be read, not altered
    private int writeOnly = 0;    // this value can only be changed, not viewed
    public int getReadOnly() {
        return readOnly;
    }
    public int setWriteOnly(int w) {
        writeOnly = w;
    }
}

2.类的用户不需要知道该类如何实际存储数据.这意味着数据是分离的,并且独立于用户而存在,因此可以更轻松地修改和维护代码.这样,维护人员就可以经常进行更改,例如错误修复,设计和性能增强,而又不会影响用户.

此外,每个用户可以统一访问封装的资源,并且具有独立于用户的相同行为,因为此行为是在类中内部定义的.

示例(获取值):

class EncapsulationExample {
    private int value;
    public int getValue() {     
        return value; // return the value
    }
}

现在如果我想返回两倍的值怎么办?我可以更改自己的吸气剂,使用示例的所有代码都不需要更改,并且将获得两倍的值:

Now what if I wanted to return twice the value instead? I can just alter my getter and all the code that is using my example doesn't need to change and will get twice the value:

class EncapsulationExample {
    private int value;
    public int getValue() {
        return value*2; // return twice the value
    }
}

3.使代码更整洁,更易读且更易于理解.

这里是一个例子:

无封装:

class Box {
    int widthS; // width of the side
    int widthT; // width of the top
    // other stuff
}

// ...
Box b = new Box();
int w1 = b.widthS;  // Hm... what is widthS again? 
int w2 = b.widthT;  // Don't mistake the names. I should make sure I use the proper variable here!

带封装:

class Box {
    private int widthS; // width of the side
    private int widthT; // width of the top
    public int getSideWidth() {
        return widthS;
    }
    public int getTopWIdth() {
        return widthT;
    }
    // other stuff
}

// ...
Box b = new Box();
int w1 = b.getSideWidth(); // Ok, this one gives me the width of the side
int w2 = b.getTopWidth(); // and this one gives me the width of the top. No confusion, whew!

在第二个示例中,可以查看您对所获取的信息有多少控制权,以及该信息有多清晰.请注意,此示例很简单,在现实生活中,您将要处理的类涉及许多资源,这些资源可被许多不同的组件访问.因此,封装资源可以使我们更清楚地了解我们正在访问哪些资源以及以何种方式(获取或设置).

Look how much more control you have on which information you are getting and how much clearer this is in the second example. Mind you, this example is trivial and in real-life the classes you would be dealing with a lot of resources being accessed by many different components. Thus, encapsulating the resources makes it clearer which ones we are accessing and in what way (getting or setting).

这是关于此主题的 good SO thread

这里是 good read 关于数据封装.

Here is good read on data encapsulation.

这篇关于获取和设置方法的重点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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