Java – reasoning of object-oriented access specifiers

I have a general question about the reasons for object - oriented access specifiers I never fully understand why they exist. I just think they exist as a very basic form of code security, but after reviewing the discussion of this thread

Does Python have “private” variables in classes?

I know I was completely wrong. They didn't help safety at all Are those access specifiers just considered good programming practices in object-oriented design? When we say private or protected, who protects data fields or classes or methods? Can people still access methods that already know it? Through reflection and other methods?

I'm sorry if this problem seems basic or a little meta plagiarism, but it always bothers me. I don't know the exact reasoning of one of the main OOP encapsulation concepts

Solution

The access modifier is used to indicate how you intend to use your code They are particularly important when you maintain your internal state Consider a class that holds counts:

public class Thing {
    public int count = 0;
    public void doSomething() { count++; }
    public int getHowManyTimesDone() { return count; }
}

What's wrong with this code? If the caller modifies the count, my code violates its contract:

Thing x = new Thing();
x.doSomething();
x.doSomething();
x.count = 0;
System.out.println(x.getHowManyTimesDone());

My class contract says this should print 2, but it prints 0 because the caller modified the count By calculating a private variable, I tell the caller, "Hey, you shouldn't touch this! Doing so may break the code!"

Instead, by convention, a single underscore prefixed with a variable provides the caller with the same warning: "if you touch it, it may break this code!" This is probably most similar to being protected Double underscores also trigger name changes, indicating that even subclasses should not use it; It's most personal In Python, if they want to access these members anyway, the caller has the responsibility to accept the risk, but encourages programmers to be as public as possible

As for who implements variable visibility in Java, it is the runtime itself However, there are some ingenious ways to solve this problem I believe reflection provides some means for anyone who enters bytecode or the JVM itself to do something Consider what the simulation does; They can convince JVM emulation to be a special type, even if it is not

Edit:

There is another thing. In Java, programmers are encouraged to keep all instance variables confidential and use methods to access and change them This is for maintainability, not philosophical reasons to hide details Since Java has no c# or Python like attribute mechanism, if you need to add logic when getting or setting an instance variable, you need to modify all code that depends on the instance variable to use these methods By always using methods to access variables, you can minimize related code interrupted by making such changes In other words, it is an obstacle to dealing with language shortcomings

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>