移动端六大语言速记:第5部分 - 面向对象编程(OOP)

移动端六大语言速记:第5部分 - 面向对象编程(OOP)

本文对比Java、Kotlin、Flutter(Dart)、Python、ArkTS和Swift这六种移动端开发语言的面向对象编程特性,帮助开发者快速掌握各语言的语法差异。

5. 面向对象编程(OOP)

5.1 类与对象

各语言类与对象的语法对比:

语言类定义构造函数实例化属性访问
Javaclass ClassName {...}ClassName() {...}new ClassName()object.property
Kotlinclass ClassName {...}constructor(...) {...}ClassName()object.property
Dartclass ClassName {...}ClassName() {...}ClassName()object.property
Pythonclass ClassName:def __init__(self):ClassName()object.property
ArkTSclass ClassName {...}constructor(...) {...}new ClassName()object.property
Swiftclass ClassName {...}init() {...}ClassName()object.property
示例对比

Java:

// 类定义
public class Person {
    // 属性
    private String name;
    private int age;
    
    // 构造函数
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // 无参构造函数
    public Person() {
        this.name = "Unknown";
        this.age = 0;
    }
    
    // Getter和Setter方法
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    // 方法
    public void introduce() {
        System.out.println("My name is " + name + " and I am " + age + " years old.");
    }
}

// 使用类
public class Main {
    public static void main(String[] args) {
        // 实例化对象
        Person person1 = new Person("Alice", 25);
        Person person2 = new Person();
        
        // 访问方法
        person1.introduce();  // 输出: My name is Alice and I am 25 years old.
        person2.introduce();  // 输出: My name is Unknown and I am 0 years old.
        
        // 使用Setter修改属性
        person2.setName("Bob");
        person2.setAge(30);
        person2.introduce();  // 输出: My name is Bob and I am 30 years old.
    }
}

Kotlin:

// 类定义(主构造函数在类头部)
class Person(private var name: String, private var age: Int) {
    // 次构造函数
    constructor() : this("Unknown", 0)
    
    // 属性可以直接在主构造函数中声明并初始化
    // Kotlin自动为属性生成getter和setter(对于var)
    
    // 方法
    fun introduce() {
        println("My name is $name and I am $age years old.")
    }
}

// 使用类
fun main() {
    // 实例化对象(不需要new关键字)
    val person1 = Person("Alice", 25)
    val person2 = Person()
    
    // 访问方法
    person1.introduce()  // 输出: My name is Alice and I am 25 years old.
    person2.introduce()  // 输出: My name is Unknown and I am 0 years old.
    
    // 使用自动生成的setter修改属性
    // 如果属性是private,需要提供自定义的setter
    // 这里假设我们将属性改为public或提供了setter
    person2.name = "Bob"  // 假设有setter
    person2.age = 30     // 假设有setter
    person2.introduce()  // 输出: My name is Bob and I am 30 years old.
}

Dart:

// 类定义
class Person {
    // 属性
    String name;
    int age;
    
    // 构造函数
    Person(this.name, this.age);  // 简洁的语法直接初始化属性
    
    // 命名构造函数
    Person.unknown() {
        name = "Unknown";
        age = 0;
    }
    
    // 方法
    void introduce() {
        print('My name is $name and I am $age years old.');
    }
}

// 使用类
void main() {
    // 实例化对象(不需要new关键字,但可以使用)
    var person1 = Person("Alice", 25);
    var person2 = Person.unknown();
    
    // 访问方法
    person1.introduce();  // 输出: My name is Alice and I am 25 years old.
    person2.introduce();  // 输出: My name is Unknown and I am 0 years old.
    
    // 直接修改属性(Dart默认提供公共访问)
    person2.name = "Bob";
    person2.age = 30;
    person2.introduce();  // 输出: My name is Bob and I am 30 years old.
}

Python:

# 类定义
class Person:
    # 构造函数
    def __init__(self, name=None, age=None):
        self.name = name if name else "Unknown"
        self.age = age if age else 0
    
    # 方法
    def introduce(self):
        print(f"My name is {self.name} and I am {self.age} years old.")

# 使用类
if __name__ == "__main__":
    # 实例化对象
    person1 = Person("Alice", 25)
    person2 = Person()
    
    # 访问方法
    person1.introduce()  # 输出: My name is Alice and I am 25 years old.
    person2.introduce()  # 输出: My name is Unknown and I am 0 years old.
    
    # 直接修改属性(Python默认提供公共访问)
    person2.name = "Bob"
    person2.age = 30
    person2.introduce()  # 输出: My name is Bob and I am 30 years old.

