• 6-7抽象类和抽象方法


    随着继承层次中一个个新子类的定义,类变得越来越具体,而父类则更一
    般,更通用。类的设计应该保证父类和子类能够共享特征。有时将一个父
    类设计得非常抽象,以至于它没有具体的实例,这样的类叫做抽象类

    在这里插入图片描述

    abstract 关键字的使用

    1.abstract:抽象的
    2.abstract 可以用来修饰的结构:类、方法
    3.abstract 修饰类:抽象类

    此类不能实例化
    抽象类中一定有构造器,便于子类实例化时调用(涉及:子类对象实例化全过程)
    开发中,都会提供抽象类的子类,让子类对象实例化,实现相关的操作

    4.abstract 修饰方法:抽象方法

    抽象方法,只有方法的声明,没有方法体。
    包含抽象方法的类,一定是一个抽象类。反之,抽象类中可以没有抽象方法
    若子类重写了父类中所有的抽象方法,此子类方可实例化
    若子类没有重写了父类中所有的抽象方法,此子类也是一个抽象类

    abstract 使用上的注意点:

    1.abstract 不能用来修饰变量、代码块、构造器;
    2.abstract 不能用来修饰私有方法、静态方法、final 的方法、final 的类。

    在这里插入图片描述

    不能实例化

    abstract class Person {
        String name;
        int age;
    
        public Person(){
    
        }
    
        public Person(String name,int age){
            this.name = name;
            this.age = age;
        }
    
        //不是抽象方法
    	public void eat(){
    		System.out.println("人吃饭");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
        public static void main(String[] args) {
            //一旦 Person 类抽象了,就不可实例化
    		Person p1 = new Person();
    		p1.eat();
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    抽象类中一定有构造器,便于子类实例化时调用(涉及:子类对象实例化全过程)

    class Student extends Person{
        public Student(String name,int age){
            super(name,age);
        }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    抽象方法:

        //不是抽象方法,只是空方法体
    	public void eat(){}
    	//抽象方法,只有方法的声明,没有方法体
    	public abstract void eat();
    
    • 1
    • 2
    • 3
    • 4

    包含抽象方法的类一定是一个抽象类,反之,抽象类中可以没有抽象方法

    abstract class Person {
    public abstract void eat();
    }
    
    • 1
    • 2
    • 3

    完整代码:

    public class AbstractTest {
        public static void main(String[] args) {
            //一旦 Person 类抽象了,就不可实例化
    //		Person p1 = new Person();
    //		p1.eat();
    
        }
    }
    
    abstract class Creature{
        public abstract void breath();
    }
    
    abstract class Person extends Creature{
        String name;
        int age;
    
        public Person(){
    
        }
    
        public Person(String name,int age){
            this.name = name;
            this.age = age;
        }
    
        //不是抽象方法
    //	public void eat(){
    //		System.out.println("人吃饭");
    //	}
    
        //抽象方法
        public abstract void eat();
    
        public void walk(){
            System.out.println("人走路");
        }
    }
    
    class Student extends Person{
        public Student(String name,int age){
            super(name,age);
        }
        public void eat(){
            System.out.println("学生应该多吃有营养的。");
        }
        @Override
        public void breath() {
            System.out.println("学生应该呼吸新鲜的无雾霾空气");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
  • 相关阅读:
    openssl3.2 - exp - export ecc pubkey from ecc priv key
    vue 引入pinia报 injection “symbol(pinia)“ not found
    配置haproxy负载均衡http
    Mysql的函数方法
    第五章 虚拟机的安装和使用
    TCP的保活机制
    【CSAPP+电流+梯度下降法】九阳神功-速览1
    java-php-python-ssm基于汽车美容管理计算机毕业设计
    数字时代古文的传承———云南文化瑰宝“爨文化“(我为家乡发声)
    整站抓取的神器
  • 原文地址:https://blog.csdn.net/qq_44774198/article/details/125612230