抽象类、接口的具体用法

来源:互联网 发布:为什么淘宝退款打不开 编辑:程序博客网 时间:2024/05/01 22:22
package com.afterclaa;public class Demo_4_chouxianglei {    public static void main(String[] args) {        // TODO Auto-generated method stub        //抽象类的用法        Rose rose=new Rose();        Lily lily=new Lily();        rose.introduce();//非抽象方法        rose.get_name();        rose.get_color();        rose.get_smell();        lily.introduce();        lily.get_name();        lily.get_color();        lily.get_smell();        //接口的用法        Peony peony=new Peony();        System.out.println("num="+Fow.num);//这里也可以用peony.num进行访问        peony.get_name();        peony.get_color();        peony.get_smell();    }}//创建一个抽象类abstract class Flower{    abstract void get_name();    abstract void get_smell();    abstract void get_color();    public void introduce(){        System.out.println("我是一朵花");    }}//创建一个玫瑰花类class Rose extends Flower{    void get_name(){        System.out.println("我是一朵玫瑰花");    }    void get_smell(){        System.out.println("我有玫瑰香");    }    void get_color(){        System.out.println("我是红色");    }}//创建一个百合花类class Lily extends Flower{    void get_name(){        System.out.println("我是一朵百合花");    }    void get_smell(){        System.out.println("我有百合香");    }    void get_color(){        System.out.println("我是白色");    }}interface Fow1{    //接口中的方法默认为public,所以在下面的类中的成员方法钱要加public    void get_name();}interface Fow2{    void get_color();}//可以同时继承和实现多个接口interface Fow extends Fow1,Fow2{    //接口也可以添加新的属性和方法    void get_smell();    //注意:接口中的数据成员是final常量,它的值不能被改变。    int num=100;}class Peony implements Fow{    public void get_name(){        System.out.println("我是一朵牡丹花");    }    public void get_color(){        System.out.println("我是粉红色");    }    public void get_smell(){        System.out.println("我有牡丹花香味");    }}

运行结果:
我是一朵花
我是一朵玫瑰花
我是红色
我有玫瑰香
我是一朵花
我是一朵百合花
我是白色
我有百合香
num=100
我是一朵牡丹花
我是粉红色
我有牡丹花香味

0 0