ArkTS:

// 类定义
class Person {
    // 属性
    private name: string;
    private age: number;
    
    // 构造函数
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    
    // 静态方法创建默认实例
    static createUnknown(): Person {
        return new Person("Unknown", 0);
    }
    
    // Getter和Setter方法
    getName(): string {
        return this.name;
    }
    
    setName(name: string): void {
        this.name = name;
    }
    
    getAge(): number {
        return this.age;
    }
    
    setAge(age: number): void {
        this.age = age;
    }
    
    // 方法
    introduce(): void {
        console.log(`My name is ${this.name} and I am ${this.age} years old.`);
    }
}

// 使用类
function main(): void {
    // 实例化对象
    let person1 = new Person("Alice", 25);
    let person2 = Person.createUnknown();
    
    // 访问方法
    person1.introduce();  // 输出: My name is Alice and I am 25 years old.
    person2.introduce();  // 输出: My name is Unknown and I am 0 years old.
    
    // 使用Setter修改属性
    person2.setName("Bob");
    person2.setAge(30);
    person2.introduce();  // 输出: My name is Bob and I am 30 years old.
}

Swift:

// 类定义
class Person {
    // 属性
    private var name: String
    private var age: Int
    
    // 构造函数
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
    
    // 便利构造函数
    convenience init() {
        self.init(name: "Unknown", age: 0)
    }
    
    // 方法
    func introduce() {
        print("My name is \(name) and I am \(age) years old.")
    }
    
    // Getter和Setter(Swift中通常使用计算属性)
    func getName() -> String {
        return name
    }
    
    func setName(_ name: String) {
        self.name = name
    }
    
    func getAge() -> Int {
        return age
    }
    
    func setAge(_ age: Int) {
        self.age = age
    }
}

// 使用类
func main() {
    // 实例化对象
    let person1 = Person(name: "Alice", age: 25)
    let person2 = Person()
    
    // 访问方法
    person1.introduce()  // 输出: My name is Alice and I am 25 years old.
    person2.introduce()  // 输出: My name is Unknown and I am 0 years old.
    
    // 使用Setter修改属性
    person2.setName("Bob")
    person2.setAge(30)
    person2.introduce()  // 输出: My name is Bob and I am 30 years old.
}

5.2 继承

各语言继承的语法对比:

语言继承语法方法重写调用父类方法多重继承
Javaclass Child extends Parent@Overridesuper.method()不支持,但可实现多接口
Kotlinclass Child : Parent()override funsuper.method()不支持,但可实现多接口
Dartclass Child extends Parent@overridesuper.method()不支持,但可实现多接口和mixin
Pythonclass Child(Parent):直接重写super().method()支持
ArkTSclass Child extends Parentoverridesuper.method()不支持,但可实现多接口
Swiftclass Child: Parentoverride funcsuper.method()不支持,但可实现多协议
示例对比

Java:

// 父类
public class Animal {
    protected String name;
    
    public Animal(String name) {
        this.name = name;
    }
    
    public void makeSound() {
        System.out.println("Some generic sound");
    }
}

// 子类
public class Dog extends Animal {
    private String breed;
    
    public Dog(String name, String breed) {
        super(name);  // 调用父类构造函数
        this.breed = breed;
    }
    
    @Override  // 方法重写
    public void makeSound() {
        System.out.println("Woof! Woof!");
    }
    
    public void makeSound(boolean loud) {  // 方法重载
        if (loud) {
            System.out.println("WOOF! WOOF!");
        } else {
            makeSound();
        }
    }
    
    public void displayInfo() {
        System.out.println("Dog: " + name + ", Breed: " + breed);
        super.makeSound();  // 调用父类方法
    }
}

// 使用继承
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy", "Golden Retriever");
        dog.makeSound();     // 输出: Woof! Woof!
        dog.makeSound(true); // 输出: WOOF! WOOF!
        dog.displayInfo();   // 输出: Dog: Buddy, Breed: Golden Retriever
                            // 输出: Some generic sound
    }
}

Kotlin:

// 父类(需要open关键字允许继承)
open class Animal(protected val name: String) {
    // 需要open关键字允许方法被重写
    open fun makeSound() {
        println("Some generic sound")
    }
}

// 子类
class Dog(name: String, private val breed: String) : Animal(name) {
    // 使用override关键字重写方法
    override fun makeSound() {
        println("Woof! Woof!")
    }
    
