一、封裝性
封裝性是指將對象的屬性和方法封裝在一起,對外界隱藏對象的細節,使得對象對使用者來說更加簡單易用和安全可靠。
例如,我們可以創建一個類Car來表示汽車,在類中定義汽車的屬性和方法:
class Car { private String brand; private String color; private int price; public Car(String brand, String color, int price) { this.brand = brand; this.color = color; this.price = price; } public void run() { System.out.println("The car is running."); } }
其中,屬性brand、color、price被標記為私有(private),只能在Car類內部訪問,類外部無法直接訪問。但我們可以通過給屬性提供公有(public)的setter和getter方法來訪問屬性值:
public String getBrand() { return this.brand; } public void setBrand(String brand) { this.brand = brand; }
通過封裝,我們不僅可以保護對象內部的實現細節,同時也可以方便地控制訪問方式,保證數據不會被錯誤地篡改。
二、繼承性
繼承性是指在一個已有類的基礎上創建新類,新類可以繼承已有類的屬性和方法,並且可以在此基礎上擴展新的特性。
例如,我們可以創建一個類Animal作為基類,然後創建兩個子類Dog和Cat,這兩個子類可以共享父類Animal的屬性和方法,並且可以為自己添加新的特性,如下所示:
class Animal { private String name; private int age; public Animal(String name, int age) { this.name = name; this.age = age; } public void eat() { System.out.println("The animal is eating."); } } class Dog extends Animal { private String breed; public Dog(String name, int age, String breed) { super(name, age); this.breed = breed; } public void bark() { System.out.println("The dog is barking."); } } class Cat extends Animal { private boolean isCute; public Cat(String name, int age, boolean isCute) { super(name, age); this.isCute = isCute; } public void sleep() { System.out.println("The cat is sleeping."); } }
通過繼承,我們可以提高代碼的復用性,減少代碼的重複編寫。同時,繼承也可以使得代碼更加靈活擴展,便於引入新的特性。
三、多態性
多態性是指同一種行為或方法具有多種不同的表現形式,即不同的對象對同一種方法做出不同的響應。多態性主要基於繼承和接口實現來實現。
例如,我們可以創建一個接口Shape,然後創建三個類Circle、Rectangle和Triangle,這三個類實現Shape接口,並且分別提供自己的面積計算方法:
interface Shape { public double area(); } class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double area() { return Math.PI * radius * radius; } } class Rectangle implements Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double area() { return length * width; } } class Triangle implements Shape { private double base; private double height; public Triangle(double base, double height) { this.base = base; this.height = height; } public double area() { return 0.5 * base * height; } }
通過多態性,我們可以更加方便地擴展功能,而且代碼更加靈活,易於維護。
四、抽象性
抽象性是指忽略實例的具體細節,而只關注它們的共同特徵,將它們歸納為一個類或一組類的過程。
例如,我們可以創建一個抽象類Shape來表示形狀,然後在Shape類中定義一個抽象方法area(),表示計算面積的方法,具體實現由子類去實現:
abstract class Shape { public abstract double area(); } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double area() { return Math.PI * radius * radius; } } class Rectangle extends Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double area() { return length * width; } } class Triangle extends Shape { private double base; private double height; public Triangle(double base, double height) { this.base = base; this.height = height; } public double area() { return 0.5 * base * height; } }
通過抽象性,我們可以將類歸納為更高層次的概念,使得代碼更加優雅簡潔,並且易於擴展。
原創文章,作者:AGIAL,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/371508.html