Author: javaduke (www.dukejava.com)
(1) the first letter of the class name should be capitalized. The first letter of the field, method, and object (handle) should be in lowercase. For all identifiers, all the words in them should be placed together, and the first letter of the intermediate word should be capitalized. For example:
ThisIsAClassName
ThisIsMethodOrFieldName
If a constant initialization character appears in the definition, all the letters in the static final basic type identifier are capitalized. In this way, they are constants of the compilation period.
A Java Package is a special case where all these are lowercase letters, even the middle words. All domain name extension names, such as com, org, net, or edu, should be in lower case (this is also one of the differences between Java 1.1 and Java 1.2 ).
(2) when creating a class for the general purpose, take the "classic form" and include the definition of the following elements:
Equals ()
HashCode ()
ToString ()
Clone () (implement Cloneable)
Implement Serializable
(3) for each class you create, consider placing a main (), which contains the code used to test that class. To use a class in a project, we do not need to delete the test code. If any form of modification is made, you can easily return to the test. These codes can also be used as an example of how to use classes.
(4) the method should be designed into a brief functional unit to describe and implement a discontinuous class Interface section. Ideally, the method should be concise. If the length is large, you can divide it into several shorter methods in some way. This also facilitates the reuse of code in the class (sometimes, the methods must be very large, but they should still only do the same thing ).
(5) when designing a class, consider it for the client programmer (the usage of the class should be very clear ). Then, let's look at the form of changes that may be made to the person who manages the code (think about how to make them easier ).
(6) make the class as short and concise as possible, and only solve a specific problem. The following are some suggestions for class design:
■ A complex switch statement: consider adopting the "multi-form" mechanism
■ A large number of methods involve very different types of operations: Consider using several classes to implement them separately
■ Many member variables have major differences in features: Consider using several classes
(7) make everything "private" -- private as much as possible. A certain part of the database can be "public" (a method, class, or a field, etc.), and it will never be taken out. If you forcibly extract the code, it may damage the existing code of others, so that they have to rewrite and design it. If you only publish what you must publish, you can boldly change anything else. In a multi-threaded environment, privacy is a particularly important factor-only private fields can be protected in non-synchronous use.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service