    // 方法重载
    fun makeSound(loud: Boolean) {
        if (loud) {
            println("WOOF! WOOF!")
        } else {
            makeSound()
        }
    }
    
    fun displayInfo() {
        println("Dog: $name, Breed: $breed")
        super.makeSound()  // 调用父类方法
    }
}

// 使用继承
fun main() {
    val dog = Dog("Buddy", "Golden Retriever")
    dog.makeSound()      // 输出: Woof! Woof!
    dog.makeSound(true)  // 输出: WOOF! WOOF!
    dog.displayInfo()    // 输出: Dog: Buddy, Breed: Golden Retriever
                        // 输出: Some generic sound
}

Dart:

// 父类
class Animal {
    String name;
    
    Animal(this.name);
    
    void makeSound() {
        print('Some generic sound');
    }
}

// 子类
class Dog extends Animal {
    String breed;
    
    Dog(String name, this.breed) : super(name);  // 调用父类构造函数
    
      // 方法重写
    void makeSound() {
        print('Woof! Woof!');
    }
    
    // 方法重载(Dart不支持传统的方法重载,但可以使用可选参数或命名参数)
    void bark({bool loud = false}) {
        if (loud) {
            print('WOOF! WOOF!');
        } else {
            makeSound();
        }
    }
    
    void displayInfo() {
        print('Dog: $name, Breed: $breed');
        super.makeSound();  // 调用父类方法
    }
}

// 使用继承
void main() {
    var dog = Dog('Buddy', 'Golden Retriever');
    dog.makeSound();       // 输出: Woof! Woof!
    dog.bark(loud: true);  // 输出: WOOF! WOOF!
    dog.displayInfo();     // 输出: Dog: Buddy, Breed: Golden Retriever
                          // 输出: Some generic sound
}

Python:

# 父类
class Animal:
    def __init__(self, name):
        self.name = name
    
    def make_sound(self):
        print("Some generic sound")

# 子类
class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)  # 调用父类构造函数
        self.breed = breed
    
    # 方法重写(Python不需要特殊注解)
    def make_sound(self):
        print("Woof! Woof!")
    
    # 方法重载(Python不支持传统的方法重载,但可以使用默认参数)
    def bark(self, loud=False):
        if loud:
            print("WOOF! WOOF!")
        else:
            self.make_sound()
    
    def display_info(self):
        print(f"Dog: {self.name}, Breed: {self.breed}")
        super().make_sound()  # 调用父类方法

# 多重继承示例
class Swimmer:
    def swim(self):
        print("Swimming...")

class Labrador(Dog, Swimmer):  # 多重继承
    def __init__(self, name):
        Dog.__init__(self, name, "Labrador")
    
    def display_abilities(self):
        self.make_sound()
        self.swim()

# 使用继承
if __name__ == "__main__":
    dog = Dog("Buddy", "Golden Retriever")
    dog.make_sound()    # 输出: Woof! Woof!
    dog.bark(True)      # 输出: WOOF! WOOF!
    dog.display_info()  # 输出: Dog: Buddy, Breed: Golden Retriever
                       # 输出: Some generic sound
    
    # 多重继承
    lab = Labrador("Max")
    lab.display_abilities()  # 输出: Woof! Woof!
                           # 输出: Swimming...

ArkTS:

// 父类
class Animal {
    protected name: string;
    
    constructor(name: string) {
        this.name = name;
    }
    
    makeSound(): void {
        console.log("Some generic sound");
    }
}

// 子类
class Dog extends Animal {
    private breed: string;
    
    constructor(name: string, breed: string) {
        super(name);  // 调用父类构造函数
        this.breed = breed;
    }
    
    override makeSound(): void {  // 方法重写
        console.log("Woof! Woof!");
    }
    
    // 方法重载
    makeNoise(loud: boolean): void {
        if (loud) {
            console.log("WOOF! WOOF!");
        } else {
            this.makeSound();
        }
    }
    
    displayInfo(): void {
        console.log(`Dog: ${this.name}, Breed: ${this.breed}`);
        super.makeSound();  // 调用父类方法
    }
}

// 使用继承
function main(): void {
    let dog = new Dog("Buddy", "Golden Retriever");
    dog.makeSound();     // 输出: Woof! Woof!
    dog.makeNoise(true); // 输出: WOOF! WOOF!
    dog.displayInfo();   // 输出: Dog: Buddy, Breed: Golden Retriever
                        // 输出: Some generic sound
}

