自学内容网 自学内容网

Java(面向对象进阶——抽象类)

抽象方法、抽象类

 

 

 练习

package day10.抽象类和抽象方法;

public  abstract class animals {
    private String name;
    private int age;
    public animals(){

    }
    public animals(String name,int age){
        this.name=name;
        this.age=age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public abstract void eat(String something);
    public void drink(){
        System.out.println("喝水");
    }
}
package day10.抽象类和抽象方法;

public class frog extends animals {
    public frog(){

    }
    public frog(String name,int age){
        super(name,age);
    }
    @Override
    public void eat(String something) {
        System.out.print("青蛙吃"+something);
    }
}
package day10.抽象类和抽象方法;

public class dog extends animals{
    public dog(){

    }
    public dog(String name,int age){
        super(name,age);
    }
    @Override
    public void eat(String something){
        System.out.print("狗吃"+something);
    }
}

 

 

package day10.抽象类和抽象方法;

public class sheep extends animals{
    public sheep(){

    }
    public sheep(String name,int age){
        super(name,age);
    }
    @Override
    public void eat(String something){
        System.out.print("山羊吃"+something);
    }
}
package day10.抽象类和抽象方法;

public class test {
    public static void main(String[] args) {
        frog f=new frog("小青",1);
        dog d=new dog("小黄",3);
        sheep s=new sheep("喜羊羊",12);
        f.eat("虫子");
        f.drink();
        d.eat("骨头");
        d.drink();
        s.eat("青草");
        s.drink();
    }
}

 

 小结

 


原文地址:https://blog.csdn.net/weixin_68931360/article/details/145212759

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!