Tightly encapsulated class

This is my homework question: Which of the following classes is NOT tightly encapsulated?

class A
{
    private int x;
}
class B
{
    private int x;
    public void setX(int x)
    {
        this.x=x;
    }
    public int getX()
    {
        return x;
    }
}
class C
{
    private int x;
    private void setX(int x)
    {
        this.x=x;
    }
    private int getX()
    {
        return x;
    }
}

I spent some time searching for the encoder, but could not get the necessary material. Can anyone help me out?

I think the class C is not encapsulated because the methods are private. And about class A, what I feel is not only encapsulated. As the basic definition of encapsulation says, "you have to hide your members and provide custom class methods that behave the way you want." Now class A has no accessor-mutator methods. Therefore, A should not be considered encapsulated. I'm not sure, but. Please correct me if I am wrong.

+5
source share
11

" " ? Google, .

, . X? . A ? "" C , ?

. " ". , , , , , , , . , , , , .

+4

. A, C , .

+1

. , .

, . .

, , , total. . ( ) (double amt), .

A, . - , .

+1

, () .

, private, , , .

+1

, , . , - , , . . None, , .

0

B , " , "

0

.

, . , .

0

.

, .

getter setter.

getter Setters, , .

, getter setter, Tightly .

0

, - , NB, -, , .

0

. . , - ( , , ).

0

" " , ( , getter setter.)

In your example, all three classes are closely encapsulated, since each class has all the variables as private.

-1
source

All Articles