Swift:

// 父类
class Animal {
    protected var name: String
    
    init(name: String) {
        self.name = name
    }
    
    func makeSound() {
        print("Some generic sound")
    }
}

// 子类
class Dog: Animal {
    private var breed: String
    
    init(name: String, breed: String) {
        self.breed = breed
        super.init(name: name)  // 调用父类构造函数
    }
    
    override func makeSound() {  // 方法重写
        print("Woof! Woof!")
    }
    
    // 方法重载
    func makeSound(loud: Bool) {
        if loud {
            print("WOOF! WOOF!")
        } else {
            makeSound()
        }
    }
    
    func displayInfo() {
        print("Dog: \(name), Breed: \(breed)")
        super.makeSound()  // 调用父类方法
    }
}

// 使用继承
func main() {
    let dog = Dog(name: "Buddy", breed: "Golden Retriever")
    dog.makeSound()         // 输出: Woof! Woof!
    dog.makeSound(loud: true)  // 输出: WOOF! WOOF!
    dog.displayInfo()       // 输出: Dog: Buddy, Breed: Golden Retriever
                           // 输出: Some generic sound
}

5.3 封装

各语言封装(访问控制)的语法对比:

语言公共访问私有访问保护访问包/模块访问
Javapublicprivateprotected默认(无修饰符)
Kotlin默认privateprotectedinternal
Dart默认_开头无直接支持library
Python默认__开头(名称修饰)_开头(约定)无直接支持
ArkTSpublicprivateprotectedinternal
Swiftpublic/openprivate/fileprivate无直接对应internal(默认)
示例对比

Java:

public class BankAccount {
    // 公共访问 - 任何地方都可访问
    public String accountType;
    
    // 私有访问 - 只在类内部可访问
    private double balance;
    
    // 保护访问 - 类内部和子类可访问
    protected String accountNumber;
    
    // 包访问(默认) - 同包内可访问
    String ownerName;
    
    public BankAccount(String accountNumber, String ownerName, String accountType) {
        this.accountNumber = accountNumber;
        this.ownerName = ownerName;
        this.accountType = accountType;
        this.balance = 0.0;
    }
    
    // 公共方法提供对私有属性的访问
    public double getBalance() {
        return balance;
    }
    
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Deposited: $" + amount);
        }
    }
    
    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            System.out.println("Withdrawn: $" + amount);
        } else {
            System.out.println("Insufficient funds");
        }
    }
}

Kotlin:

class BankAccount(val accountNumber: String, var ownerName: String, var accountType: String) {
    // 私有访问 - 只在类内部可访问
    private var balance: Double = 0.0
    
    // 保护访问 - 类内部和子类可访问
    protected var transactionCount: Int = 0
    
    // 内部访问 - 同模块内可访问
    internal var lastTransactionDate: String = "None"
    
    // 公共方法提供对私有属性的访问
    fun getBalance(): Double {
        return balance
    }
    
    fun deposit(amount: Double) {
        if (amount > 0) {
            balance += amount
            transactionCount++
            lastTransactionDate = "Today"
            println("Deposited: $${amount}")
        }
    }
    
    fun withdraw(amount: Double) {
        if (amount > 0 && amount <= balance) {
            balance -= amount
            transactionCount++
            lastTransactionDate = "Today"
            println("Withdrawn: $${amount}")
        } else {
            println("Insufficient funds")
        }
    }
}

Dart:

class BankAccount {
    // 公共访问(默认) - 任何地方都可访问
    String accountType;
    
    // 私有访问(以_开头) - 只在库内部可访问
    double _balance;
    
    // Dart没有protected关键字,但可以在同一个库中访问私有成员
    String accountNumber;
    
    // 库私有 - 同库内可访问
    String ownerName;
    
    BankAccount(this.accountNumber, this.ownerName, this.accountType) {
        _balance = 0.0;
    }
    
    // 公共方法提供对私有属性的访问
    double getBalance() {
        return _balance;
    }
    
    void deposit(double amount) {
        if (amount > 0) {
            _balance += amount;
            print('Deposited: \$${amount}');
        }
    }
    
    void withdraw(double amount) {
        if (amount > 0 && amount <= _balance) {
            _balance -= amount;
            print('Withdrawn: \$${amount}');
        } else {
            print('Insufficient funds');
        }
    }
}

Python:

