定义
注意事项:
抽象类和抽象方法的注意事项:
1.抽象类不能被实例化
2.抽象类不一定有抽象方法,但是有抽象方法的类一定是抽象类
3.可以有构造方法(主要为其子类实例化对象用的)
4.抽象类的子类
要么重写抽象类中所有的抽象方法
要么本身也是抽象类
总结:
练习:
java
package Test;
/**
* @ClassName Animal
* @author gyf
* @Date 2024/8/21 10:26
* @Version V1.0
* @Description :
*/
public abstract class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(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 void drink(){
System.out.println("动物在喝水");
}
public abstract void eat();
}
java
public class Frog extends Animal{
public Frog() {
}
public Frog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("青蛙再吃虫子");
}
}
java
public class Dog extends Animal{
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("狗在吃骨头");
}
}
java
public class Sheep extends Animal{
public Sheep() {
}
public Sheep(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("山羊在吃草");
}
}