OO programming style - virtual property, constructor parameter, or protected setter

If I have a base class ( MyBasein the example below) that needs subclasses to provide / override the value ( Value), is there a preferred way to implement it? I can come up with several ways to do this, but I don’t really feel why I would choose one by one. I have some examples (in C #, but they can be any OO language).

Concrete property in the base class . Subclasses can set this value if they wish.

public class MyBase
{
    protected string Value { get; set; }

    protected MyBase()
    {
        Value = "Default Value";
    }

    private DoSomethingWithValue()
    {
         Console.WriteLine(Value);
    }
}

public class MySub : MyBase
{
    public MySub()
    {
        Value = "Overridden Value";
    }
}

Virtual property . Subclasses can override this if they wish.

public class MyBase
{
    protected virtual string Value { get { return "Default Value"; } }

    protected MyBase()
    {
    }

    private DoSomethingWithValue()
    {
         Console.WriteLine(Value);
    }
}

public class MySub : MyBase
{
    protected override string Value { get { return "Overridden Value"; } }

    public MySub()
    {
    }
}

The property specified in the constructor of the base class . Subclasses can provide value.

public class MyBase
{
    protected string Value { get; private set; }

    protected MyBase(string value)
    {
         Value = value;
    }

    protected MyBase() : this("Default Value")
    {
    }

    private DoSomethingWithValue()
    {
         Console.WriteLine(Value);
    }
}

public class MySub : MyBase
{
    public MySub() : base("Overridden Value")
    {
    }
}

, . , , ?

+3
2

, ?

, , , , return "constant";, .

0

OO . , , . , , , ( ).

, , . , , . , , , . , , .

( ++, ):

class Rational {
  int numerator; int denominator;
public: 
  Rational (int x, int y) { 
    if (y == 0) { throw DivisionByZero; }
    assert (y != 0);
    if (y<0) { x = -x; y = -y; }
    assert (y>0);
    int d = gcd(x,y);
    assert(d>0); // proof, from specs of gcd
    // assert: if there exists an integer q>0 which divides both x/d and y/d
    // then q == 1 (from definition of gcd)

    assert (x % d == 0); // d divides x exactly (from defn of gcd)
    assert (y % d == 0); // d divides y exactly (from defn of gcd)

    numerator = x/d;
    denominator = y/d;

    // assert: provided y != 0, numerator / denominator (math div, not C div)
    // equals input x/ input y (math div, not C div).

    // invariant: denominator > 0, gcd (numerator, denominator) == 1
    // Theorem: representation is unique
  }

  bool eq(Rational other) { 
    numerator == other.numerator && denominator == other.denominator;
    // sufficient by uniqueness theorem
  }
}:

, , , , . , . , . , .

:

R: int * int - { x,y | y<0 or Exists d. d divides y and d divides x }

, , R ,

R < == > A

A - , .

: . , , , OO, , , , , , ( ​​, ).

- , , , .

Yttrill: ..dont!

, !

-1

Source: https://habr.com/ru/post/1777066/


All Articles