class BankAccount:
    def __init__(self, account_number, owner_name, account_type):
        # 公共访问(默认) - 任何地方都可访问
        self.account_type = account_type
        
        # 私有访问(双下划线前缀) - 名称修饰,类外难以直接访问
        self.__balance = 0.0
        
        # 保护访问(单下划线前缀) - 约定俗成,表示不应直接访问
        self._account_number = account_number
        
        # 公共访问但按约定视为包私有
        self.owner_name = owner_name
    
    # 公共方法提供对私有属性的访问
    def get_balance(self):
        return self.__balance
    
    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount
            print(f"Deposited: ${amount}")
    
    def withdraw(self, amount):
        if amount > 0 and amount <= self.__balance:
            self.__balance -= amount
            print(f"Withdrawn: ${amount}")
        else:
            print("Insufficient funds")

# 测试访问控制
account = BankAccount("123456", "John Doe", "Savings")
print(account.account_type)  # 可以访问
# print(account.__balance)   # 错误:无法直接访问私有属性
# 但可以通过名称修饰访问(不推荐)
# print(account._BankAccount__balance)  # 可以访问,但不应该这样做
print(account._account_number)  # 可以访问,但按约定不应直接访问

5.4 多态

各语言多态的语法对比:

语言方法重载方法重写接口/协议多态
Java支持@Override接口实现
Kotlin支持override fun接口实现
Dart不支持@override接口实现
Python不支持直接重写鸭子类型
ArkTS支持override接口实现
Swift支持override func协议实现
示例对比

Java:

// 多态示例
interface Shape {
    double area();
}

class Circle implements Shape {
    private double radius;
    
    Circle(double radius) { this.radius = radius; }
    
    @Override
    public double area() {
        return Math.PI * radius * radius;
    }
}

class Rectangle implements Shape {
    private double width, height;
    
    Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    @Override
    public double area() {
        return width * height;
    }
}

public class Main {
    public static void printArea(Shape shape) {
        System.out.println("Area: " + shape.area());
    }
    
    public static void main(String[] args) {
        Shape circle = new Circle(5.0);
        Shape rectangle = new Rectangle(4.0, 6.0);
        
        printArea(circle);    // 输出: Area: 78.53981633974483
        printArea(rectangle); // 输出: Area: 24.0
    }
}

Kotlin:

// 多态示例
interface Shape {
    fun area(): Double
}

class Circle(private val radius: Double) : Shape {
    override fun area(): Double {
        return Math.PI * radius * radius
    }
}

class Rectangle(private val width: Double, private val height: Double) : Shape {
    override fun area(): Double {
        return width * height
    }
}

fun printArea(shape: Shape) {
    println("Area: ${shape.area()}")
}

fun main() {
    val circle: Shape = Circle(5.0)
    val rectangle: Shape = Rectangle(4.0, 6.0)
    
    printArea(circle)    // 输出: Area: 78.53981633974483
    printArea(rectangle) // 输出: Area: 24.0
}

Dart:

// 多态示例
abstract class Shape {
    double area();
}

class Circle implements Shape {
    double radius;
    
    Circle(this.radius);
    
    
    double area() {
        return 3.14159 * radius * radius;
    }
}

class Rectangle implements Shape {
    double width, height;
    
    Rectangle(this.width, this.height);
    
    
    double area() {
        return width * height;
    }
}

void printArea(Shape shape) {
    print('Area: ${shape.area()}');
}

void main() {
    Shape circle = Circle(5.0);
    Shape rectangle = Rectangle(4.0, 6.0);
    
    printArea(circle);    // 输出: Area: 78.53975
    printArea(rectangle); // 输出: Area: 24.0
}

Python:

# 多态示例
class Shape:
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    
    def area(self):
        return 3.14159 * self.radius ** 2

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height
    
    def area(self):
        return self.width * self.height

def print_area(shape):
    print(f"Area: {shape.area()}")

if __name__ == "__main__":
    circle = Circle(5.0)
    rectangle = Rectangle(4.0, 6.0)
    
    print_area(circle)    # 输出: Area: 78.53975
    print_area(rectangle) # 输出: Area: 24.0

ArkTS:

// 多态示例
interface Shape {
    area(): number;
}

class Circle implements Shape {
    private radius: number;
    
    constructor(radius: number) {
        this.radius = radius;
    }
    
    area(): number {
        return Math.PI * this.radius * this.radius;
    }
}

class Rectangle implements Shape {
    private width: number;
    private height: number;
    
    constructor(width: number, height: number) {
        this.width = width;
        this.height = height;
    }
    
