Java programming ideas --- Chapter IX Interface (on)

Chapter IX  Interface

  Interface and internal class we provided a method for the interface and implementation separation of the more structured.

 

9.1 abstract classes and abstract methods

 

  Java provides a mechanism called abstract method, this mechanism is not complete, but not the only method body, an abstract method declaration syntax is as follows:

abstract void f();

  The method comprising abstract class called abstract class, if a class contains one or more abstract methods, must be defined as an abstract class, the compiler will otherwise given. If an abstract class is incomplete, then when we try to produce objects of the class, because of insecurity when you create an object of abstract class, so we'll get an error message from the compiler where the compiler will ensure the purity of the abstract class we do not have to worry about misuse it.

  If you inherit from an abstract class, and want to create objects of the class, it must provide a way to define not if you do not then it is a derived class abstract class is the base class for all abstract methods, and the compiler will force us to use abstract keyword this class is defined.

  Previous chapter Instrument class can easily be converted into abstract class since become a class abstract class so not all methods are abstract, so that only certain methods can be declared abstract, as shown below:

 

  We modify the above example orchestra:

package com.example.demo;

abstract class Instrument4 {
    private int i;
    public abstract void play(Note n);
    public String what() { return "Instrument4"; }
    public abstract void adjust();
}

class Wind4 extends Instrument4 {
    public void play(Note n) {
        System.out.println("Wind4.play() " + n);
    }
    public String what() { return "Wind4"; }
    public void adjust() {}
}

class Percussion4 extends  Instrument4 {
    public void play(Note n) {
        System.out.println("Percussion4.play() " + n);
    }
    public String what() { return "Percussion4"; }
    public void adjust() {}
}

class Stringed4 extends  Instrument4 {
    public void play(Note n) {
        System.out.println("Stringed4.play() " + n);
    }
    public String what() { return "Stringed4"; }
    public void adjust() {}
}

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

class Woodwind4 extends Wind4 {
    public void play(Note n) {
        System.out.println("Woodwind4.play() " + n);
    }
    public String what() {
        return "Woodwind";
    }
}

public class Music4 {
    static void tune(Instrument4 i) {
        i.play(Note.MIDDLE_C);
    }
    static void tuneAll(Instrument4[] e) {
        for(Instrument4 i : e)
            tune(i);
    }
    public static void main(String[] args) {
        Instrument4[] orchestra = {
                new Wind4(),
                new Percussion4(),
                new Stringed4(),
                new Brass4(),
                new Woodwind4()
        };
        tuneAll(orchestra);
    }
}

 

The output is:

Wind4.play() MIDDLE_C

Percussion4.play() MIDDLE_C

Stringed4.play() MIDDLE_C

Brass4.play() MIDDLE_C

Woodwind4.play() MIDDLE_C

 

  We can see that in addition to the base class, in fact, nothing changed.

  Create abstract classes and abstract methods are very useful because they can make the abstract class of clear up, and tells the user how and compilers intend to use them.

 

9.2 Interface

 

  Interface keywords make abstract concepts more step to move forward, abstract allows us to create one or more do not have any method defined in the class, which provides an interface part, but did not provide any corresponding specific implementations that are made such successor created. Interface produces a completely abstract class keyword, it does not provide any specific implementation that allows the creator to determine the method name, return type and parameter list, but there is no way, with interface only provides form, without any specific implementation. But Interface is not just an extremely abstract class because it allows people to achieve some similar characteristics by creating multiple variants, following a transition can be up to a variety of types of the base class.

  To create an interface, you need to use interface keyword instead of class keyword, like classes, can interface plus the previous public key, if you do not add the public only the package access. Interface may also comprise domains, these domains are implicitly static and final of.

  To make a class to follow a particular interface requires implement keyword, it is said to be on in this class interface specific implementation inheritance looks like:

 

  You can see, once implements an interface to its implementation becomes an ordinary class, you can expand it in the usual way. Can choose to display in the interface method declared as public , but even if you do not do them is public , otherwise if they will only get the default package access, which methods are inherited in the process, their access to It is reduced, which is the Java compiler not allowed.

 

9.3 completely decoupled

 

  As long as a method of operation when the class rather than the interface, then you can only use this class and its subclasses, if you want this method is applied to this inheritance structure is not a class, then you will come to grief up. Interface can relax this limit to a large extent.

  Create a method can have different behavior depending on the parameter object passed, called the strategy design pattern, such methods include algorithms to be executed in the fixed part and change policies that contain part of strategy is parameter passing in an object that contains the code to be executed.

 

9.4 Java 中的多重继承

 

  接口不仅仅只是一种更纯粹形式的抽象类,它的目标比这要高,因为接口是根本没有任何具体实现的,也就是说没有任何与接口相关的存储,因此也就无法阻止多个接口的组合。在C++中,组合多个类的接口的行为被称作多重继承,在Java中你可以执行同样的行为,但是只有一个类可以有具体实现。

 

  在导出类中不强制要求必须有一个是抽象的或具体的基类,如果要从一个非接口的类继承,那么只能从一个类去继承,其余的基元素都必须是接口,需要将所有的接口名都置于implements关键字之后,用都好一一隔开,可以继承任意多个接口,并向上转型为每个接口,因为每一个接口都是一个独立类型。

 

package com.example.demo;

interface CanFight {
    void fight();
}

interface CanSwim {
    void swim();
}

interface CanFly {
    void fly();
}

class ActionCharacter {
    public void fight() {}
}

class Hero extends ActionCharacter
        implements CanFight, CanSwim, CanFly {
    public void swim() {}
    public void fly() {}
}

public class Adventure {
    public static void t(CanFight x) {x.fight();}
    public static void u(CanSwim x) {x.swim();}
    public static void v(CanFly x) {x.fly();}
    public static void w(ActionCharacter x) {x.fight();}
    public static void main(String[] args) {
        Hero h = new Hero();
        t(h);
        u(h);
        v(h);
        w(h);
    }
}

 

  可以看到,Hero组合了具体类ActionCharacter和接口CanFight、CanSwim、CanFly ,当通过这种方式将一个具体类和多个接口组合在一起时,这个具体类必须放在前面,后面跟着的才是接口。

Guess you like

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