Java programming ideas --- Chapter VIII polymorphism (on)

Chapter VIII   polymorphism (on)

  In object-oriented programming language, it is the third essential feature polymorphism data after the inheritance and abstraction.

  Polymorphic by separating what to do and how to do it from another point of interface and implementation separated, multi-state can not only improve the organizational structure and readability of the code, but also the ability to create scalable program, regardless of when the project was originally created or when you need to add new functions can be grown program. Package to create new data types by combining the characteristics and behaviors.

 

8.1 Again on the upward transition

 

  In the last chapter we already know, the object can be used as its own type of use, you can also use as its base class, and that the reference to an object is deemed to be references to the practice known as its base class upward transition.

//: demo/Note.java

package com.example.demo;

public enum Note {
    MIDDLE_C, C_SHARP, B_FLAT;
}

//: demo/Instrument.java
package com.example.demo;
public class Instrument { public void play(Note n) { System.out.println("Instrument.play()"); } } //: demo/Wind.java package com.example.demo;
public class Wind extends Instrument { public void play(Note n) { System.out.println("Wind.play()" + n); } } //: demo/Music.java package com.example.demo; public class Music { public static void tune(Instrument i) { i.play(Note.MIDDLE_C); } public static void main(String[] args) { Wind flute = new Wind(); tune(flute); } }

  The output is:

  Wind.play()MIDDLE_C

 

  Derived class can accept all the base class interface, the interface may make the transition up "zoom out", but not all narrower than the base class interface.

 

8.1.1 forget Object Type

 

package com.example.demo;

class Stringed extends Instrument {
    public void play(Note n) {
        System.out.println("Stringed.play() " + n);
    }
}

class Brass extends Instrument {
    public void play(Note n) {
        System.out.println("Brass.play() " + n);
    }
}

public class Music2{
    public static void tune(Wind i) {
        i.play(Note.MIDDLE_C);
    }

    public static void tune(Stringed i) {
        i.play(Note.MIDDLE_C);
    } 

    public static void tune(Brass i) {
        i.play(Note.MIDDLE_C);
    }

    public static void main(String[] args) {
        Wind flute = new Wind();
        Stringed violin = new Stringed();
        Brass frenchHorn = new Brass();
        tune(flute);
        tune(violin);
        tune(frenchHorn);
    }
}

 

  The output is:

Wind.play()MIDDLE_C

Stringed.play() MIDDLE_C

Brass.play() MIDDLE_C

 

  If we forget to reload a method, the compiler will not return any error messages, so on the whole process becomes difficult type of manipulation. If we just write a simple method that takes only the base class as a parameter, rather than a special derived class, it would be better? This is more than allowed by state, but most programmers have a process-oriented programming background, multi-state mode of operation may be a little confused.

 

8.2 Transit

 

8.2.1 Method Invocation binding

 

  One method calls the same method body is called binding associates. If the binding before the program execution, called early binding. The reason why the above program puzzling, mainly because of early binding, when the compiler is only one Instrument referencing, it is impossible to know exactly which method to call my son. The solution is late binding, meaning he is bound according to the type of object at runtime.

  Java in addition to the static method and the final addition to the method, all other methods are late binding, which means that under normal circumstances we do not have to determine whether it should perform late binding, it will happen automatically. Call the final can prevent others from covering the method, but the more important point is that this can be effectively closed dynamic binding, so the compiler can that final call to generate more efficient code,

 

8.2.2 produce the correct behavior

 

  Once you know Java after all methods are multi-state by dynamic binding this is true, we can write code only program dealing with the base class, and the code can run correctly on all export categories.

  In this example geometry, there is a base class Shape and deriving a plurality of classes, inheritance graph showing the following relationship between them:

 

 

 

  Up transformation can be as simple as the following statement:

Shape s = new Circle();

 

  Here creates a Circle object and get a reference assigned to the Shape , it seems wrong but in fact there is no problem, because through inheritance, Circle is a kind of the Shape .

  Suppose you call a method of the base class, and it is covered in the derived class:

s.draw();

  You might think that once again calls is Shape 's draw () method, because it is, after all, a Shape reference, then the compiler is how to know what to do other things? Since late binding, which is polymorphic, or called correctly Circle.draw () method.

  Shape base class to inherit from it comes, that is to say, all the Acts can be depicted and delete, export categories covered by these definitions to provide individual behavior for each particular type of geometry.

 

8.2.3 Scalability

 

  Now we return to the instrument example. Because of how state mechanism, we can add as many new types of systems according to their needs, without the need for the tune () method. In a well-designed OOP program, most of the methods will follow Tune () of the model, and only communication with the base interface. Scalable when such a program, as can inherit from a common base class to add new data types some functionality, those grass base interface does not require any changes to be applied to the new class.

  我们向乐器的基类添加更多的方法,并加入一些新类:

 

 

 

  事实上,不需要改动tune()方法,所有新类都能与原有类一起正确运行,及时tune()是单独放在某个文件中,并且在Instrument接口中添加了其他的新方法,tune()也不需要编译就能正确运行。下面是上图的具体实现:

 

Guess you like

Origin www.cnblogs.com/parable/p/11470203.html