    area(): number {
        return this.width * this.height;
    }
}

function printArea(shape: Shape): void {
    console.log(`Area: ${shape.area()}`);
}

function main(): void {
    let circle: Shape = new Circle(5);
    let rectangle: Shape = new Rectangle(4, 6);
    
    printArea(circle);    // 输出: Area: 78.53981633974483
    printArea(rectangle); // 输出: Area: 24
}

Swift:

// 多态示例
protocol Shape {
    func area() -> Double
}

class Circle: Shape {
    private var radius: Double
    
    init(radius: Double) {
        self.radius = radius
    }
    
    func area() -> Double {
        return Double.pi * radius * radius
    }
}

class Rectangle: Shape {
    private var width: Double
    private var height: Double
    
    init(width: Double, height: Double) {
        self.width = width
        self.height = height
    }
    
    func area() -> Double {
        return width * height
    }
}

func printArea(shape: Shape) {
    print("Area: \(shape.area())")
}

func main() {
    let circle: Shape = Circle(radius: 5.0)
    let rectangle: Shape = Rectangle(width: 4.0, height: 6.0)
    
    printArea(shape: circle)    // 输出: Area: 78.53981633974483
    printArea(shape: rectangle) // 输出: Area: 24.0
}

5.5 抽象类与接口

各语言抽象类与接口的语法对比:

语言抽象类接口范型注解/元数据
Javaabstract classinterface<T>@Annotation
Kotlinabstract classinterface<T>@Annotation
Dartabstract classabstract class (可充当接口)<T>@Annotation
PythonABC模块无原生支持装饰器
ArkTSabstract classinterface<T>@Decorator
Swiftprotocol (可带实现)protocol<T>@Attribute
示例对比

Java:

// 抽象类与接口示例
abstract class Animal {
    protected String name;
    
    public Animal(String name) {
        this.name = name;
    }
    
    // 抽象方法
    public abstract void makeSound();
    
    // 具体方法
    public void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

interface Flyable {
    void fly();
}

class Bird extends Animal implements Flyable {
    public Bird(String name) {
        super(name);
    }
    
    @Override
    public void makeSound() {
        System.out.println("Chirp Chirp!");
    }
    
    @Override
    public void fly() {
        System.out.println(name + " is flying.");
    }
}

public class Main {
    public static void main(String[] args) {
        Bird bird = new Bird("Sparrow");
        bird.makeSound();  // 输出: Chirp Chirp!
        bird.fly();        // 输出: Sparrow is flying.
        bird.sleep();      // 输出: Sparrow is sleeping.
    }
}

Kotlin:

// 抽象类与接口示例
abstract class Animal(val name: String) {
    // 抽象方法
    abstract fun makeSound()
    
    // 具体方法
    fun sleep() {
        println("$name is sleeping.")
    }
}

interface Flyable {
    fun fly()
}

class Bird(name: String) : Animal(name), Flyable {
    override fun makeSound() {
        println("Chirp Chirp!")
    }
    
    override fun fly() {
        println("$name is flying.")
    }
}

fun main() {
    val bird = Bird("Sparrow")
    bird.makeSound()  // 输出: Chirp Chirp!
    bird.fly()        // 输出: Sparrow is flying.
    bird.sleep()      // 输出: Sparrow is sleeping.
}

Dart:

// 抽象类与接口示例
abstract class Animal {
    String name;
    
    Animal(this.name);
    
    // 抽象方法
    void makeSound();
    
    // 具体方法
    void sleep() {
        print('$name is sleeping.');
    }
}

// Dart中没有interface关键字,使用抽象类充当接口
abstract class Flyable {
    void fly();
}

class Bird extends Animal implements Flyable {
    Bird(String name) : super(name);
    
    
    void makeSound() {
        print('Chirp Chirp!');
    }
    
    
    void fly() {
        print('$name is flying.');
    }
}

void main() {
    var bird = Bird('Sparrow');
    bird.makeSound();  // 输出: Chirp Chirp!
    bird.fly();        // 输出: Sparrow is flying.
    bird.sleep();      // 输出: Sparrow is sleeping.
}

Python:

# 抽象类与接口示例
from abc import ABC, abstractmethod

class Animal(ABC):
    def __init__(self, name):
        self.name = name
    
    @abstractmethod
    def make_sound(self):
        pass
    
