Getters and Setters 在公共基类和派生类内部是否有区别?

huangapple go评论69阅读模式
英文:

Is there a difference between Getters and Setters inside a public base class and inside the derived class?

问题

在第一个实现中,你将getS1setS1函数放在了基类的公共部分。在第二个实现中,你将这两个函数放在了派生类的公共部分。从你的描述和代码看,这两个实现在功能上是相同的,都允许派生类访问基类的受保护变量s1

英文:

Hello fellow programmers,

I have a fairly simple question. Does anyone know if there is any difference between me putting the get and set functions inside the public part of the base class and inside the public part of one of the derived classes? Basically i want the derived class to access the protected variable of the base class and i want to know if there is a difference between the Type1 and Type 2 codeblocks that i wrote.

Thanks a lot in advance to anyone who answers.

//Type1
#include <iostream>
using namespace std;

class base
{
    public:
    string getS1()
    {
        return s1;
    }
    void setS1(string s)
    {
        s1 = s;
    }

    protected:
    string s1;
};

class derived: public base
{
    public:
    string NotUsed;
};

int main()
{
    derived d;
    d.setS1("Test");
    cout << d.getS1() << endl;
}
//Type2
#include <iostream>
using namespace std;

class base
{
    public:

    protected:
    string s1;
};

class derived: public base
{
    public:
    string getS1()
    {
        return s1;
    }
    void setS1(string s)
    {
        s1 = s;
    }
    
    string NotUsed;
};

int main()
{
    derived d;
    d.setS1("Test");
    cout << d.getS1() << endl;
}

I have tried both of the codeblocks above and i got the same results. I just want to know if there is any difference between these 2 implementations.

答案1

得分: 0

通常最好将基类成员的setter和getter保留在基类中。想象一下,当您只需要一个Base对象(而且成员是私有的)的情况。您无法访问这些成员。而且,如果您将setter放在基类中,您可以始终从任何派生类以及基类访问它们!

英文:

It's usually better to keep your setters and getters for base class members in the base class.
Imagine the instance where you only need a Base object (and your members are private). There isn't a way for you to access those members.
And if you place the setters in the base class, you can always access them from any derived class, and the base class as well!

答案2

得分: 0

两种实现之间的主要区别在于它们如何处理封装和设计。在 Type1 中,getter 和 setter 函数放置在基类中并具有公共访问权限。这意味着所有派生类都将继承这些函数。这种方法在你希望基类负责提供一致的交互界面时很有帮助。它符合封装的原则,即基类管理对其内部状态的访问。

另一方面,使用 Type2 可能导致代码不够有组织且难以维护。这是因为你需要在每个派生类中手动重新定义 getter 和 setter 函数。

因此,通常首选使用 Type1,因为它促进了代码重用并保持了关注点的清晰分离。它允许在派生类之间保持一致的接口,并避免了代码的重复。

英文:

The main difference between the implementations is how they handle encapsulation and design. In Type1, the getter and setter functions are placed in the base class and have public access. This means that all derived classes will inherit these functions. This approach can be helpful if you want the base class to be responsible for providing a consistent interface for interaction. It aligns with the principle of encapsulation, where the base class manages access to its internal state.

On the other hand, using Type2 could lead to code that is less organized and harder to maintain. This is because you would need to manually redefine the getter and setter functions in each derived class.

Therefore, using Type1 is generally preferred as it promotes code reuse and maintains a clear separation of concerns. It allows for a consistent interface across derived classes and avoids duplication of code.

huangapple
  • 本文由 发表于 2023年5月22日 23:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定