Java object-oriented in basic grammar 05-

 

First, the package

Object-oriented three properties: encapsulation, inheritance, polymorphism

Hide the complexity of internal objects, only open to the public simple interface. Easy to call the outside world, thereby enhancing the system scalability, maintainability.

How to package

Access is controlled by a respective control modifiers visible boundary

(1) Class

(2 bags

(3) Module: After introducing Java9

(4) System

For the members of the class: four permission modifiers can be used

For external classes: public and can only be used by default both

Encapsulated object attributes

  • Implementation details behind class

  • So that users can only access data in advance by a predetermined method, which can be added to the control logic inside the method, unreasonable restrictions on access to member variables.

  • Data can be checked, which will help ensure the integrity of the object information.

  • Easy to modify and improve the maintainability of the code.

Using the privatemodified member variable

private data type variable name;

How to solve the problem of local variables and member variables of the same name

When the local variables and class variables (static member variables modified static variables) of the same name, in front of the class variables plus "class name.";

When the local variables and instance variables (non-static member variables) of the same name, in front of the instance variable is incremented by "this."

Package (Package) acting

(1) to avoid duplicate names like: Once you have the package, the full name of the class becomes: package name of the class

Numerous (2) Classification and Organization Management

(3) can control some types of members or visible range

If the permission or members of a certain type of modification of the default, then it is limited to the use of this package

Statement package

package package name;

(1) must be the first line of code in the source file

(2) a source file can have a package declaration statement

Specifications: All words are lowercase, using split between each word.

How line compiler and runtime package declaration in the Java command file (understand)

javac -d . TestPackage.java

Where. Denotes generating package directory in the current directory

Full name of the class to run correctly use this class

How to use the class cross-package

Prerequisite: permission modifier class or member is to use> the default, you can see

Full Name (1) type of use

After (2) use the import statement, using the code name Jane

note:

import statement must be in the following package, class above

When two different classes package with the same name, for example: java.util.Date and java.sql.Date. Use a full name, use a simple name

Member variable initialization problem

We know that the class member variables have default values, but sometimes we want to assign a number to specify values ​​for them, then how can we do it this time?

(1) explicit assignment

public class Student{
  private String name = "小明";
}

(2) initialization block / block

Static initialization block: static variable initialization

// used to initialize variables may be more complex

[Modifier] class class name {
  static {
    static initialization
  }
}

Examples Initialization: initialize an instance variable

[Modifier] {class class name
  {
    instance initialization block
  }
}

EXECUTION

Static initialization block: initialization in the class called by the class loader implementation of static initialization of each class only once, founded in early instance of an object.

Examples of initialization block: automatically executed each time new instance of an object, an object for each new, executed once.

Constructor

We found examples of initialization block is the same as the value of an instance variable for each instance of an object initialization, so if we want a different instance object is initialized to a different value, how to do it? At this point we can consider using the constructor.

The role of the constructor

When creating an object instance variable is assigned an initial value.

Note: The only constructor to initialize an instance variable, is not static class variable initialization

Syntax constructor

[Modifier] constructor name () {
  // Initialization code instance
}
[] constructor modifier name (parameter list) {
  // instance initializer
}

Precautions:

  1. Constructor name must be it in the name of the class must be the same.

  2. It does not return a value, so no return type, not even need to void

  3. If you do not provide constructors, the system will give no default constructor parameters, and the same modifiers default constructor of the class modifiers

  4. If you provide a constructor, the system will no longer provide no-argument constructor, unless you define.

  5. Constructor is overloaded , either defined parameters, the parameters may not be defined.

  6. Constructor modifier can only be a privilege modifier can not be any other modification

A variety of ways of initialization order problem

1, class initialization

Conclusion : explicit assignment of static initialization order execution

Analysis:

In fact, Java compiler to compile Java class, the class code to do processing, explicit assignment of static variables compiler will Java class static initialization statements assembled into a <the clinit> () class initialization method.

2, instance initialization

If a non-static instance variables, both explicit assignment, another instance initialization block, as well as constructor, then what is the result?

in conclusion:

(1) explicitly initialized Example initialization block , whether you create an instance constructor by which they will be executed

(2) Example explicit assignment initialization block execution order

(3) corresponding to the last code execution constructor

Analysis:

In fact, Java compiler to compile the Java class, the class code done processing an explicit assignment of the instance variables and instance compiler will initialize the Java class constructor statement assembled into one <init> (...) instance initialization method, you declare several constructors, and finally there are several examples corresponding initialization method, if the class does not declare any constructors, then there is a default constructor with no arguments, that also correspond one example initialization method have no parameters.

Examples of configuration and initialization block

To some degree, non-static block (initialization block instance) is complementary to the constructor, the non-static code block is always performed prior to performing the constructor. The difference is that the constructor, non-static code block is a fixed code for execution, it can not receive any parameters. Thus non-static initialization process block identical for all objects of the same class performed. For this reason, the use of substantially difficult to find a non-static code block, if there is a code identical to the initialization process for all objects, and without any parameters, it is possible to extract this initialization processing code to non-static block.

Each constructor that is, if the same initialization code, and these need not receive parameter initialization code, can put them in a non-static code defined in the block. By the same plurality of code extraction unit configured to define non-static block, can better improve the initial code reuse, to improve the maintainability of the entire application.

Example class initialization sequence initialization

Class initialization must take precedence over instance initialization and class initialization occurs only once for each class, and initialization is to look at every new instance of the object must be executed once.

Standard JavaBean

(1) and the specific class must be public,

(2) has a configuration and method without parameters,

(3) the member variable privatization, and provides for operating the member variables setand getmethods.

 

Guess you like

Origin www.cnblogs.com/Open-ing/p/11862515.html