    def sleep(self):
        print(f"{self.name} is sleeping.")

# Python中没有接口的原生支持,使用抽象类模拟
class Flyable(ABC):
    @abstractmethod
    def fly(self):
        pass

class Bird(Animal, Flyable):
    def make_sound(self):
        print("Chirp Chirp!")
    
    def fly(self):
        print(f"{self.name} is flying.")

if __name__ == "__main__":
    bird = Bird("Sparrow")
    bird.make_sound()  # 输出: Chirp Chirp!
    bird.fly()         # 输出: Sparrow is flying.
    bird.sleep()       # 输出: Sparrow is sleeping.

ArkTS:

// 抽象类与接口示例
abstract class Animal {
    protected name: string;
    
    constructor(name: string) {
        this.name = name;
    }
    
    // 抽象方法
    abstract makeSound(): void;
    
    // 具体方法
    sleep(): void {
        console.log(`${this.name} is sleeping.`);
    }
}

interface Flyable {
    fly(): void;
}

class Bird extends Animal implements Flyable {
    constructor(name: string) {
        super(name);
    }
    
    override makeSound(): void {
        console.log("Chirp Chirp!");
    }
    
    fly(): void {
        console.log(`${this.name} is flying.`);
    }
}

function main(): void {
    let bird = new Bird("Sparrow");
    bird.makeSound();  // 输出: Chirp Chirp!
    bird.fly();        // 输出: Sparrow is flying.
    bird.sleep();      // 输出: Sparrow is sleeping.
}

Swift:

// 抽象类与接口示例
protocol Animal {
    var name: String { get }
    
    func makeSound()
    
    func sleep()
}

protocol Flyable {
    func fly()
}

class Bird: Animal, Flyable {
    let name: String
    
    init(name: String) {
        self.name = name
    }
    
    func makeSound() {
        print("Chirp Chirp!")
    }
    
    func sleep() {
        print("\(name) is sleeping.")
    }
    
    func fly() {
        print("\(name) is flying.")
    }
}

func main() {
    let bird = Bird(name: "Sparrow")
    bird.makeSound()  // 输出: Chirp Chirp!
    bird.fly()        // 输出: Sparrow is flying.
    bird.sleep()      // 输出: Sparrow is sleeping.
}

5.6 范型与注解

5.6.1 范型(Generics)

各语言范型的语法对比:

语言范型声明类型约束通配符型变
Javaclass Box<T><T extends Type><?>, <? extends>, <? super>使用通配符
Kotlinclass Box<T><T : Type>*, out, in声明处型变
Dartclass Box<T><T extends Type>
PythonGeneric[T]TypeVar('T', bound=Type)
ArkTSclass Box<T><T extends Type>
Swiftstruct Box<T><T: Protocol>关联类型
范型示例

Java:

// 范型类
class Box<T> {
    private T content;
    
    public void set(T content) {
        this.content = content;
    }
    
