Перенос страниц
This commit is contained in:
parent
4d57446057
commit
4c954c9186
129 changed files with 14 additions and 15 deletions
2655
book/06-классы-объектно-ориентированный-стиль/README.md
Normal file
2655
book/06-классы-объектно-ориентированный-стиль/README.md
Normal file
File diff suppressed because it is too large
Load diff
Binary file not shown.
After Width: | Height: | Size: 34 KiB |
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
Binary file not shown.
After Width: | Height: | Size: 55 KiB |
Binary file not shown.
After Width: | Height: | Size: 9.3 KiB |
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
|
@ -0,0 +1,43 @@
|
|||
class Widget
|
||||
{
|
||||
// Константа
|
||||
enum fudgeFactor = 0.2;
|
||||
|
||||
// Разделяемое неизменяемое значение
|
||||
static immutable defaultName = "A Widget";
|
||||
|
||||
// Некоторое состояние, определенное для всех экземпляров класса Widget
|
||||
string name = defaultName;
|
||||
uint width, height;
|
||||
|
||||
// Статический метод
|
||||
static double howFudgy()
|
||||
{
|
||||
return fudgeFactor;
|
||||
}
|
||||
|
||||
// Метод
|
||||
void changeName(string another)
|
||||
{
|
||||
name = another;
|
||||
}
|
||||
|
||||
// Метод, который нельзя переопределить
|
||||
final void quadrupleSize()
|
||||
{
|
||||
width *= 2;
|
||||
height *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
// Обратиться к статическому методу класса Widget
|
||||
assert(Widget.howFudgy() == 0.2);
|
||||
// Создать экземпляр класса Widget
|
||||
auto w = new Widget;
|
||||
// Поиграть с объектом типа Widget
|
||||
assert(w.name == w.defaultName); // Или Widget.defaultName
|
||||
w.changeName("Мой виджет");
|
||||
assert(w.name == "Мой виджет");
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
class Outer
|
||||
{
|
||||
int x;
|
||||
|
||||
class Inner
|
||||
{
|
||||
int y;
|
||||
|
||||
this()
|
||||
{
|
||||
x = 42;
|
||||
// x – то же, что this.outer.x
|
||||
assert(this.outer.x == 42);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
auto outer = new Outer;
|
||||
auto inner = outer.new Inner;
|
||||
assert(outer.x == 42); // Вложенный объект inner изменил внешний объект outer
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
import std.stdio : writeln;
|
||||
|
||||
class Shape
|
||||
{
|
||||
protected string _name;
|
||||
abstract void print();
|
||||
}
|
||||
|
||||
class DBObject
|
||||
{
|
||||
protected string _name;
|
||||
abstract void print();
|
||||
void saveState()
|
||||
{
|
||||
writeln(_name);
|
||||
}
|
||||
}
|
||||
|
||||
class StorableShape : Shape
|
||||
{
|
||||
private class MyDBObject : DBObject
|
||||
{
|
||||
this(string name)
|
||||
{
|
||||
_name = name;
|
||||
}
|
||||
|
||||
override void print()
|
||||
{
|
||||
writeln(_name);
|
||||
}
|
||||
|
||||
final override void saveState()
|
||||
{
|
||||
writeln(this.outer._name, "; ", _name);
|
||||
}
|
||||
}
|
||||
|
||||
private MyDBObject _store;
|
||||
alias _store this;
|
||||
|
||||
this(string outName, string inName)
|
||||
{
|
||||
_name = outName;
|
||||
_store = new MyDBObject(inName);
|
||||
}
|
||||
|
||||
override void print()
|
||||
{
|
||||
writeln(_name);
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto s = new StorableShape("first", "second");
|
||||
s.print(); // StorableShape._name
|
||||
s._store.print(); // StorableShape.MyDBObject._name
|
||||
s.saveState(); // StorableShape._name and StorableShape.MyDBObject._name
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
import std.array;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto stack = new StackImpl!int;
|
||||
assert(stack.empty);
|
||||
stack.push(3);
|
||||
assert(stack.top == 3);
|
||||
stack.push(5);
|
||||
assert(stack.top == 5);
|
||||
stack.pop();
|
||||
assert(stack.top == 3);
|
||||
stack.pop();
|
||||
assert(stack.empty);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
import std.stdio;
|
||||
|
||||
class A
|
||||
{
|
||||
int x = 42;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
{
|
||||
auto a1 = new A;
|
||||
assert(a1.x == 42);
|
||||
auto a2 = a1;
|
||||
a2.x = 100;
|
||||
assert(a1.x == 100);
|
||||
}
|
||||
{
|
||||
auto a1 = new A;
|
||||
auto a2 = new A;
|
||||
a1.x = 100;
|
||||
a2.x = 200;
|
||||
// Заставим a1 и a2 обменяться привязками
|
||||
auto t = a1;
|
||||
a1 = a2;
|
||||
a2 = t;
|
||||
assert(a1.x == 200);
|
||||
assert(a2.x == 100);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
import std.math;
|
||||
|
||||
class Test
|
||||
{
|
||||
double a = 0.4;
|
||||
double b;
|
||||
|
||||
this(int b)
|
||||
{
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
this() {}
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
auto t = new Test(5);
|
||||
auto z = new Test;
|
||||
|
||||
assert(t.b == 5);
|
||||
assert(isNaN(z.b));
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
import std.math;
|
||||
|
||||
class Test
|
||||
{
|
||||
double a = 0.4;
|
||||
double b;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
// Объект создается с помощью выражения new
|
||||
auto t = new Test;
|
||||
assert(t.a == 0.4 && isNaN(t.b));
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
class Widget
|
||||
{
|
||||
this(Widget source)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Widget duplicate()
|
||||
{
|
||||
return new Widget(this); // Выделяет память и вызывает this(Widget)
|
||||
}
|
||||
}
|
||||
|
||||
class TextWidget : Widget
|
||||
{
|
||||
this(TextWidget source)
|
||||
{
|
||||
super(source);
|
||||
}
|
||||
|
||||
override TextWidget duplicate()
|
||||
{
|
||||
return new TextWidget(this);
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
TextWidget tw;
|
||||
TextWidget clone = tw.duplicate();
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
class Contact
|
||||
{
|
||||
string bgColor()
|
||||
{
|
||||
return "Серый";
|
||||
}
|
||||
}
|
||||
|
||||
class Friend : Contact
|
||||
{
|
||||
string currentBgColor = "Светло-зеленый";
|
||||
string currentReminder;
|
||||
|
||||
this(ref string c)
|
||||
{
|
||||
currentBgColor = c;
|
||||
}
|
||||
|
||||
override string bgColor()
|
||||
{
|
||||
return currentBgColor;
|
||||
}
|
||||
|
||||
string reminder()
|
||||
{
|
||||
return currentReminder;
|
||||
}
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
string startColor = "Синий";
|
||||
Friend f = new Friend(startColor);
|
||||
Contact c = f; // Подставить экземпляр класса Friend вместо экземпляра класса Contact
|
||||
auto color = c.bgColor(); // Вызвать метод класса Friend
|
||||
|
||||
import std.stdio : writeln;
|
||||
writeln(color);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
module test;
|
||||
|
||||
class Widget {}
|
||||
|
||||
unittest
|
||||
{
|
||||
assert((new Widget).toString() == "test.Widget");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue