खोज…


वाक्य - विन्यास

  • कक्षा फू {} // वस्तु से विरासत में मिली
  • क्लास बार: फू {} // बार एक फू भी है
  • Foo f = new 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