什么是抽象类

1.抽象方法:

   - 由abstract修饰
   - 只有方法的定义,没有具体的实现(连{}都没有)

2. 抽象类:

   - 由abstract修饰

   - 包含抽象方法的类必须是抽象类,但不包含抽象方法的类也可以声明为抽象类

   - 抽象类不能被实例化(new对象)

   - 抽象类是需要被继承的,派生类:

     - 必须重写所有抽象方法------------------变不完整为完整
     - 也声明为抽象类----------------------------一般不这么用

   - 抽象类的意义:

     - 封装共有的属性和行为------------------代码复用

     - 可以包含抽象方法,为所有派生类统一入口(名字统一),强制必须重写

       ```java
       public abstract class Animal {
           String name;
           int age;
           String color;
           Animal(String name,int age,String color){
               this.name = name;
               this.age = age;
               this.color = color;
           }
       
           void drink(){
               System.out.println(color+"色的"+age+"岁的"+name+"正在喝水...");
           }
           abstract void eat();
       }
       
       public class Dog extends Animal{
           Dog(String name,int age,String color){
               super(name,age,color);
           }
       
           void lookHome(){
               System.out.println(color+"色的"+age+"岁的狗狗"+name+"正在看家...");
           }
           void eat(){
               System.out.println(color+"色的"+age+"岁的狗狗"+name+"正在吃肯头...");
           }
       }
       
       public class Chick extends Animal {
           Chick(String name,int age,String color){
               super(name,age,color);
           }
           void layEggs(){
               System.out.println(color+"色的"+age+"岁的小鸡"+name+"正在下蛋...");
           }
           void eat(){
               System.out.println(color+"色的"+age+"岁的小鸡"+name+"正在吃小米...");
           }
       }
       
       public class Fish extends Animal {
           Fish(String name,int age,String color){
               super(name,age,color);
           }
       
           void eat(){
               System.out.println(color+"色的"+age+"岁的小鱼"+name+"正在吃小虾...");
           }
       }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值