抽象类必须使用abstract修饰:
修饰符 abstract class 类名{ }
抽象方法:
就是抽象类中定义的子类必须完成的功能的基本要求。
没有方法体,只有方法签名,必须 abstract修饰
例子:
父类:
package abstract_class;
public abstract class Animal {
public abstract void run();
}
子类1:
package abstract_class;
public class Tiger extends Animal{
@Override
public void run() {
System.out.println("老虎跑得快");
}
}
子类2
package abstract_class;
public class Dog extends Animal{
@Override
public void run() {
System.out.println("狗跑得快");
}
//"Alt"+"回车"
}
测试代码:
package abstract_class;
public class Text {
public static void main(String[] args) {
Tiger t = new Tiger();
t.run();
Dog t1 = new Dog();
t1.run();
}
}
输出:
E:\develop\Java\jdk-17\bin\java.exe "-javaagent:E:\idea\IntelliJ IDEA Community Edition 2023.3.4\lib\idea_rt.jar=64230:E:\idea\IntelliJ IDEA Community Edition 2023.3.4\bin" -Dfile.encoding=UTF-8 -classpath E:\code\stduy1\out\production\stduy1 abstract_class.Text
老虎跑得快
狗跑得快
Process finished with exit code 0
标签:run,package,abstract,class,案例,void,抽象类,public
From: https://blog.csdn.net/2301_81147503/article/details/137156765