JAVA knowledge summary (two): Packaging

After a lapse of nearly a year, I suddenly thought of this article has not yet finished, so we continue to start writing. I do not know where he was last written, regardless of where to start from three characteristics of object-oriented.

Classes and Objects

Prior to this, we first understand what is the object, what is already an object-oriented? Object: Everything object, thing actually exists in reality can be viewed as an object. The object-oriented is the person in concern, concern information of things. What is the class? Class is a characteristic (property) and behavior (methods) of the object will have a mold, typically for determining. That the presence of the specific object is a class, and the class is abstract object. Apple is a class, you're holding apple is an example of the object.

Just said objects have properties and methods, properties and methods so what is it? Attribute is a static object has a variety of features, what is generally used to describe a subject; and a method is described of various objects with the dynamic behavior, i.e. what the object can do.

If I had a jeep, its color, price, property type is; but it can speed up, slow down, stop that has a variety of methods.

General steps for creating a class: class == Create "instantiate an object ==" writing specific logic.

Usually we write class, need to follow the principle of single responsibility (single responsibility principle), which means that a class should have one and only one reason to cause functional changes. If more undertaken in which a class function, then blend it, coupling the higher, the less likely are multiplexed. Especially since high coupling, it may be due to a change in duties, causing changes in other duties, thereby affecting the operation of the whole process, and this is what we want to see.

Instantiate an object of the process can be divided into three steps: declare an object, the object is instantiated, the two bind. Which states that the object is a piece of open space in the memory stack memory, but this time not a valid object, because the object in space is empty. If at this time to call its properties and methods of error is thrown. Objects are instantiated in the heap is an open space, it has completed the initialization operation of the object-specific information. Finally, both the binding by assignment.

Is declared objects and instances of objects are different in spatial memory is completed by assignment, the association between them. Specific associate specific object is to heap memory address is stored in the open before the stack memory in order to complete the binding.

Construction method

Construction method is also known: a constructor or a constructor, you can use the object constructor initializes the relevant operations accomplished.

Call the constructor method must match the new keywords, it can not be called a separate object. Note that the configuration and method must not return value class of the same name.

Method name parameter list of optional, but can only be called when the object is instantiated.

When not specified constructor, the system will automatically add the constructor with no arguments; when a specified method of construction, whether it is a reference, no argument constructor method, the system will not automatically add the constructor with no arguments, a class there can be multiple constructors.

this: The current default reference to an object; use this: Call member properties, to resolve conflict member properties and local variables of the same name, you can also call member methods.

If you want to call overloaded constructor, it can only be used by this keyword this (); And remember, if it is invoked by this construction method (), it must be placed on the first line of the method body. (This must be noted)

Package

Package is hiding certain information classes within a class, do not allow direct access to an external program, but to operate and access to hidden information provided by such a method. That is to say two things: First, the hidden information object, the second leaving the interface (methods) that can be accessed.

Package specific steps: 1. Replace the access modifiers private; 2, providing a common setter and getter methods; 3, must have a constructor (the system will call the default constructor with no arguments). Wherein the setter method is writable, getter is readable. We can add your own business logic inside the setter methods, in order to achieve judge the reasonableness of the value.

Because the package has been achieved, and therefore we do not need custom parameters passed again when the object is instantiated, otherwise there is no way we provide the setter into effect.

Package management

Why management pack say it? Because the management pack is too important, in fact, many pit is the package of command and management irregularities caused.

We know that the folder can be managed file, the same file can be stored in several different files, files with the same name can only be stored in different folders.

In Java, we also managed by package java file to resolve the conflict in the same file, the bag can not be stored in a Java class with the same name, but different packages is possible.

Custom packages must be placed on the first line of Java source files. As mentioned above, we define a class, try to follow the single responsibility principle, the same token, when the establishment of the package, it is recommended to store information functions within each packet should be single.

How to call across the bags? Import keyword can be used to implement import package. Note that * can only access the specified package name of the class import package name, can not access the sub-class package under .

There is a package of efficiency, recommended import包名.类名ways to import packages, this can increase the load efficiency

import net.oschina.Test.*; // 加载包下的所有类
import net.oschina.Test.Java; // 加载指定包下的指定类

Note that it has nothing to do with the order to load the class import import statement location: priority designation of specific packet is greater than a wildcard. I.e. following loading will be greater than the rate of load above.

Finally, talk about several matters concerning package management Note:
1, must be placed on the first line of Java source file;
2, a Java source file can have only one package statement;
3, the package name in all lowercase letters manner;
4, named packet is: + modules + reverse domain function.

The static keyword

static modification

static representation static.

If it is modified member variable, then the variable is called a static class member or members, it is the class of all. This means that no matter how much class to instantiate an object, it will share the same memory space. The latter will overwrite the previous value.

If it is modified property, then the property is called static properties.

If it's modified method, the method is called a static method.

If it is modified initialization block, then the initialization block is called a static initialization block.

But can not be modified static type, not modified local variables, classes can not be modified to initialize the internal block (not static initialization block defined within the class).

Static member of a very long life cycle, generated when the class is loaded, release only until the class is destroyed.

static access

Member properties and methods common member method can directly access the current object (instantiated objects of the class), you can also access the current static methods and static properties of the object directly

Static method or class method (modified front is static), it can not directly access the same class of non-static member, because it lacks the implicit this parameter. Static Static methods can only be called directly members of the same class. If you have access only through the instantiate an object, using the object. The method of the ways members to access non-static members.

Initialization block (block)

Description of initialization block

In Java, it is enclosed in {} code is called a code block (initialization block).

When the code block appears in the method, called common code block, ordinary block of code execution order and the general statement is the same: top to bottom, the order of execution, first appeared ,, executed first.

When the block of code is defined directly in the class, the parallel members and methods, attributes, we call this time constructed of code blocks.

Remember precedence over configuration block constructor. Constructed a block of code which is called when an object is created, in preference to the constructor execution. Therefore, regardless of the configuration of a code block in another location of the classes, which are executed before the constructor.

There are a plurality of configurations between the sequence of code blocks, but are performed prior to the front constructor.

Performs initialization block

Now consider the static initialization block, block execution order ordinary initialization, the constructor of these three? Just remember to load only the static initialization block initialization in the class, and is performed only once. For three, the program execution priority: static initialization block - "initialization block -" configuration method. (Here configured initialization block comprises initialization block (either directly (declared in a method declared in the class) and a common initialization block).

Range initialization block

Look at a piece of code:

public void test(){
        {
            System.out.println("我是普通代码块1");
        }
        System.out.println("初始化块的范围");
        {
            System.out.println("我是普通代码块2");
        }
    }

We found this test method space is divided into three parts, so we can use this feature to implement variable naming conflicts.

public void test(){
        int temp =10;
        int temp =12;
    }

The above is not possible, but the following is possible:

public void test(){
{     int temp =10;
 };
       
 {    int temp =12;
};
    }

In this case two blocks of code space allows their local values, duplicate names will not cause conflict. But you can not add the same name as a local variable in the whole method of position, because it would cause problems with the two variables are defined repeat:

public void test(){
{     int temp =10;
 };
       int temp =11;
 {    int temp =12;
};
    }

Temp scope because from there to the end, which will generate a variable naming conflicts with the back of the block.

Because of space problems, inheritance and polymorphism next time I say ha, I write to you today, and thank you for reading reward! ! !

Guess you like

Origin www.cnblogs.com/envythink/p/11871384.html