使用go语言、rust、c语言、php、node、zig分别实现继承

以下是使用.md格式分别实现Go语言、Rust、C语言、PHP、Node.js和Zig的继承代码的示例:

Go语言

package main

import "fmt"

type Animal struct {
    name string
}

func (a Animal) Speak() {
    fmt.Println("Animal speaks...")
}

type Dog struct {
    Animal
    breed string
}

func (d Dog) Speak() {
    fmt.Println("Dog barks...")
}

func main() {
    animal := Animal{}
    animal.Speak()
    
    dog := Dog{Animal: Animal{name: "Tom"}, breed: "Labrador"}
    dog.Speak()
}

Rust

trait Speak {
    fn speak(&self);
}

struct Animal {
    name: String,
}

impl Speak for Animal {
    fn speak(&self) {
        println!("Animal speaks...");
    }
}

struct Dog {
    animal: Animal,
    breed: String,
}

impl Speak for Dog {
    fn speak(&self) {
        println!("Dog barks...");
    }
}

fn main() {
    let animal = Animal { name: String::from("Animal") };
    animal.speak();
    
    let dog = Dog { animal: Animal { name: String::from("Tom") }, breed: String::from("Labrador") };
    dog.speak();
}

C语言

#include 
#include 

typedef struct {
    void (*speak)(void*);
    char* name;
} Animal;

void animal_speak(void* animal) {
    printf("Animal speaks...\n");
}

Animal* animal_new() {
    Animal* animal = (Animal*)malloc(sizeof(Animal));
    animal->speak = animal_speak;
    return animal;
}

typedef struct {
    Animal animal;
    char* breed;
} Dog;

void dog_speak(void* dog) {
    printf("Dog barks...\n");
}

Dog* dog_new(char* name, char* breed) {
    Dog* dog = (Dog*)malloc(sizeof(Dog));
    dog->animal.name = name;
    dog->animal.speak = dog_speak;
    dog->breed = breed;
    return dog;
}

int main() {
    Animal* animal = animal_new();
    animal->speak(animal);
    
    Dog* dog = dog_new("Tom", "Labrador");
    dog->animal.speak(dog);
    
    return 0;
}

PHP

speak();

$dog = new Dog();
$dog->name = "Tom";
$dog->breed = "Labrador";
$dog->speak();
?>

Node.js

class Animal {
    constructor(name) {
        this.name = name;
    }
    
    speak() {
        console.log("Animal speaks...");
    }
}

class Dog extends Animal {
    constructor(name, breed) {
        super(name);
        this.breed = breed;
    }
    
    speak() {
        console.log("Dog barks...");
    }
}

const animal = new Animal();
animal.speak();

const dog = new Dog("Tom", "Labrador");
dog.speak();

Zig

const std = @import("std");

pub const Interface = struct {
    speak: fn(self: anytype) void,
};

pub const Animal = struct {
    interface: Interface,
    name: []u8,
};

pub const Dog = struct {
    animal: Animal,
    breed: []u8,
};

pub fn speak(animal: anytype) void {
    std.debug.print("Animal speaks...\n", .{});
}

pub fn animal_new() Animal {
    return Animal {
        .interface = Interface { .speak = speak },
        .name = "Animal",
    };
}

pub fn dog_new(name: []const u8, breed: []const u8) Dog {
    return Dog {
        .animal = Animal {
            .interface = Interface { .speak = speak },
            .name = name,
        },
        .breed = breed,
    };
}

pub fn main() !void {
    const animal = animal_new();
    animal.interface.speak(&animal);
    
    const dog = dog_new("Tom", "Labrador");
    dog.animal.interface.speak(&dog);
}

这些是简单的继承示例,用于介绍不同语言中的继承概念。具体的实现方式可能根据需求和语言特性有所差异。

本文由mdnice多平台发布

你可能感兴趣的:(后端)