수색…
통사론
- class Foo {} // Object로부터 상속받습니다.
- class Bar : Foo {} // Bar는 Foo입니다.
- Foo f = 새로운 Foo (); // 힙에 새 객체를 인스턴스화합니다.
비고
명세 를보고, 클래스 에 대한 책의 장을 살펴보고, 상속을 하고, 대화식으로 재생하십시오 .
계승
class Animal
{
abstract int maxSize(); // must be implemented by sub-class
final float maxSizeInMeters() // can't be overridden by base class
{
return maxSize() / 100.0;
}
}
class Lion: Animal
{
override int maxSize() { return 350; }
}
void main()
{
import std.stdio : writeln;
auto l = new Lion();
assert(l.maxSizeInMeters() == 3.5);
writeln(l.maxSizeInMeters()); // 3.5
}
인스턴스화
class Lion
{
private double weight; // only accessible with-in class
this(double weight)
{
this.weight = weight;
}
double weightInPounds() const @property // const guarantees no modifications
// @property functions are treated as fields
{
return weight * 2.204;
}
}
void main()
{
import std.stdio : writeln;
auto l = new Lion(100);
assert(l.weightInPounds == 220.4);
writeln(l.weightInPounds); // 220.4
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow