JAVA_SE_ notes finishing (Object-Oriented II)

Oriented Object two

1 , Inheritance:

Inheritance Overview: same properties and behavior in the presence of a plurality of classes, the extracted content to a single class, then the class without redefining a plurality of these properties and behavior, as long as that class can be inherited. the reason.

Usage : class  subclass name  extends  parent class name {}

This class is called the parent separate class, the base class or superclass; it may be referred to more than one class or subclass derived class.

Inheritance benefits:

Improve the reusability of code, multiple members of the same class can be placed in the same class.

Improve the maintenance of the code, if the function changes only need to modify one.

Let generate a relationship between classes, polymorphism is the premise: malpractice, class of coupling enhancements.

Code design principles: low coupling high cohesion

Simple to understand:

Cohesion is their ability to complete something is.

It is coupled with the class relationship between classes.

When we design principle is: he can not complete the trouble others, so others had to modify future, it has little effect on me.

Thus: use inheritance in development is in fact the use of double-edged sword. Today, we are still at the benefits of inheritance to use, because there are many other inherited characteristics.

Inherited characteristics:

java only supports single inheritance, do not support multiple inheritance.

A class can have only one parent can not have more than one parent class.

// class SubDemo extends Demo {} // is possible.

// class SubDemo extends Demo1, Demo2 {} // not plurality father

java supports multiple inheritance (inheritance system)

class A{}
class B extends A {}

class C extends B {}

Precautions:

Subclass can only inherit all non-private members of the parent class (member methods and member variables), static non-private property can be inherited parent class.

In fact, this also reflects the shortcomings of another inheritance: to break the encapsulation

Subclass can not inherit the parent class constructor, but by the super keyword to access the parent class constructor.

Do away to inherit some of the features.

Inherited member variables in the relationship:

Child the parent class of the same name and different names of member variables:

Subclass to access a variable

First, find the local area in the subclass

Then look at the membership subclass

Finally, look for membership in the parent class (certainly not have access to the parent class local scale)

If you still can not find on the error.

2 , Super key word

usage and super like this

Representative of this class object reference to the present

Representative super parent identifier storage space (will be appreciated that the parent class object reference)

Usage (this can be used as follows and super)

Access to member variables:

this. member variable super. member variables

Access constructor (parent class constructor problem child speaking)

this(…) super(…)

Member method (problem child parent class member method of speaking)

this. member method () super. member method ()

3 , the relationship between the constructor inheritance

All subclasses constructor default constructor with no arguments will access the parent class.

Because a subclass inherits the parent class data, you can also use the data in the parent class. Before all subclasses initialization, be sure to complete the parent class initialization data.

Each default constructor first statement is: super ();

If the call is displayed on their own, it will not default call super ();

 

By displaying super subclass constructor to call the parent class of the other parameterized

By this subclass of this class to invoke other constructors

Other configurations are also in this category must first access the parent class constructor

We must pay attention to:

super (...) or this (....) must appear in the first statement

Otherwise error.

4 , block

What is a block of code?

A code segment can be executed, for short code blocks, the {}

Three kinds: ordinary code block, code block structure, static code block.

Common block: in the process of occurring or {statement} is called common code block. Common code block and general statement execution order is determined by the order in which they appear in the code - "first appeared before the implementation of the" code block to define variables only valid in the block.

Block configured: not added directly defined and referred to as a {} block block configured static keyword in the class. Code block is configured to be called when the object is created each time will be called to create an object, and the execution order of the code block structure in preference to class constructor.

Static code blocks: blocks of code declared static keyword in java. Block is used to initialize static class initialization for the class attributes. Each static block of code is executed only once. Since the JVM executes a static block if the class is loaded, the static code block to execute the main method. // if the class contains a plurality of static code block, according to the "first execution code is first defined, the code execution definition."

Note: static block of code, can not exist in any of the methods vivo

Static block of code can not directly access instance variables and instance methods. You need to be accessed through an object instance.

5 , inherited a member of the method:

Subclass object by a method to access

First look for in a subclass

Then look in the parent class

If you're still not on the error. (Irrespective of the parent class of the parent class)

Method overrides Overview:

Subclass method declaration and appears exactly the same parent class (as defined), also called coating method, replication method.

Advantage:

If a different method name, it calls the corresponding method.

If the method names are the same, the end-use sub-class of their own.

The method of rewriting applications:

When a subclass needs function of the parent class, subclass and main function has its own unique content, you can override the parent class method, so that followed the functionality of the parent class, but also defines the type of unique content.

Precautions:

Parent private methods can not be overridden

When subclasses override the parent class method, access rights can not be lower.

// given: Trying to lower to assign access rights; formerly public- "Subclasses override permissions rear smaller than the parent class method permissions

Parent class static methods, subclass must be rewritten by the static method. (In fact, this method is not really rewrite, but the phenomenon is indeed the case, as to why not really overridden methods, polymorphism I'll explain)

6 , Final Keyword

final keyword is the ultimate meaning can be modified class, member variables, member methods.

Modified class, the class can not be inherited

Modification methods, the method can not be overridden

Modifying a variable, it becomes a constant, it can only be assigned once. Ordinary local variables are modified to be constant. Member variables are modified, member constant. Members of the constants can only be initialized before the constructor and method of construction. Otherwise, initialization is not allowed.

Modify member variables:

Basic types, a value can not be changed

Reference type, the address value can not be changed

final modifications member variable initialization time:

Before object construction can be completed (it must be assigned only once and can not be repeated assignment)

Guess you like

Origin www.cnblogs.com/songliuzhan/p/12624131.html