I have a base class like this:
public class Trajectory{
public int Count { get; set; }
public double Initial { get; set { Count = 1; } }
public double Current { get; set { Count ++ ; } }
}
So, I have code in the base class, which makes the set-s virtual, but the get-s must stay abstract. So I need something like this:
...
public double Initial { abstract get; virtual set { Count = 1; } }
...
But this code gives an error. The whole point is to implement the counter functionality in the base class instead in all the derived classes. So, how can I make the get and set of a property with different modifiers?
Yes, abstract methods are virtual by definition; they must be overridable in order to actually be overridden by subclasses: When an instance method declaration includes an abstract modifier, that method is said to be an abstract method.
Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and force the derived classes to override the method. So, abstract methods have no actual code in them, and (non-abstract) subclasses HAVE TO override the method.
An abstract property declaration does not provide an implementation of the property accessors -- it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.
C# Abstract Class FeaturesAn Abstract class can have constants and fields.
split it into 2 functions:
public double Initial
{
get { return GetInitial(); }
set { SetInitial(value); }
}
protected virtual void SetInitial(double value)
{
Count = 1;
}
protected abstract double GetInitial();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With