參考自d程序設計語言---個人博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow me編程
跟絕大部分opp編程語言的接口相似。
編程語言
d語言的接口能夠增長修飾符 private protected public ide
d語言也支持抽象類,使用abstract語法。this
d語言也支持,內中嵌套類,方法中嵌套類,
spa
class A{private DbObject _db; alias _db this;}這個是個很變態的語法,A能夠任意轉換成DbObject對象.net
import std.stdio; import std.array; interface light{ final void showLight(){ up(); down(); } /* private: void up(); void down(); */ void up(); void down(); } class PhoneLight : light{ int a = 0; //override private void up(){ override void up(){ a = 1; showstatus(); } //override private void down(){ override void down(){ a = 0; showstatus(); } void showstatus(){ writeln(a); } } class Shape{ abstract void draw(); } class RectangularShape : Shape{ abstract void drawFrame(); } class Rectangle:RectangularShape { override void draw(){ writeln("override draw"); } } class DbObject{ public void connect(){ writeln("connect to db"); } } class SolidRectangle:Rectangle{ private DbObject _db; alias _db this; this(){ _db = new DbObject(); } override void drawFrame(){ writeln("override drawFrame"); } } class Outer{ int x = 0; static int x1 = 1; void fun(int a){ writeln(a); } static class Inner2{ void func(){ writeln(x1); } } class Inner{ int y; void gun(){ fun(x+y); } } } void func(int x){ class Inner{ int y; void gun(){ writeln(x+y); } } auto iner = new Inner; iner.y =11; iner.gun(); } interface Stack(T){ @property bool empty(); @property ref T top(); void push(T value); void pop(); } class StackImpl(T):Stack!T{ private T[] _store; @property bool empty(){ return _store.empty; } @property ref T top(){ assert(!empty); return _store.back; } void push(T value){ _store ~= value; } void pop(){ assert(!empty); _store.popBack(); } } unittest { auto stack = new StackImpl!int; assert(stack.empty); stack.push(111); stack.push(222); assert(stack.top == 222); stack.pop(); assert(stack.top == 111); func(21); Outer outer = new Outer; auto obj = outer.new Inner; auto pl = new PhoneLight(); //pl.up(); //pl.down(); pl.showLight(); //auto rectangele = new Rectangle(); auto solid = new SolidRectangle(); solid.drawFrame(); solid.connect(); DbObject db = solid; db.connect(); }