以下是使用.md格式分別實(shí)現(xiàn)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);
}
這些是簡單的繼承示例,用于介紹不同語言中的繼承概念掩完。具體的實(shí)現(xiàn)方式可能根據(jù)需求和語言特性有所差異噪漾。
本文由mdnice多平臺發(fā)布