    public T get() {
        return content;
    }
}

// 范型方法
public class Utils {
    public static <T> void swap(T[] array, int i, int j) {
        T temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

// 类型约束
class NumberBox<T extends Number> {
    private T number;
    
    public void set(T number) {
        this.number = number;
    }
    
    public double getValue() {
        return number.doubleValue();
    }
}

Kotlin:

// 范型类
class Box<T>(var content: T)

// 范型方法
fun <T> swap(array: Array<T>, i: Int, j: Int) {
    val temp = array[i]
    array[i] = array[j]
    array[j] = temp
}

// 类型约束
class NumberBox<T : Number>(var number: T) {
    fun getValue(): Double = number.toDouble()
}

// 声明处型变
class Producer<out T>(private val item: T) {
    fun get(): T = item
}

class Consumer<in T> {
    fun consume(item: T) {
        println(item)
    }
}

Dart:

// 范型类
class Box<T> {
    T? _content;
    
    void set(T content) => _content = content;
    T? get() => _content;
}

// 范型方法
void swap<T>(List<T> list, int i, int j) {
    T temp = list[i];
    list[i] = list[j];
    list[j] = temp;
}

// 类型约束
class NumberBox<T extends num> {
    T _number;
    
    NumberBox(this._number);
    
    double getValue() => _number.toDouble();
}

Python:

from typing import TypeVar, Generic, List

# 范型类
T = TypeVar('T')

class Box(Generic[T]):
    def __init__(self):
        self._content: T = None
    
    def set(self, content: T) -> None:
        self._content = content
    
    def get(self) -> T:
        return self._content

# 范型方法
def swap(array: List[T], i: int, j: int) -> None:
    array[i], array[j] = array[j], array[i]

# 类型约束
NumberT = TypeVar('NumberT', bound=float)

class NumberBox(Generic[NumberT]):
    def __init__(self, number: NumberT):
        self._number = number
    
    def get_value(self) -> float:
        return float(self._number)

ArkTS:

// 范型类
class Box<T> {
    private content: T;
    
    set(content: T): void {
        this.content = content;
    }
    
    get(): T {
        return this.content;
    }
}

// 范型方法
function swap<T>(array: T[], i: number, j: number): void {
    const temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}

// 类型约束
class NumberBox<T extends number> {
    private number: T;
    
    constructor(number: T) {
        this.number = number;
    }
    
    getValue(): number {
        return this.number;
    }
}

Swift:

// 范型类
struct Box<T> {
    private var content: T
    
    mutating func set(_ content: T) {
        self.content = content
    }
    
    func get() -> T {
        return content
    }
}

// 范型方法
func swap<T>(_ array: inout [T], _ i: Int, _ j: Int) {
    let temp = array[i]
    array[i] = array[j]
    array[j] = temp
}

// 类型约束
protocol Numeric {
    func toDouble() -> Double
}

class NumberBox<T: Numeric> {
    private var number: T
    
    init(_ number: T) {
        self.number = number
    }
    
    func getValue() -> Double {
        return number.toDouble()
    }
}

5.6.2 注解(Annotations)

各语言注解的语法对比:

语言注解语法元注解自定义注解运行时反射
Java@Annotation@Target, @Retention支持支持
Kotlin@Annotation@Target, @Retention支持支持
Dart@annotation无专门元注解支持支持
Python装饰器 @decorator无专门元注解支持支持
ArkTS@Decorator@Component支持有限支持
Swift@propertyWrapper无专门元注解支持有限支持
注解示例

Java:

// 自定义注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
interface Validate {
    String message() default "";
}

// 使用注解
@Validate(message = "User data")
class User {
    @Deprecated
    public void oldMethod() {}
    
    @Override
    public String toString() {
        return "User";
    }
}

Kotlin:

// 自定义注解
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class Validate(val message: String = "")

// 使用注解
@Validate(message = "User data")
class User {
    @Deprecated("Use newMethod instead")
    fun oldMethod() {}
    
    override fun toString() = "User"
}

Dart:

// 自定义注解
class Validate {
    final String message;
    const Validate({this.message = ""});
}

// 使用注解
(message: "User data")
class User {
    
    void oldMethod() {}
    
    
    String toString() => "User";
}

Python:

# 自定义装饰器
def validate(message=""):
    def decorator(cls):
        cls._validation_message = message
        return cls
    return decorator

# 使用装饰器
@validate(message="User data")
class User:
    @property
    def name(self):
        return self._name
    
    @name.setter
    def name(self, value):
        self._name = value
    
    def __str__(self):
        return "User"

ArkTS:

// 自定义装饰器
function Validate(message: string = "") {
    return function (target: any) {
        target.prototype.validationMessage = message;
    }
}

// 使用装饰器
@Validate("User data")
class User {
    @Prop
    name: string;
    
    toString(): string {
        return "User";
    }
}

Swift:

// 自定义属性包装器
@propertyWrapper
struct Validate {
    private var value: String
    private let message: String
    
    var wrappedValue: String {
        get { value }
        set { value = newValue }
    }
    
    init(wrappedValue: String, message: String = "") {
        self.value = wrappedValue
        self.message = message
    }
}

// 使用属性包装器
class User {
    @Validate(message: "User data")
    var name: String = ""
    
    @available(*, deprecated)
    func oldMethod() {}
    
    override var description: String {
        return "User"
    }
}

5.6.3 总结

  1. 范型支持

    • Java、Kotlin、Swift提供了最完整的范型支持,包括类型约束和型变
    • Dart和ArkTS的范型支持相对基础,主要用于集合类型
    • Python通过typing模块提供了类型提示功能
  2. 注解机制

    • Java和Kotlin提供了最强大的注解系统,支持元注解和运行时反射
    • Dart和Python的注解/装饰器较为简单但实用
    • ArkTS和Swift的注解主要用于UI组件和属性包装
  3. 使用场景

    • 范型主要用于创建类型安全的容器类和通用算法
    • 注解主要用于元数据标记、编译时检查和运行时行为修改
    • 不同语言的实现方式反映了各自的设计理念和应用场景
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ya-Jun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值