Think In Java notes (five)

Polymorphism And Interface

1. method binding. Before the program runs, the use of binding, we call pre-bundled. There are dynamic binding, java which uses dynamic binding effect only when the program is running. Java, all methods are dynamic binding, in addition to static and final as well as private methods. If the parent class has a method a, subclass does not cover a, then by upcast, call a (), will call the parent class method, if covered, a sub-category method is called.

Exercise 2

package polymorphism;

public class Circle extends Shape{
    public void draw() {System.out.println("Circle.draw()");}
    public void erase() {System.out.println("Circle.erase()");}
    public void print() {System.out.println("this is circle print");
    }
}
/*output:Triagnle.draw()
this is print
Triagnle.draw()
this is print
Square.draw()
this is print
Triagnle.draw()
this is print
Square.draw()
this is print
Triagnle.draw()
this is print
Square.draw()
this is print
Triagnle.draw()
this is print
Circle.draw()
this is circle print
 */

 Exercise10

package polymorphism;

public class Exercise10 {
    public static void main(String[] args) {
        One t = new Two();
        t.a(); //output:this is two b 
    }
}

class One{
    void a(){
        b();
    }
    void b() {
        System.out.println("this is a");
    }
}

class Two extends One{
    void b() {
        System.out.println("this is two b");
    }
}

2. When a subclass upcast reference to the parent class, all data fields will not follow the rules of multi-state, call the reference to the parent class will directly call the data field of the parent class, not the subclass. So pay attention, it is best not to name the variable data fields and subclasses of the parent class is set as, or set to private, so that the parent class data fields hidden.

3. The actual sequence constructor call, after call parent class constructors, methods are all call will also be overriden. The next is the other initialization.

Interface

1. abstract method (abstract method), once a class has abstract methods, then this class must be declared as an abstract class, abstract class can not be instantiated because the abstract class itself is unfinished class. If you inherit from an abstract class, it must implement all of the abstract methods. Note that even if your class is not abstract methods, may also be declared abstract (abstract) (ie this type can not be instantiated). 03 can be seen from practice, when the constructor calls an abstract parent class, if the constructor calls the parent class abstract method, then automatically complete method (if the method is correct override in a subclass) subclass call .

Exercise02

package interfaces;

public class Exercise02 { // won't work
    public static void main(String[] args) {
        A a = new A();//cannot initialize abstract class of A
    }
}

abstract class A{
    void b(){
        System.out.println("this is A");
    }
}

Exercise03

package interfaces;

public class Exericse03 extends X{
    private int t = 1;
    void print(int x){
        t = x;
        System.out.println(t);
    }
    public static void main(String[] args) {
        new Exericse03().print(3);; //output:2,3
    }
}

abstract class X{
    X(){
        print(2);
    }
    abstract void print(int i);
}

 4. The interface may comprise a data field, but must be static , Final . Inheritance can be any number of interfaces, connecting with a comma behind implements, note that if the interface when the non-inherited, inherited only one. If you need to inherit from an entity class (non-Interface) interfaces and class, entity class needs EDITORIAL.

Practical application, select the interface class or abstract class? If you want to create a no -defined methods or member variables of a base class, then use the interface class.

In addition, because java does not support multiple inheritance, so no diamond inheritance problems, although the interface can be multiple inheritance, however, note that the interface is no member variables, and all methods are highly abstract, methods were not realized.

Examples of the interface in the domain automatic static final. Interface mosaic, when all interfaces must be public. If the interface is included in a class, private outer interface is not implemented in the class.

The method of using the interface and factory interfaces may be multiplexed. as follows:

class XX extends YY implements AA,BB,CC...NN
import static net.mindview.util.Print.*;
interface Game { boolean move(); }
interface GameFactory { Game getGame(); }
class Checkers implements Game {
private int moves = 0;
private static final int MOVES = 3;
public boolean move() {
print("Checkers move " + moves);
return ++moves != MOVES;
}
}
class CheckersFactory implements GameFactory {
public Game getGame() { return new Checkers(); }
}
class Chess implements Game {
private int moves = 0;
private static final int MOVES = 4;
public boolean move() {
print("Chess move " + moves);
return ++moves != MOVES;
}
}
class ChessFactory implements GameFactory {
public Game getGame() { return new Chess(); }
}
public class Games {
public static void playGame(GameFactory factory) {
Game s = factory.getGame();
while(s.move())
;
}
public static void main(String[] args) {
playGame(new CheckersFactory());
playGame(new ChessFactory());
}
} /* Output:
Checkers move 0
Checkers move 1
Checkers move 2
Chess move 0
Chess move 1
Chess move 2
Chess move 3
*///:~

 

Guess you like

Origin www.cnblogs.com/waytek/p/11121817.html