c++里 accessor 和 mutator 分别指什么?
答案:2 悬赏:80
解决时间 2021-02-08 02:39
- 提问者网友:多余借口
- 2021-02-07 05:50
c++里 accessor 和 mutator 分别指什么?
最佳答案
- 二级知识专家网友:厭世為王
- 2021-02-07 07:30
都指一样的东西。
让某个变量只能通过公共方法来存取,这些变量就叫accessor或mutator。
比如
学生类中的有姓名变量name
我只设name为私有private,只能通过公共方法getName()和setName(n)存取。
这样在外部就根本不知道name在哪里存储,甚至name变量本身都可以不存在,对外只需要知道"Name"这个名称,就能操作这个想象中的Name变量。
此时'name'这个名称就成了一种accessor存取器或叫mutator变值器。
让某个变量只能通过公共方法来存取,这些变量就叫accessor或mutator。
比如
学生类中的有姓名变量name
我只设name为私有private,只能通过公共方法getName()和setName(n)存取。
这样在外部就根本不知道name在哪里存储,甚至name变量本身都可以不存在,对外只需要知道"Name"这个名称,就能操作这个想象中的Name变量。
此时'name'这个名称就成了一种accessor存取器或叫mutator变值器。
全部回答
- 1楼网友:转身后的回眸
- 2021-02-07 08:40
简单的说就是我们常见的bean中的get/set方法。mutator methods是set方法,accessor methods是get方法。
public class requestparameters {
private string id;
// accessor method
public final string getid() {
return id;
}
// mutator method
public final void setid(string id) {
this.id =id;
}
}
an accessor method is used to return the value of a private field. it follows a naming scheme prefixing the word "get" to the start of the method name. these methods always return the same data type as their corresponding private field (e.g., string) and then simply return the value of that private field.
a mutator method is used to set a value of a private field. it follows a naming scheme prefixing the word "set" to the start of the method name. these methods do not have a return type and accept a parameter that is the same data type as their corresponding private field. the parameter is then used to set the value of that private field.
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