Java Advanced Features

1. Inheritance

1.1 Syntax

class Subclass extends Superclass{ }

1.2 Role

  1. Appears inherited improve the reusability of code.
  2. It appears inheritance between classes so that had a relationship, provide a prerequisite of polymorphism.
  3. Do not just get the other classes inherit a function away.

1.3 class inherits

  1. Subclass inherits the parent class, it inherits the methods and properties of the parent class.
  2. In a subclass, using methods and properties defined in the parent class, or create new data and methods.
  3. In Java, use the inherited keyword "extends", that is a subset of the parent class is not a class, but the parent class of "extended"
  4. Private member variables and methods of (private) subclass can not directly access the parent class
  5. Java supports only single inheritance, multiple inheritance is not allowed

2. The method of rewriting (the override)

2.1 Definitions

Definition: may be needed in the subclass method inherited from the parent class to the transformation, also known as the reset method of covering. During program execution, the method subclass will override the parent class
requirements:

  1. Override method must have the same method name and method are rewritten, the parameter list and return type.
  2. Overriding methods can not be rewritten to use more stringent than the method of access.
  3. The method of rewriting and shall also be rewritten as static or non-static while the
  4. Subclass exception thrown is not greater than the parent class is rewritten method abnormality
  5. Subclass can not access the parent class private things

3. The four access rights modifier

If the parent class and subclass in the same package as the following, then for as long as the members of the parent class modifiers not private Private , then the subclass can be used
if the child class and parent class is not in the same package below, you can only use the subclass parent class proteced and public -modified member

4. Keyword super

The use of super in the Java class to call the specified operating parent class:

  1. It can be used to access the super properties defined in the parent class
  2. The method can be used to call super member defined in the parent class
  3. super it is used to call the parent class constructor subclass constructor

note:

  1. Especially when the sub-members of the same name as the parent class appears, can be distinguished by super
  2. super traceability is not limited to the direct parent
  3. and similar super this usage, this representative of this class object reference to the representative identifier of the memory space of the parent class super

Call the parent class constructor

  1. All subclasses default constructors will access the parent class constructor parameter hollow
  2. When the parent is not null constructor parameter, subclass constructor must call statement specifies the parent class or classes present in the corresponding configuration by this (parameter list) or super (parameter list), and must be placed constructor the first line
  3. If neither the sub-class constructor explicitly call parent class constructor or present, and not the parent class has no argument constructor, the compiler errors
    ** ** difference between the this and super

The subclass object instantiation

5.1 Simple instantiation process object

Instantiation process 5.2 subclass object!

6. Polymorphism

6.1 There are two embodied in java

  1. Overloaded methods (overload) and rewrite (overwrite).
  2. Polymorphism objects - can be applied directly on the abstract classes and interfaces.
    Java reference variable has two types: compile-time type and run-time type . Compile-time type of the variable is declared type used in the decision, runtime type of the object assigned to the variable actual decision.
    If the run time and compile-time type inconsistent type, there have polymorphism (Polymorphism)
    polymorphic objects - in Java, an object subclass can be objects of the parent class alternative to the use of
    Person e = new Student(); // Person类型的变量e,指向Student类型的对象

6.2 upward transition (upcasting)

It can be seen as a special sub-class of the parent class, so the reference to the parent class type of the object subclass can point: upward transition (upcasting).

A reference type variable if the parent class type declarations, but actually references subclass object, then the variable will not longer have access to the properties and methods of the subclass added, because the property is determined at compile time ; compiling father from time to time class type, no sub-class member variables

Student m = new Student();
    m.school = “pku”;   //合法,Student类有school成员变量
    Person e = new Student(); 
    e.school = “pku”;   //非法,Person类没有school成员变量
      属性是在编译时确定的,编译时e为Person类型,没有school成员变量,因而编译错误。

6.3 virtual method call (Virtual Method Invocation)

E compile time for the Person type, the method is invoked at run time are determined , the call is getInfo Student class () method. - Dynamic Binding

Person e = new Student();
        e.getInfo();    //调用Student类的getInfo()方法

Summary:
Premise:

  1. There is need to implement inheritance or relationship
  2. Have covered operational

Member method:

  1. Compile-time: whether the method has been called class variable belongs to view references.
  2. Runtime: Call override the methods in the class of the actual object belongs.
    Member variables:
  3. Do not have the polymorphism, but only refer to a class variable belongs.
    Application examples polymorphism
    as arguments to call the methods declared parameter type parent class type, the object may be used subclass
public class Test{ 
    public void method(Person e) {
               //……
               e.getInfo();
    }
    public static  void main(Stirng args[]){
               Test t = new Test();
                Student m = new Student();
                t.method(m); //子类的对象m传送给父类类型的参数e
    }
}

7. Object, package

7.1 instanceof operator

        //instanceof 判断一个对象(对象的实际指向)是否是类A的子类
        Circle c2 = new Cylinder(); 
        System.out.println(c instanceof Circle);
        System.out.println(c1 instanceof Cylinder);

The main method 7.2 Object class

        //object类,是所有类的父类,在你知道形参要使用什么类的时候,可以使用object类
        Object o = new Circle();
        //object类具有的方法
        System.out.println(c1.equals(c2));//对象1.equals 对象2;判断对象的引用是否是同一个
        System.out.println(c1.equals(c));
        System.out.println(c1.equals(o));
        c1 = c2;
        //object是所有类的父类,所以所有类都可以执行object的方法
        System.out.println(c1.equals(c2));//直接将c2的引用赋给c1,是同一个引用为TRUE;
        //
        System.out.println(o.hashCode());//一串hash码//873415566
        //tostring打印对象时使用
        System.out.println(o.toString());//当前引用的对象的内存地址//com.day03.Circle@340f438e

Guess you like

Origin www.cnblogs.com/istart/p/11924598.html