接口
定义接口并让类实现它,计算矩形面积。
C C23
C23
#include <stdio.h>
// 接口惯用法:仅函数指针的 vtable 结构体,实现类型提供具体函数
typedef struct {
int (*area)(const void *self);
} ShapeVTable;
typedef struct {
const ShapeVTable *vtable;
} Shape; // Shape* 是"接口引用",可指向任何实现类型
typedef struct {
Shape iface;
int width, height;
} Rectangle;
static int rectangle_area(const void *self) {
const Rectangle *r = (const Rectangle *)self;
return r->width * r->height;
}
static const ShapeVTable rectangle_vtable = { rectangle_area };
int main(void) {
Rectangle r = { { &rectangle_vtable }, 3, 4 };
Shape *s = &r.iface; // 经接口类型使用(动态分发)
printf("%d", s->vtable->area(s));
return 0;
}
输出
12
C++ C++26
C++26
#include <iostream>
class IShape { // 接口:只含纯虚函数(惯例以 I 前缀命名)
public:
virtual int area() const = 0;
virtual ~IShape() = default;
};
class Rectangle : public IShape {
int width, height;
public:
Rectangle(int width, int height) : width(width), height(height) {}
int area() const override { return width * height; }
};
int main() {
Rectangle r(3, 4);
std::cout << r.area() << "\n";
return 0;
}
输出
12
C# v11.0.100
v11.0.100
var r = new Rectangle(3, 4);
Console.WriteLine(r.Area());
interface IShape {
int Area(); // 只声明签名,不含实现
}
class Rectangle : IShape {
private readonly int width, height;
public Rectangle(int width, int height) { this.width = width; this.height = height; }
public int Area() => width * height;
}
输出
12
Go v1.27.4
v1.27.4
package main
import "fmt"
type Shape interface {
Area() int
}
type Rectangle struct {
width, height int
}
func (r Rectangle) Area() int {
return r.width * r.height
}
func main() {
var s Shape = Rectangle{width: 3, height: 4}
fmt.Println(s.Area())
}
输出
12
Java v27.0.1
v27.0.1
public class Main {
interface Shape {
int area();
}
static class Rectangle implements Shape {
int width, height;
Rectangle(int w, int h) { width = w; height = h; }
public int area() { return width * height; }
}
public static void main(String[] args) {
Shape s = new Rectangle(3, 4);
System.out.println(s.area());
}
}
输出
12
JavaScript v26.13.1
v26.13.1
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
function printArea(shape) {
console.log(shape.area());
}
printArea(new Rectangle(3, 4));
输出
12
PHP v8.6.0
v8.6.0
interface Shape {
public function area(): int;
}
class Rectangle implements Shape {
public function __construct(private int $width, private int $height) {}
public function area(): int {
return $this->width * $this->height;
}
}
$r = new Rectangle(3, 4);
echo $r->area();
输出
12
Python v3.15.2
v3.15.2
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
...
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
r = Rectangle(3, 4)
print(r.area())
输出
12
Rust v1.99.1
v1.99.1
trait Shape {
fn area(&self) -> i32;
}
struct Rectangle {
width: i32,
height: i32,
}
impl Shape for Rectangle {
fn area(&self) -> i32 {
self.width * self.height
}
}
fn main() {
let r = Rectangle { width: 3, height: 4 };
println!("{}", r.area());
}
输出
12
TypeScript v26.13.1
v26.13.1
interface Shape {
area(): number; // 只声明签名,不含实现
}
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(shape.area());
}
printArea(new Rectangle(3, 4));
输出
12
语言差异说明
C:无类机制,示例为 struct + 函数指针 vtable 的 OOP-in-C 惯用法,语义由约定保证。
Go:接口为隐式实现(无需声明 implements,实现其全部方法即满足接口)。
JavaScript:无接口、靠鸭子类型(duck typing)。
PHP:用 interface 关键字、类 implements 多个接口。
Python:用 abc.ABC + @abstractmethod 模拟接口(鸭子类型更常见)。
Rust:用 trait 表达接口抽象。
TypeScript:提供 interface。