简体   繁体   中英

doubt on private members and Methods

我对私有字段,变量和方法有疑问,如果我创建了一个包含私有变量和方法的类,那么该如何使用,何时申请私有成员,方法以及何时不使用?

Private members (methods are members too) are how you implement Encapsulation which is arguably the most important concept in object oriented programming.

The idea is to distinguish between concrete implementation details and an abstract interfaces. The abstract interface specifies what a class does, it's used by other classes and should not change often. That's what publich members are for.

Private members, on the other hand, are implementation details: other classes should not use or even know about them, because they are likely to change much more often.

Private - only that particular class needs access to them. Nothing outside the class can see or use them.

Protected - that particular class and any class that extends that class can access them. Classes that do not extend the member's class cannot see or use them.

Public - any other class can access, see and use them.

I learnt today that Java also has another access level which is implied if you don't specify public/protected/private. This is called "package private" and means classes within the same package can access the members, but no class outside of the package can.

This page might also help you out.

You should always give members of a class the most restrictive visibility level possible. You can use private members of a class in any method contained in that class.

Private members can only be accessed from inside the class. That is very useful, to avoid tainting the API of the class. If you declare a method as public, every outside class can use it and depends on it. Later you break code, if you want to change it. If it is private, the change is restricted to the class itself.

The principle of encapsulation when applied to Java (roughly) states that fields should be private and the values accessed by getXXX() and setXXX() methods.

Private methods are methods that are only internal to the class they're in, and have no use outside of that class.

From the Sun tutorials :

Tips on Choosing an Access Level : If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this. Use the most restrictive access level that makes sense for a particular member.

  • Use private unless you have a good reason not to.
  • Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.

if write Separate JDBC class for Connection method as private what happen. so cant use that connection method in my class deff...

A class only with private members and methods is an immuatable class, which makes it a good candidate for usage as key in Map. However you need to provide a non-private constructor and properly override hashCode and equals method.

Public and protected members are part of your classes exported API. Private and so-called package private access levels are part of the internal implementation.

You should use the lowest possible access level by default - private followed by package private - unless you have a good reason not to. I notice some developers use protected by default. This is not advised since protected is part of the API that classes in external packages can use if they are able to extend your class. If you did not anticipate/design for the extension of your class this is can lead to unfortunate side effects.

See Effective Java, 2nd Ed. for a far better explanation:

Item 13: Minimize the accessibility of classes and members

Item 17: Design and document for inheritance or else prohibit it

Quote:

"The single most important factor that distinguishes a well-designed module from a poorly designed one is the degree to which the module hides its internal data and other implementation details from other modules."

I think you've had had quite enough answers for now. Your question let me know that ur just beggining to use the Language, i'll strongly recommend you to get a copy of Joshua Bloch "Effective Java" , he explains not just what does the language do, but more importantly how to use it effectively. As in this case, why choose private members over package, public or protected ones. Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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