1. 程式人生 > >java之day8多態

java之day8多態

div override span ges err fat extend rac 單繼承

多態

package day8;

public abstract class Instrument {
    public abstract void play();
}
package day8;

public class Piano extends Instrument{

    @Override
    public void play() {
        // TODO Auto-generated method stub
        System.out.println("yueguangqu");
    }
}
package day8;

public
class Violin extends Instrument{ public void play() { // TODO Auto-generated method stub System.out.println("風吹麥浪"); } }
package day8;

//多態類題型:
//使用繼承
//子類重寫父類的方法
//父類引用子類對象

public class Artist {
//    public void make(Piano piano){
//        piano.play();
//    }
//    public void make(Violin violin){
// violin.play(); // } public void make(Instrument instrument){ instrument.play(); } }
package day8;

public class Test {
    public static void main(String[] args) {
        Artist artist = new Artist();
        Piano piano = new Piano();
        artist.make(piano);
        Violin violin 
= new Violin(); artist.make(violin); } }

================================================================================================================

接口

package day83;

public interface InterfaceDemo {
    //1:接口不能被實例化
    //2:接口裏的方法默認都是public類型的抽象方法
    //所有public abstract修飾可以忽略
    //3:類是單繼承,但是類可以實現多個接口
    //4:在接口裏定義的屬性都是public類型的靜態常量
    //public static final都是可以省略的
    //5:接口可以繼承多個接口,可以得到父級接口裏所有的屬性和方法
    
    public abstract void showName();
    
    public static final int AGE = 10;
}
package day83;

public interface InterfaceDemo2 {
    void showName2(); 
}
package day83;

public abstract class Father {
    public abstract void showFatherName();
}
package day83;

public abstract class Father2 {
    public abstract void showFatherName2();
}
package day83;

public interface ChildInterface extends InterfaceDemo,InterfaceDemo2{

}
package day83;

import java.nio.channels.ShutdownChannelGroupException;

public class Test extends Father implements InterfaceDemo,InterfaceDemo2{
    public static void main(String[] args) {
//        Interface demo = new InterfaceDemo();是不可以的
        Test test = new Test();
        
    }
    @Override
    public void showName() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void showName2() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void showFatherName() {
        // TODO Auto-generated method stub
        
    }
}

=======================================================================================================

綜合類

package day86;

public class A4Paper implements Paper{

    @Override
    public void pageSize() {
        System.out.println("使用A4紙!");
    }

}
package day86;

public class B5Paper implements Paper{
    @Override
    public void pageSize() {
        System.out.println("使用B5紙!");
    }
}
package day86;

public class BlackInk implements Ink{

    @Override
    public void inkColor() {
        System.out.println("使用黑白墨!");
    }

}
package day86;

public class ColorInk implements Ink{

    @Override
    public void inkColor() {
        System.out.println("使用彩墨!");
    }

}
package day86;

public interface Ink {
    void inkColor();
}
package day86;

public interface Paper {
    void pageSize();
}
package day86;

public class Printer {
    public Ink ink;
    public Paper paper;
    public void print(String content){
        ink.inkColor();
        paper.pageSize();
        System.out.println(content);
    }
    public static void main(String[] args) {
        Printer printer = new Printer();
        printer.ink = new BlackInk();
        printer.paper = new A4Paper();
        printer.print("我是打印內容!");
        
    }
    
}

java之day8多態