当父类的某些方法需要声明,但是又不确定如何实现时,可以将其声明为抽象方法,那么这个类就是抽象类 。
抽象方法就是没有实现的方法(即没有方法体)
当一个类中有抽象方法时,需要同时将类声明为抽象类
抽象类会被继承,由子类来实现抽象方法
abstract class Person{ public abstract void function(); }
package com.tzk.abstract_; public class AbstractDetail01 { public static void main(String[] args) { //抽象类,不能被实例化 //new A(); 错误 } } //抽象类不一定要包含 abstract 方法。也就是说,抽象类可以没有 abstract 方法 //还可以有实现的方法。 abstract class A { public void hi() { System.out.println("hi"); } } //一旦类包含了 abstract 方法,则这个类必须声明为 abstract abstract class B { public abstract void hi(); } //abstract 只能修饰类和方法,不能修饰属性和其它的 class C { // public abstract int n1 = 1; 错误 }
package com.tzk.abstract_; public class AbstractDetail02 { public static void main(String[] args) { System.out.println("hello"); } } //抽象方法不能使用 private、final 和 static 来修饰,因为这些关键字都是和重写相违背的 abstract class H { public abstract void hi();//抽象方法 } //如果一个类继承了抽象类,则它必须实现抽象类的所有抽象方法,除非它自己也声明为 abstract 类 abstract class E { public abstract void hi(); } abstract class F extends E { } class G extends E { @Override public void hi() { //这里相等于 G 子类实现了父类 E 的抽象方法,所谓实现方法,就是有方法体 } } //抽象类的本质还是类,所以可以有类的各种成员 abstract class D { public int n1 = 10; public static String name = "tzk"; public void hi() { System.out.println("hi"); } public abstract void hello(); public static void ok() { System.out.println("ok"); } }
题1,思考: abstract final class A{} 能编译通过吗,why?错误,final是不能继承
题2,思考: abstract public static void test2();
能编译通过吗,why?错误,static关键字和方法重写无关.
题3,思考: abstract private void test3();能编译通过吗,why?错误,private的方法不能重写
编写一个Employee类,声明为抽象类,包含如下三个属性: name, id, salary.
提供必要的构造器和抽象方法: work(). 对于Manager类来说,他既是员工,还
具有奖金(bonus)的属性。请使用继承的思想,设计CommonEmployee类和
Manager类,要求类中提供必要的方法进行属性访问,实现work(),提示"经理/普
通员工名字工作中.... OOP的继承+抽象类
package com.tzk.abstract_; public class AbstractExercise01 { public static void main(String[] args) { //测试 Manager jack = new Manager("jack", 999, 50000); jack.setBonus(8000); jack.work(); CommonEmployee tom = new CommonEmployee("tom", 888, 20000); tom.work(); } } abstract public class Employee { private String name; private int id; private double salary; public Employee(String name, int id, double salary) { this.name = name; this.id = id; this.salary = salary; //将 work 做成一个抽象方法 public abstract void work(); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } public class Manager extends Employee { private double bonus; public Manager(String name, int id, double salary) { super(name, id, salary); } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public void work() { System.out.println("经理 " + getName() + " 工作中..."); } } public class CommonEmployee extends Employee{ public CommonEmployee(String name, int id, double salary) { super(name, id, salary); } @Override public void work() { System.out.println("普通员工 " + getName() + " 工作中..."); } }