java core technology read 10 (b) - Object Oriented Programming 2

Static field Static

Static fields are called class field, it belongs to the class, not part of an instance of an object, the object is of this class public all instances.

Constant static field in static + final named, but it can not be modified as a method or public instance of the object constant, e.g. Math.PI.

Static field is not modulated method but can call domain object instance static variables to static prefix. In fact, there is no equivalent static method implicit parameter. E.g. Math.pow (x, y). Also static domain prefix can be omitted and the best use of the class name called directly.

In the case of using two kinds of static field following method:
• a method does not require access to object state, the required parameters are provided by an explicit parameter (eg: Math.pow)
• a domain access method only requires static class (e.g. : Employee.getNextld)

Generating different styles of objects using a static factory method.

The most common way is still the main function, each class can contain a main function can be used to test different classes to do a self-test.

Method parameters

java parameters always call by value, even if the object reference is passed by value, a copy of the transfer value of the pointer. So we can not say when java / javascript object reference call by reference . C ++ has a value of calls and call references. Reference parameters marked with an ampersand .

Object construction

Method overloading of a plurality of the same name but with different parameter types. The foregoing method include the names and parameter types .

Method three kinds of initialization data field:
0: initialization default domain, assuming no field is initialized, the value in the art, bool and the object will be initialized to 0 \ false \ null. ** tips: ** utilizes overloading may be provided without a constructor argument, some default condition satisfied

1: The field is initialized to be directly assigned to the display field, the assignment can be a constant or method calls, so you can easily get to all objects with similar values. Without performing mass participation to initialize. A hidden parameter using this () to call another manner constructor.

2: initialization block, add {} public domain part to operate, the first operating portion, the body portion before running the constructor. (uncommon)

Initialization data execution order
because the initialization data fields have a variety of ways, all paths listed construction process can be quite confusing. The following is a tone
with a specific process step constructors:
1) all data fields are initialized to default values (0, false, or null).
2) in order of appearance in the class declaration, all domains are sequentially executed initialization statements and initialization block (including static data field can be labeled).
3) If the first line constructor calls a second configuration, a second configuration is performed body
4) execution subject of this constructor.

java because automatic recovery mechanism, there is no destruction, but for some objects need to be recovered, may be utilized for release finalize offense, but since this method invocation sequence problems, is not much used in practice.

package

A class may be used in all classes of ordinary package, the package and other public class (public class).
When the same name package two classes, class needs to import a precise compilation errors to avoid

Packet structure is a nested structure, named packet mode call using the file structure corresponding manner, i.e., packages and directories need to match , for example,
the javac COM / myconipany / Payrol1App.java
Java com.mycompany.PayrollApp
Second package positioned class compiler (compiler) work.

If no package, all packages in the default package. Define packet includes package xxxx

java need to have a basic directory (Source root) , this directory is the home directory javac compiler can be used to package a variety of call java and so on. And their definition of the basic package needs to be put to record calls, because the default base directory path for the start of the call.

Class, method, or if there is no variable package plus public, private statements, will become public class, public variables, public variables will destroy the encapsulation package, leading to the result has been tampered with. Sealing package (package sealing) mechanism can improve this problem.

Classpath

Class files to be placed in the jar file, the directory group outside. Class path is the set of all classes. E.g.
/ home / user / dassdir:. : / Home / aser / archi ves /. It refers to the current directory and directory jar

The compiler javac will search all of the classes from the package import command in

Note

Note 1. Insert / ** xxxx * /, prior to annotation information
2. Class Notes / ** xxxx * /, prior to the class
3. The method of annotations can be used to represent @param @return
4, general note must @author @see use the pound sign (#) instead of a period (.) separated by class name and method name

According to the above comments, javadoc automatically generated annotation link, and various methods of the package

Class design skills

1. Class and method names functionalized
2. Examples of the package holding privatization domain
3. keep immutable object field, immutable class
4. The fields are initialized, the default values without trial
5. Do not ever use the basic data types, Society instead of using the class
defined the functions of class 5, to avoid coupling
7. not all fields are required and change accessor

Published 15 original articles · won praise 1 · views 136

Guess you like

Origin blog.csdn.net/qq_17236715/article/details/103656539