JavaSE (X) the object-oriented: the polymorphism characteristic, Object class, code block, Keyword: static, final, parent-child class execution sequence

1, the object-oriented features: polymorphism

Understand polymorphism: a variety of forms of things

1.1 polymorphisms reflect broadly:
① method of rewriting overloaded. ② polymorphism subclass objects

Polymorphism reflects the narrow 1.2:
① subclass object Polymorphism

1.3 What is subclass object polymorphism:
parent class object reference points to subclass

1.4 application polymorphism:
  virtual method invocation compiler look left, look to the right to run:
  when compiling : You can only see the parent class methods and properties
  Runtime : Call a subclass override the parent class

1.5 polymorphism description:
   Thinking? What is polymorphism?
    Parent class reference to an object subclass
  think? Properties not polymorphism?
    Not
  thinking? Unique properties and methods if you need to call the child class how to do?
    Downcast

1.6 Polymorphism premise used:
① ② must override the method must inherit relationship

1.7 Transformation and down in transition up:
upcast: Polymorphic
Transformation down: the reference to the parent class and then converted into a strong subclasses
FIG:

   

Why use downcast?
Parent class reference to an object subclass, the compiler look left, look to the right to run. At compile time you can only see the properties and methods of the parent class. In fact when the memory is also loaded with the methods and properties of Patent subclass. Then we need to call the subclass-specific methods and properties you will need to downcast.

Description:
    ① downward transition may occur: a ClassCastException
    ② downward transition can be used: casts symbol
    ③ downward transition is necessary to determine: the instanceof

a instanceof A: a whether the object is an instance of class A, if so returns true, false otherwise

1.8 face questions: polymorphic behavior compile time or run behavior?
     Runtime behavior

2、Object

Description 1.1 Object class

  1. Object class is a base class (parent class
  2. Other classes inherit a class If not, then the default inherit the Object class
  3. Object class Method 11 (of which three are overloaded methods)
  4. Object only an empty argument constructor

2.2 equals method

a equals method of Object

public boolean equals(Object obj) {
        return (this == obj);
    }

Two image system String, Date, etc. type, are rewritten equals method.
 In the custom class, if it does not override the equals method to compare the value of the object address.
         Often when we are required to call the equals method compares the contents. Therefore, custom class equals method needs to be rewritten

2.3 toString method

The method of Object toString

 public String toString() {
       return getClass().getName() + "@" + Integer.toHexString(hashCode());
     }

Two image system String, Date, etc. type, are toString method override.
     
    Custom class, if no override toString method the output value is the address of the object.
         We often when calling toString methods are needed to output the contents of the object attributes. Therefore, custom class toString method needs to be rewritten

2.4 face questions? == and equals the difference?

==: basic data types: numeric comparison of specific
         reference data types: comparing the address value of the object. (Two addresses point to the same piece of memory)
equals: If you do not override the equals method is then called Object equals method of comparison is the address value. Generally, we will rewrite the equals method to compare the content.

3, keyword static

3.1 can be used to modify the structure of the
attributes, methods, internal class, block

3.2 static attribute modification
1. multiple objects with a class of their own copy of instance variables, objects that work together have a class variable.
2. When an object of class variables and modified, then the other object is seen a modified class variables.
3. class variable is loaded with class and loading, instance variables are created with the object and loaded.
4. The class loading to load only once
call The class variable: class name of a class name of a class variable variable objects.

3.3 static modification methods
1. static method is loaded with the kind of loaded while
calling 2. static methods: static methods class name names object name static method name
3. Static methods can not call non-static methods and instance variables, non static method can call the static methods and instance variables.
4. precedence class loading to create the object
5. The static methods can not be used in this and super

3.4 Memory resolve

3.5 When to use static properties and methods of modifying
static modification properties:
    ① when multiple objects jointly owned a property declaration when ② constants. Math.PI
static modification methods:
  when you need to call the class variables ①, use the static method. As a method ② when the tools

4 structure, class: code block

Action code block 4.1: it may be the initialization of the attribute class, the class declaration

4.2 Classification: vs non-static static block of code block
format:
        static block of code: {} static
        non-static block: {}
Note: The code can only be modified static block

4.3.
Static block of code:

  1. With the class is loaded and loaded, the class is loaded only once loading (static code block is executed only once)
  2. Static code block is executed in preference to non-static block
  3. If a plurality of static code block, the order of execution is performed sequentially from top to bottom
  4. Only call static methods and class variables

Non-static code block:

  1. With the creation of objects and loaded, every time an object is created on the implementation of a
  2. Non-static loading code block precedence call to the constructor.
  3. If a plurality of non-static code block, the order of execution is performed sequentially from top to bottom
  4. You can call static methods and class variables

5, the assignment order attribute

Attribute assignments of: assigning a default value 2. Display 3. 4. constructor object name assignment method name, object name The name attribute block.

Sequence: 1 -> 2/5 (who should assign the position above) -> 3 -> 4

6, the keyword final

Final modification categories: can not be inherited
Final modified properties: only be assigned once, often used as a constant
Final modification method: can not be rewritten
assigned a modified embodiment Final properties:
1. Display assignment block 2. 3. builders (per a constructor must be assignment

7, the order of execution of the parent class and subclass

A parent class, subclass has static block of code, static methods, constructors: The particular order of execution is as follows:

  • A static block of code parent class
  • A static block of code subclass
  • Block of code configured parent class
  • Performing parent class constructor
  • Block of code configured subclass
  • Constructor method performed subclass

Guess you like

Origin www.cnblogs.com/wushaopei/p/12204263.html