1. 程式人生 > >java中抽象類和抽象方法到底什麽關系?請舉例說明!

java中抽象類和抽象方法到底什麽關系?請舉例說明!

init 舉例 web nds ike anti 聲明 use dont

抽象類和抽象方法什麽關系?抽象類中可能有抽象方法,也可能沒有抽象方法。(視頻下載) (全部書籍)那位說,就跟沒說一樣,那抽象類和抽象方法都叫抽象,他們必定有關系,那關系是什麽呢?如果一個類中有抽象方法,它必須得是抽象類。

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;//聲明是沒問題的, 但不能實例化
。。。。。。。。。。。。。。。
詳情請見:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner3_web.html#RelationAbstractMethodClass

java中抽象類和抽象方法到底什麽關系?請舉例說明!