简体   繁体   English

使用针对getter和setter方法的正确的Class修饰符来纠正Java中的层次结构

[英]Correct hierarchy in Java with correct Class modifiers for getters and setters methods

Before nothing, 一无所有

my problem is simmilar to these: get super class value in java Getting the name of a sub-class from within a super-class 我的问题与此类似: 在java中 获取类的值从类中 获取子类的名称

but not solve my problem. 但不能解决我的问题。 So, I'm going to explain it. 所以,我将解释它。 I expect no repeat a topic... 我希望没有重复的话题...

I'm creating a hierarchy. 我正在创建一个层次结构。 This hierarchy is only for Object with getter and setter. 此层次结构仅适用于具有getter和setter的Object。 Just for show information. 仅用于显示信息。

Well, I want to do correctly my hierarchy but without do something ilogical with modifiers. 好吧,我想正确地执行我的层次结构,但不要对修饰符做一些不合逻辑的事情。 This is a simplified example of my Classes, is not exactly Java is pseudocode: 这是我的类的简化示例,不完全是Java是伪代码:

Class A
    private id;

Class B extends A
    private dataB;

Class C extends A
    private dataC;

Variable "id" is common for Class B and Class C. Because that B and C extends A. I'm thinking that never I going to use Class A for show data, just B and C, like if A were an Abstract Class. 变量“ id”在B类和C类中很常见。因为B和C扩展了A。我想我永远都不会使用A类来显示数据,而只是使用B和C来显示数据,就像A是抽象类一样。

My problem is: 我的问题是:

I don't know if is correct put methods getter and setter in class A with modifiers public and use this methods from Classes B and C, because the hierarchy is correct, is logical but the data "id" is not correctly encapsulated. 我不知道类A中带有修饰符public的正确的put方法getter和setter是否正确,并使用类B和C中的此方法,因为层次结构是正确的,是合乎逻辑的,但是数据“ id”没有正确封装。

Class A
    private id;
    private name;
    private age;

    public getId(){...}
    public getName(){...}
    public getAge(){...}

Class B extends A
    private dataB;

    public getDataB(){...}

Class C extends A
    private dataC;

    public getDataC(){...}

To access to my object I want to do this: 要访问我的对象,我想这样做:

B.getDataB();
B.getId();
B.getName();
B.getAge();

C.getDataC();
C.getId();
C.getName();
C.getAge();

This works but all method of Class A must be public, the variables aren't correctly encapsulated. 这可以工作,但是A类的所有方法都必须是公共的,变量没有正确封装。

Are there other ways to do this? 还有其他方法吗? Is this the best/worst way? 这是最好/最坏的方法吗? Getters and setters could be an exception to "jump" the logical of the modifiers? 获取器和设置器可能是“跳变”修改器逻辑的例外吗?

I excpect you could understand my example and my "English". 我希望您能理解我的例子和我的“英语”。

Thank you in advanced. 在此先感谢您。

There are various ways of achieving encapsulation. 有多种实现封装的方法。

  1. The best way is to make public methods in base class to access private data member. 最好的方法是使基类中的公共方法访问私有数据成员。 That is what you have applied. 那就是你所申请的。
  2. The other way is to make your base class members protected and public methods in subclass (that inherits them) to get and set them. 另一种方法是使基类成员受保护,并在子类(继承它们)中使用公共方法来获取和设置它们。

As per my knowledge and other JAVA books authors like Paul Deitel prefers the first method to achieve maximum encapsulation. 据我所知,Paul Deitel等其他JAVA图书的作者更喜欢采用第一种方法来实现最大封装。

About getter/setter there is very simple rule : 关于getter/setter有一个非常简单的规则:

If you declare variable in class A getter/setter should be provide in class A . 如果在A类中声明变量,则应在A类中A getter/setter If you need any mutation in child class then override. 如果您需要在子类中进行任何更改,请覆盖。

This make code more readable and easy to debug. 这使代码更具可读性且易于调试。

BTW you can't write getter/setter of id anywhere other then class A because it's private. 顺便说一句,您不能在A类以外A任何地方写id getter / setter,因为它是私有的。 SO, also in this case the above theory complies. 因此,在这种情况下,上述理论也得到了遵守。

In simple, You can declare variable as private . 简单来说,您可以将变量声明为private But related getter/setter must be public. 但是相关的获取getter/setter必须是公开的。

Hope it will help you. 希望对您有帮助。

Actually We use getters and setters for 实际上,我们使用getter和setter

  • Reusability 可重用性
  • to perform validation in later stages of programming 在以后的编程阶段执行验证
  • Getter and setter methods are public interfaces to access private class members. Getter和setter方法是访问私有类成员的公共接口。

Encapsulation Procedure: The encapsulation procedure is to make fields private and methods public . 封装过程:封装过程是to make fields private并将methods public to make fields private methods public

Getter Methods: We can get access to private variables. Getter方法:我们可以访问私有变量。

Setter Methods: We can modify private fields. 设置方法:我们可以修改私有字段。

Even though the getter and setter methods do not add new functionality, we can change our mind come back later to make that method safer and faster . 即使getter and setter methods未添加新功能,我们也可以稍后改变主意,以使该方法saferfaster

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM