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();
}
}
小结