问题描述
我正在尝试编写一些代码来为ID号,名字,姓氏,midterm分数和以下数据的最终分数创建getter和setters,这是在一个文本文件中写... ...
10601 ANDRES HYUN 88 91 94 94 89 84 94 84 89 87 89 91
10611 THU ZECHER 83 79 89 87 88 88 86 81 84 80 89 81
10622啤酒杯95 92 91 96 99 97 99 89 94 96 90 97
10630 TRUMAN SOVIE 68 73 77 76 72 71 72 77 67 68 72 75
ID是第一个数字,第一个和最后一个名字是自解释的,midterm得分是第二个到最后一个数字,最后一个数字是最后一个分数。这是我第一次使用getters和setters,到目前为止我有...
int getID()const {return ID; }
void setID(int i){id = i}
string getFname()const {return Fname;}
void setFname(string f){Fname = f}
string getLname()const {return Lname;}
void setLname(string l){Lname = l}
int getMidterm()const {return Midterm;}
void setMidterm(int m){Midterm = m}
int getFinal()const {return Final;}
void setFinal(int fl){Final = fl}
我在正确的轨道上吗?此外,如何指定数据集中的第一个数字是ID,第一个字符串是第一个名称,第二个字符串第二个名称,第二个到最后一个数字是Midterm,最后一个数字是最终得分?
解决方案如果你问的是写getter和setters,是的,这是多少人做的。要写复杂类型的完美访问器,你可以使用:
void setLname(const String& input){Lname =输入; } //复制字符串
void setLname(String&& input){Lname = std :: move(input); } //移动字符串
const String& getLname()const {return Lname; } //允许从const对象获取一个const String引用
String& getLname(){return Lname; } //如果对象不是const,允许修改对Lname的访问。
请注意,这超过了两个人,通常人们谈论,通常只有一个吸气剂一个设置器。只有很少的人会想要写更多。
但是,许多访问器是一个代码气味:
他们用不提供任何抽象的函数覆盖代码。
每当你同时拥有getter和setter时,你基本上不受限制地访问该数据成员;所以你也可以写
public:String Lname
。
标志你正在建模数据,而不是行为。在这些情况下,你应该重新考虑你的设计,或者诚实地,继续使用公共成员,或者如果所有成员都是公共的,
struct
IMHO。 / p>
请注意,我在这里谈论纯访问器,而不是那些实际上做一些明智的访问检查值范围,相应地更新其他成员,等等。
所以,如果我是你,我会问自己:什么行为我需要从我的课?我真的需要谈论其他代码中的所有数据成员,或者我可以找到合理的抽象,我的类可以提供,这将使访问器无用?有可能我正在考虑对错误的对象类进行建模吗?或者,我真的只想要一个容器用于我的数据,可以在任何地方使用和修改?
I am attempting to write some code to create getters and setters for the ID number, first name, last name, midterm score, and final score of the following data, which is in a text file, in a class i'm writing...
10601 ANDRES HYUN 88 91 94 94 89 84 94 84 89 87 89 91 10611 THU ZECHER 83 79 89 87 88 88 86 81 84 80 89 81 10622 BEVERLEE WAMPOLE 95 92 91 96 99 97 99 89 94 96 90 97 10630 TRUMAN SOVIE 68 73 77 76 72 71 72 77 67 68 72 75
The ID is the first number, first and last name are self explanatory, the midterm score is the second to last number, and the last number is the final score. This is my first time using getters and setters and so far I have...
int getID()const{return ID;} void setID(int i) {ID=i} string getFname()const{return Fname;} void setFname(string f) {Fname = f} string getLname()const{return Lname;} void setLname(string l) {Lname = l} int getMidterm()const{return Midterm;} void setMidterm(int m) {Midterm=m} int getFinal()const{return Final;} void setFinal(int fl) {Final=fl}
Am I on the right track at all? Also, how do I specify that the first number in the data set is the ID, the first string is the first name, second string second name, second to last number is Midterm, and last number is final score?
解决方案If you are asking about writing getters and setters, yes, this is how many people do it. To write "perfect" accessors to complex types, you would use this:
void setLname(const String& input) { Lname = input; } //copies the string void setLname(String&& input) { Lname = std::move(input); } //moves the string const String& getLname() const { return Lname; } //allows to get a const String reference from a const object String& getLname() { return Lname; } //allows modifying access to Lname if the object is not const.
Note, that this is more than the two, people normally talk about, and usually only one getter and one setter are seen. Only very few people would want to write more than that. Yet, each of these four has a slightly different job to do and might be advisable to add.
However, lots of accessors are a code smell:
They litter the code with functions that do not provide any abstraction.
Whenever you have both a getter and a setter, you have essentially unrestricted access to that data member; so you could just as well have written
public: String Lname
.Lots of accessors are a certain sign that you are modelling data, not behaviour. In those cases, you should either rethink your design, or be honest and go ahead and use either public members, or, if all members would be public, a
struct
IMHO.Note, that I'm talking about the pure accessors here, not the ones, that actually do something sensible, like checking a value range, updating other members accordingly, and so on. And these pure ones are the ones you should avoid.
So, if I were you, I would ask myself: What behaviour do I need from my class? Do I really need to talk about all its data members in other code, or can I find sensible abstractions that my class can provide which would make the accessors useless? Is it possible that I'm thinking about modelling the wrong class of objects? Or do I really just want a container for my data that can be used and modified everywhere?
这篇关于C ++:Getters和Setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!