抽象類在 Java 中的實際應用案例很多,以下是一些常見的例子:
假設我們有一個 Animal
類,以及兩個子類 Dog
和 Cat
。Animal
類中定義了一個 eat()
方法,但這個方法的具體行為由子類決定。
public abstract class Animal {
public void eat() {
System.out.println("吃飯~");
}
}
class Dog extends Animal {
@Override
public void eat() {
System.out.println("吃骨頭~");
}
public void bark() {
System.out.println("汪汪~");
}
}
class Cat extends Animal {
@Override
public void eat() {
System.out.println("吃魚~");
}
public void mimi() {
System.out.println("喵喵~~");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.eat(); // 輸出: 吃骨頭~
dog.bark(); // 輸出: 汪汪~
Animal cat = new Cat();
cat.eat(); // 輸出: 吃魚~
cat.mimi(); // 輸出: 喵喵~~
}
}
在這個例子中,Animal
類的 eat()
方法并沒有實際意義,因為每種動物的飲食行為不同。因此,我們可以將 Animal
類定義為抽象類,只聲明方法而不提供具體實現。
假設我們要編寫一個計算矩形、三角形和圓的面積與周長的程序。我們可以為這三個類抽象出一個父類 Shape
,在父類里定義計算面積與周長的方法名,再將具體的計算公式在子類中實現。
public abstract class Shape {
abstract void draw(); // 抽象方法
void display() {
System.out.println("This is a shape.");
}
double getArea();
double getPerimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
void draw() {
System.out.println("Drawing a circle.");
}
@Override
double getArea() {
return Math.PI * radius * radius;
}
@Override
double getPerimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
void draw() {
System.out.println("Drawing a rectangle.");
}
@Override
double getArea() {
return width * height;
}
@Override
double getPerimeter() {
return 2 * (width + height);
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
circle.draw(); // 輸出: Drawing a circle.
System.out.println("Area: " + circle.getArea()); // 輸出: Area: 78.53981633974483
System.out.println("Perimeter: " + circle.getPerimeter()); // 輸出: Perimeter: 31.41592653589793
Shape rectangle = new Rectangle(4, 6);
rectangle.draw(); // 輸出: Drawing a rectangle.
System.out.println("Area: " + rectangle.getArea()); // 輸出: Area: 24.0
System.out.println("Perimeter: " + rectangle.getPerimeter()); // 輸出: Perimeter: 20.0
}
}
在這個例子中,通過抽象類 Shape
定義了計算面積和周長的方法,具體的計算方法由子類 Circle
和 Rectangle
實現。
假設我們有一個 Vehicle
抽象類,包含一些通用的屬性和方法,如 color
和 start()
方法,然后通過具體的子類 Car
和 Bike
來實現這些方法。
public abstract class Vehicle {
protected String color;
public Vehicle(String color) {
this.color = color;
}
public abstract void start();
}
class Car extends Vehicle {
public Car(String color) {
super(color);
}
@Override
public void start() {
System.out.println("Car starting.");
}
}
class Bike extends Vehicle {
public Bike(String color) {
super(color);
}
@Override
public void start() {
System.out.println("Bike starting.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car("Red");
car.start(); // 輸出: Car starting.
System.out.println(car.color); // 輸出: Red
Vehicle bike = new Bike("Blue");
bike.start(); // 輸出: Bike starting.
System.out.println(bike.color); // 輸出: Blue
}
}
在這個例子中,Vehicle
類是一個抽象類,包含了一個抽象方法 start()
和一個具體方法 color
。具體的實現由子類 Car
和 Bike
完成。
這些例子展示了抽象類在 Java 中的實際應用,通過抽象類可以有效地組織和管理代碼,提高代碼的可維護性和可擴展性。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。