接口,抽象類的一個demo

#定義一個抽象類
abstract class Ou {
        #成員變量
    private String name;
    private int age;
       #空參構造方法
    public Ou(){}  
       #有參構造方法
    public Ou(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
       #抽象方法
    public abstract  void eat();
    public abstract  void sleep();
}

#定義接口
interface Jumping{
    public void jump();
}


#定義Mimi類繼承抽象類Ou
class Mimi extends Ou{
    public Mimi(){}
    public Mimi(String name,int age){
        super(name,age);
    }
    public  void eat(){
        System.out.println("貓吃飯");
    };
    public void sleep(){
        System.out.println("貓愛睡覺");
    }
}


#定義JumpCat繼承Mimi類並實現Jumping接口
class JumpCat extends Mimi implements Jumping{
    public JumpCat(){}
    public JumpCat(String name,int age){
        super(name,age);
    }
    public void jump(){
        System.out.println("貓跳高");
    }
}


public class Test_interFace {
    public static void main(String[] args){
        Mimi m = new Mimi("加肥",8);
        m.eat();
        m.sleep();
        
        
        JumpCat jc = new JumpCat("蛋黃",4);
        jc.eat();
        jc.jump();
        jc.sleep();
        
    }
}    
相關文章
相關標籤/搜索