抽象类

来源:互联网 发布:中文安卓编程王者荣耀 编辑:程序博客网 时间:2024/05/22 17:41

抽象类
关键字:abstract,
有抽象方法的类一定是抽象类,但是抽象类不一定有抽象方法。当抽象类里所有方法都是抽象方法,则称为接口(接口中定义的属性必须也一定是public static final 类型,即都是常量,不能修改)
由来:
不确定某个方法的具体实现细节,
将方法设置为abstract类型,则类也要设成abstract类型
抽象类特征:
1.不能被实例化
2.有抽象方法的类一定是抽象类
3.抽象类可以没有抽象方法
目的:
1.设计出父类,供子类继承,实现代码重用,同时给子类提供了模板
2. 设计类需要实现的功能(某些功能不具体),由派生类来具体实现
3. 为了保护类的数据和方法,将类设为abstract类型,
只有派生类才能创建对象
注意:
abstract只能修饰类和方法
不能和static联用
不能和private联用
案例
抽象类教师(Teacher)
教师
子类1:MusicTeacher 继承Teacher, 实现teachProcedure()方法
子类2:softwareTeacher 继承Teacher,实现实现teachProcedure()方法
测试类1:TestMusicTeacher
测试类2:TestsoftwareTeacher
代码:
Teacher类

public abstract class Teacher {    //属性    private int id;    private String name;    private String sex;    private int age;    private String education;    private String teacherTitile;    //方法    public void startWork(int time){        System.out.println(this.name + time + "上班");    }    public void offWork(int time){        System.out.println(this.name + time + "下班");    }    public void teach(String course){        System.out.println(this.name  + "教" + course);    }    //构造方法    public Teacher(int id, String name, String sex, int age, String education,            String teacherTitile) {        super();        this.id = id;        this.name = name;        this.sex = sex;        this.age = age;        this.education = education;        this.teacherTitile = teacherTitile;    }    //get,set    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getEducation() {        return education;    }    public void setEducation(String education) {        this.education = education;    }    public String getTeacherTitile() {        return teacherTitile;    }    public void setTeacherTitile(String teacherTitile) {        this.teacherTitile = teacherTitile;    }    abstract public void teachProcedure();    @Override    //ToString方法    public String toString() {        return "Teacher [id=" + id + ", name=" + name + ", sex=" + sex                + ", age=" + age + ", education=" + education                + ", teacherTitile=" + teacherTitile + "]";    }   }

MusicTeacher 类

public class MusicTeacher extends Teacher {    public MusicTeacher(int id, String name, String sex, int age,            String education, String teacherTitile) {        super(id, name, sex, age, education, teacherTitile);    }    @Override    public void teachProcedure() {        System.out.println("先唱歌,后弹琴");    }}

SoftwareTeacher类

public class SoftwareTeacher extends Teacher {    public SoftwareTeacher(int id, String name, String sex, int age,            String education, String teacherTitile) {        super(id, name, sex, age, education, teacherTitile);            }    @Override    public void teachProcedure() {        System.out.println("先java基础,后web");         }}

TestMusicTeacher类

public class TestMusicTeacher {    public static void main(String[] args) {        // TODO Auto-generated method stub        Teacher musicTeacher = new MusicTeacher(1, "张三", "男", 0, "音乐", "教授");        musicTeacher.startWork(9);        musicTeacher.offWork(17);        System.out.println(musicTeacher);        musicTeacher.teachProcedure();    }}

TestSoftwareTeacher 类

public class TestSoftwareTeacher {    public static void main(String[] args) {        Teacher softTeacher = new SoftwareTeacher(2, "李四", "女", 25, "软件老师", "教授");        System.out.println(softTeacher);        softTeacher.startWork(9);        softTeacher.offWork(17);        softTeacher.teachProcedure();    }}
0 0
原创粉丝点击