java中抽象类和抽象方法到底什么关系?

抽象类和抽象方法什么关系?抽象类中可能有抽象方法,也可能没有抽象方法。那位说,就跟没说一样,那抽象类和抽象方法都叫抽象,他们必定有关系,那关系是什么呢?如果一个类中有抽象方法,它必须得是抽象类。

An abstract class may have no abstract method,such as the following class Car. 马  克- t  o --wi n: At this time,the only point and the meaning of abstract class is that we can not instantiated the class, because it is abstract class.Why an abstract class can have a nonabstract method? what is the point? also in logic, think over the following example, car is a bit abstract in that you dont' know exactly whether it is a truck or a jeep or a limersine, 马克-to-win:so it is defined as a abstract class. but no matter whether it is truck,jep, or limersine, it definitely use steering wheel. so its steer() method is an ordinary method instead of an abstract method.  )
Abstract class can’t be instantiated.
例1.7.1---

abstract class Nothing {//里面有方法也照样声明为abstract

   void nothing() {
       System.out.println("nothing");
    }
}
abstract class VehMark_to_win {
    abstract void steer();
    abstract void move();
}
class Bike extends VehMark_to_win {//Bike不是抽象的, 所以必须得全部实现abstract方法
    void steer() {
        System.out.println("Turn handlebar");
    }
    void move() {//Bike不是抽象的, 所以必须得实现move方法
        System.out.println("move");
    }
}
abstract class Cart extends VehMark_to_win {
    //因为Cart是抽象的, 可以这里还是什么都不干
}
abstract class Car extends VehMark_to_win {
    void steer() {
        System.out.println("Turn steering wheel");
    }
    void move() {
        System.out.println("move");
    }
}
class Lim extends Car {
    //之所以这里可以什么都不干, 而且还不是抽象的,马克-to-win: 因为父类Car全部实现了Veh的抽象方法
}
public class Test {
    public static void main(String[] args) {
        Nothing n0;//声明是没问题的, 但不能实例化
        Bike b = new Bike();
        Lim l = new Lim();
        b.steer();
        l.steer();
    }
}

更多请见:http://www.mark-to-win.com/tutorial/java_3_RelationAbstractMethodClass.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值