课堂总结5

课堂总结5

抽象类相关

  • 抽象类:一个类没有足够的信息去描述一个对象,那么这个类叫做抽象类。‘

  • 抽象方法:抽象方法一定·在抽象类中,只有方法名并且抽象方法没有方法体.抽象方法没有定义,方法名后直接接上分号,无"{ }"

  • 抽象类特点:一般抽象类有abstract修饰,其没有实例,所以不能实例化(即new)对象,并且其一定有构造方法·,负责被子类继承。抽象方法没有方法体。抽象类除了不能实例化对象外其他与正常类一致,成员变量,成员方法以及构造方法的访问方式不会改变。而且,因为抽象类中不可以实例化对象,因此抽象类只能通过被继承而使用。

其存在的意义:1.多态中父类型引用指向子类型对象

2.面向抽象,不是面向具体


示例代码:

public abstract class Abstract {
    public String name;
    public int age;
    public String type;
    public Abstract(String name,int age,String type){
        this.name = name;
        this.age = age;
        this.type=type;
    }
    public String Sort(){
        System.out.println("sort action!");
        return " ";
    }
    public int AgeSort(){
        System.out.println("Age sort action!");
        return 0;
    }
    public void Print(){
        System.out.println(name + "的年龄是" + age + "属于" + type + "类型");
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public String getType(){
        return type;
    }
    public void setName(String name){
       this.name=name;
    }
    public void setAge(int age){
        this.age=age;
    }
    public void setType(String type){
        this.type=type;
    }
}
public class SonAbstract extends Abstract{
    private double number;
    public SonAbstract(String name,int age,String type,int number){
        super(name,age,type);
        setNumber(number);
    }
    public void setNumber(int number){
        this.number=number;
    }
    public double getNumber(){
        return number;
    }
    public void Print(){
        System.out.println("this is a print");
        System.out.println("数量为" + number + "叫做" + name + "的年龄是" + age + "种族是" + type);

    }
    public void setNumber(){
        this.number=number;
    }
    public double getNumber(int Number){
        return number;
    }
}
public class Test {
    public static void main(String[] args){
        SonAbstract e =new SonAbstract("giegie",14,"human",356);
        e.Sort();
        e.Print();
    }
}

输出结果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WqDHrrbE-1650810172964)([(5条消息)] 课堂笔记5图_Re:Life的博客-CSDN博客)

4)] 课堂笔记5图_Re:Life的博客-CSDN博客](https://blog.csdn.net/m0_67219881/article/details/124393389))

  • 若不使用Sonabstract子类,则会报错,因此使用Sonabstract子类是必须的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值