【自学java的小白】对抽象类的总结

对于Java抽象类的总结

  1. 在 java 中采用 abstract 关键字定义的类就是抽象类,采用 abstract 关键字定义的方法就是抽象方法
  2. 抽象的方法只需在抽象类中,提供声明,不需要实现
  3. 如果一个类中含有抽象方法,那么这个类必须定义成抽象类
  4. 如果这个类是抽象的,那么这个类被子类继承,抽象方法必须被重写。如果在子类中不复写该抽象方法,那么必须将此类再次声明为抽象类
  5. 抽象类不能被实例化,实例化的工作应该交由它的子类来完成,它只需要有一个引用即可。
  6. 抽象类不能被 final 修饰,即abstract不能与final并列修饰同一个类。
  7. 抽象方法不能被 final修饰,因为抽象方法就是被子类实现的
  8. 只要包含一个抽象方法的抽象类,该方法必须要定义成抽象类,不管是否还包含有其他方法。
  9. abstract 不能与private、static、final或native并列修饰同一个方法。
  10. 抽象类中可以包含具体的方法,当然也可以不包含抽象方法。
  11. 抽象类中的抽象方法不含有方法体,但其他方法可以拥有方法体

实例

public abstract class Weapon {
    public abstract void attack();
    public abstract void move();
}

class Tank extends Weapn {
    public void attack() {
        System.out.println("坦克进攻!");
    }

    public void move() {
        System.out.println("坦克撤退!");
    }
}

class Flighter extends Weapon{

    public void attack() {
        System.out.println("战士进攻!");
    }

    public void move() {
        System.out.println("战士撤退!");
    }
}

class WarShip extends Weapon{

    public void attack() {
        System.out.println("战舰进攻!");
    }

    public void move() {
        System.out.println("战舰撤退!");
    }
}

//测试
public class Test01 {
    public static void main(String[] args) {
        Weapon w1 = new Flighter();
        w1.attack();
        w1.move();

        Weapon w2 = new Tank();
        w2.attack();
        w2.move();

        Weapon w3 = new WarShip();
        w3.attack();
        w3.move();
    }
}
//输出
战士进攻!
战士撤退!
坦克进攻!
坦克撤退!
战舰进攻!
战舰撤退!

1、采用 abstract 声明抽象类

public class Test {

    public static void main(String[] args) {
//不能实例化抽象类
//抽象类是不存在的,抽象类必须有子类继承
        //Person p = new Person();
//以下使用是正确的,因为我们new的是具体类
        Person p1 = new Employee();
        p1.setName("张三");
        System.out.println(p1.getName());
    }
}

//采用 abstract 定义抽象类
//在抽象类中可以定义一些子类公共的方法或属性
// 这样子类就可以直接继承下来使用了,而不需要每个子类重复定义
abstract class Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
	
	/*抽象类中的抽象方法不能拥有方法体
	public abstract void commonMethod1() {
        System.out.println("---------commonMethod1-------");
    }*/
    
    //此方法各个子类都可以使用
    public void commonMethod1() {
        System.out.println("---------commonMethod1-------");
    }
}

class Employee extends Person {
}

class Student extends Person {
}

2、抽象的方法只需在抽象类中,提供声明,不需要实现,起到了一个强制的约束作用,要求子类必须实现

public class Test02 {
    public static void main(String[] args) {
//Person p = new Employee();
Person p = new Student();
//Person p = new Person();
        p.setName("张三");
        p.printInfo();
    }
}

abstract class Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    //此方法各个子类都可以使用
    public void commonMethod1() {
        System.out.println("---------commonMethod1-------");
    }

    /*public void printInfo() {
        System.out.println("------Person.printInfo()--------");
    }*/
//采用 abstract 定义抽象方法
//如果有一个方法为抽象的,那么此类必须为抽象的
//如果一个类是抽象的,并不要求具有抽象的方法
    public abstract void printInfo();
}

class Employee extends Person {
    //必须实现抽象的方法
    public void printInfo() {
        System.out.println("Employee");
    }
}

class Student extends Person {
    //必须实现抽象的方法
    public void printInfo() {
        System.out.println("Student");
    }
}

3、如果这个类是抽象的,那么这个类被子类继承,抽象方法必须被覆盖。如果在子类中不覆盖该抽象方法,那么必须将此方法再次声明为抽象方法

public class Test03 {
    public static void main(String[] args) {
//此时不能再 new Employee 了
        //Person p = new Employee();
        Person p = new Student();
    }
}

abstract class Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    //此方法各个子类都可以使用
    public void commonMethod1() {
        System.out.println("---------commonMethod1-------");
    }

    //采用 abstract 定义抽象方法
    public abstract void printInfo();
}

abstract class Employee extends Person {
    //再次声明该方法为抽象的
    public abstract void printInfo();
}

class Student extends Person {
    //实现抽象的方法
    public void printInfo() {
        System.out.println("Student");
    }
}

4、抽象类不能被 final 修饰

public class Test04 {
    public static void main(String[] args) {
    }
}

//不能采用 final 修改抽象类
//两个关键字是矛盾的
//final abstract class Person {
abstract class Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    //此方法各个子类都可以使用
    public void commonMethod1() {
        System.out.println("---------commonMethod1-------");
    }

    //采用 abstract 定义抽象方法
    public abstract void printInfo();
}

class Employee extends Person {
    //实现抽象的方法
    public void printInfo() {
        System.out.println("Employee");
    }
}

class Student extends Person {
    //实现抽象的方法
    public void printInfo() {
        System.out.println("Student");
    }
}

5、抽象方法不能被 final 修饰

public class Test05 {
    public static void main(String[] args) {
    }
}

abstract class Person {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    //此方法各个子类都可以使用
    public void commonMethod1() {
        System.out.println("---------commonMethod1-------");
    }
    //不能采用 final 修饰抽象的方法
    //这两个关键字存在矛盾
    //public final abstract void printInfo();
    public abstract void printInfo();
}
class Employee extends Person {
    //实现抽象的方法
    public void printInfo() {
        System.out.println("Employeee");
    }
}
class Student extends Person {
    //实现抽象的方法
    public void printInfo() {
        System.out.println("Student");
    }
}

本篇文章为本人自己总结,如有错误或遗漏之处请大家批评指正。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值