Home>

java:naming

1.Class name

Class names usually end with a noun.And it should be reflected in the class name whether it is mainly to save data or provide functionality.For example, the connectionbuilder class can all guess that its main function is to create a connection object.

Class names ending with the verb -er/or,It should contain at least one method that starts with the verb.For example, the connectionbuilder class should contain at least one method that starts with build-.With this tacit understanding,Others can use this class more conveniently.

2, the name of the method

Take the addition, deletion, modification of the person class as an example--

The method for creating a person object is generally named createperson ()/newperson (),

Add method named addperson (), modify method named updateperson (),

The delete method is named deleteperson ()/removeperson (),

The method to query based on the primary key is named getperson ()/queryperson ().

Query based on other conditions,To indicate the specific condition type,For example getpersonbyname () or getpersonbyage ().

The names of the methods should be uniform.For example, you used deletexxx instead of removexxx at the beginning, so the entire project should be used like this.

3.Counting method

The counting method is usually named getnumberofxxx ()/getxxxcount (). For example, getnumberofrows (), getquestioncount ().

4. Methods that return boolean values

In javabean, the returned boolean attribute must be named with isxxx ().

A method to check whether an attribute is empty or a record exists.It is usually named hasxxx (), such as hasresult ().

Methods for checking the status of an object are usually named after the is + adjective. For example isclosed (), isready ()

5. Methods that return collection objects

Methods that return collection objects should be plural.For example, getpersons () can also reflect the container type,For example getpersonlist ().

6.Don't simplify words

Do not write gethislist (), but gethistorylist ();do not write usrno, but usernumber. A simple principle is to be able to pronounce the name.

7.Parameter naming should be precise

createuser (string str1, string str2) looks hard to understand,But it would be easy to understand if written as createuser (string username, string password).

8.The return value is named result

Name the return value in a method to make the context of the method clearer.

Interface

There are no specific principles for naming interfaces.Like the class name, it must accurately reflect the function of the interface.

Some people like to prefix the interface name with i. absolutely okay.Whether it is added or not,To ensure the uniformity of the entire project.

Java:variable values ​​and references

Broadly speaking,In java, any identifier that can point to an object or contains a value,Both can be called variables.

The content of the variable,May be a value,It may also be a reference to an object.

When the type of the variable is a basic type (short, byte, int, long, float, double, boolean, char), the content of the variable is a value;When a variable is of type object and its subclasses,The content of a variable is a reference to an object.E.g:

int a=3;
string s1="hello!";
string s2=s1;

At this time, the content of the variable a is 3, and the content of the variable s1 is a reference to a string object.s2 points to the object like s1.If the following statement is executed:

a=4;
s1="hello again!";

The content of the variable a is changed to 4, and the content of s1 is changed to a reference to another string object ("hello again!"). s2 still points to the original string ("hello!").

Let's talk about the method call.Method calls are passed by value.For example this method:

void f (int i, string s) {
  i=100;//the value of i is changed to 100
  s="hi!";//s now points to the string "hi!"
}

When f (a, s1) is called, the values ​​of the variables a and s1 are assigned to the parameters i and s before the f method is executed. That is,No matter how i and s change, they will not affect the values ​​of a and s1.

What follows is a slightly more complicated example.First define a method:

void f (int i, list list) {
  i=10;
  list.add ("hello!");
}

Suppose you execute the following sentence again:

int a=3;
list l=new arraylist ();
f (a, l);

So what is the result after execution?

i and a are two different variables,Changing the value of i will not affect a, so the value of a is still 3;

list and l are two different variables,But they point to the same list object. An element was added to this list object during the execution of f,Then after execution,We can get this element through the l variable:

l.get (0);//"hello!"

But if f is written like this:

void f (int i, list list) {
  i=10;
  list=new arraylist ();
  list.add ("hello!");
}

Then the list first references another list object,What changed was the content of another list object,So the list object pointed to by variable l has not changed,l.size () is still 0.

Concepts like this,Beginners may be easily confused;But as long as you practice more,It will soon become familiar.

  • Previous 11 ways ASP connects to a database
  • Next Detailed ASPNET site navigation application