关于后端:使用go语言rustc语言phpnodezig分别实现继承

4次阅读

共计 2639 个字符,预计需要花费 7 分钟才能阅读完成。

以下是应用.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 <stdio.h>
#include <stdlib.h>

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

<?php

interface Speak {public function speak();
}

class Animal implements Speak {
    public $name;
    
    public function speak() {echo "Animal speaks...\n";}
}

class Dog extends Animal {
    public $breed;
    
    public function speak() {echo "Dog barks...\n";}
}

$animal = new Animal();
$animal->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 多平台公布

正文完
 0