码迷,mamicode.com
首页 > 编程语言 > 详细

java之day8多态

时间:2017-08-24 23:52:33      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:end   dem   方法   java   shutdown   重写   int   pex   child   

 

多态

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多态

标签:end   dem   方法   java   shutdown   重写   int   pex   child   

原文地址:http://www.cnblogs.com/haloxinghong/p/7425552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!