一、抽象类的概述
案例:抽象类
Test测试类部分代码
package Package1;
public class Test {
public static void main(String[] args) {
//测试狗类
Dog dog = new Dog();
dog.eat();
//测试老鼠类
Mouse mouse = new Mouse();
mouse.eat();
System.out.println("-----------------");
//通过多态来测试
Animal an = new Dog();
an.eat();
}
}
Animal类(父类(抽象类)) 部分代码:
package Package1;
//父类:动物类(抽象类)
public abstract class Animal {
//抽象方法(特点:要求子类必须重写)
public abstract void eat();
}
Dog类(子类) 部分代码:
package Package1;
//子类:狗类
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("狗吃骨头");
}
}
Mouse类(子类) 部分代码:
package Package1;
//子类:老鼠类
public class Mouse extends Animal {
@Override
public void eat() {
System.out.println("老鼠吃奶酪");
}
}
输出结果:
二、抽象类的特点
Test类部分代码:
package Package1;
public class Test {
public static void main(String[] args) {
//抽象类不能new(抽象类不能实例化)
//Animal an = Animal(); 报错!
//初始化抽象类
Animal an = new Cat();
System.out.println("--------------");
//抽象类的成员特点
an.name = "汤姆";
System.out.println(an.name);
//an.AGE = 50; //代码会报错,原因是常量的值不能发生改变
System.out.println(an.AGE);
}
}
Animal类(父类(抽象类)) 部分代码:
package Package1;
//父类:动物类(抽象类)
public abstract class Animal {
//构造方法
public Animal() {}
public Animal(String name) {
this.name = name;
}
//成员变量:其值可以变化
String name = "哈士奇";
//成员常量(一般全用大写字母),其值不能发生改变
final int AGE = 30;
//抽象方法(特点:要求子类必须重写)
//吃
public abstract void eat();
//睡
public abstract void sleep();
//非抽象方法
public void call() {
System.out.println("动物会叫");
}
}
Dog类(子类(抽象类)) 部分代码:
package Package1;
//子类:狗类
public abstract class Dog extends Animal {
}
Cat类(子类) 部分代码:
package Package1;
//子类:狗类
public class Cat extends Animal {
//子类是普通类,要重写所有的方法
@Override
public void eat() {
System.out.println("猫吃鱼");
}
@Override
public void sleep() {
System.out.println("猫卧着睡");
}
}
总结:
抽象类中的成员比普通类多一种:抽象方法
其它和普通类一样
三、案例(抽象类_员工类代码实现)
案例:定义员工之间的继承关系并使用
Test测试类部分代码
package Package;
public class Test {
public static void main(String[] args) {
//测试程序员类
Employee em = new Coder();
em.work();
//测试经理类
Employee em2 = new Manager();
em2.work();
System.out.println("--------------");
//扩展内容:快速实例化
//需求:创建一个姓名叫张三,工资为30000,工号为:研发部007的程序员
/*
Coder c = new Coder();
c.setName("张三");
c.setSalary(30000);
c.setId("研发部007");
System.out.println("姓名:"+c.getName());
System.out.println("工资:"+c.getSalary());
System.out.println("工号:"+c.getId());
*/
Coder c = new Coder("张三", 30000, "研发部007");
System.out.println("姓名:" + c.getName());
System.out.println("工资:" + c.getSalary());
System.out.println("工号:" + c.getId());
//需求:创建一个名字叫:李四,工资为:40000,研发部01,奖金为100000的经理 }
Manager m = new Manager("李四", 40000, "研发部01", 100000);
System.out.println("姓名:" + m.getName());
System.out.println("工资:" + m.getSalary());
System.out.println("工号:" + m.getId());
System.out.println("奖金为:" + m.getBonus());
}
}
Employee类(父类(抽象类)) 部分代码
package Package;
//父类:员工类
public abstract class Employee {
//成员变量
//姓名
private String name;
//工资
private double salary;
//工号
private String id;
//构造方法
//空参构造
public Employee() {
}
//全参构造
public Employee(String name, double salary, String id) {
this.name = name;
this.salary = salary;
this.id = id;
}
//getXxx(),setXxx()
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
//成员方法
//工作(抽象类)
public abstract void work();
}
Coder类(子类) 部分代码
package Package;
//子类:程序员类
public class Coder extends Employee {
//小细节:在实际开发中,子类一般都有两个构造方法
//子类的空参构造访问父类的空参构造
//子类的全参构造访问父类的全参构造
public Coder() {
super();
}
public Coder(String name, double salary, String id) {
super(name, salary, id);
}
@Override
public void work() {
System.out.println("程序员要敲代码");
}
}
Manager类(子类) 部分代码
package Package;
//子类,经理类
public class Manager extends Employee{
public Manager() {
}
public Manager(String name, double salary, String id, int bonus) {
super(name, salary, id);
this.bonus = bonus;
}
//奖金
private int bonus;
public int Bonus() {
return bonus;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("经理看程序员敲代码");
}
}
输出结果: