C
C#2y ago
popcorn

✅ abstract class - default value

Hello, I have a simple code:
public abstract class Vehicle
{
public int speed = 1;

public void PrintSpeed()
{
Console.WriteLine(speed);
}
}

public class Car : Vehicle
{
public int speed = 9;

public void DoSomething()
{
// ....
PrintSpeed(); // Here it prints 1, but I want it to print 9
}
}
public abstract class Vehicle
{
public int speed = 1;

public void PrintSpeed()
{
Console.WriteLine(speed);
}
}

public class Car : Vehicle
{
public int speed = 9;

public void DoSomething()
{
// ....
PrintSpeed(); // Here it prints 1, but I want it to print 9
}
}
Is it possible to have an abstract class hold default parameter but when I override that then when I call the function it will use the overridden parameter in the child class?
5 Replies
phaseshift
phaseshift2y ago
you could use a constructor argument in the base class
popcorn
popcornOP2y ago
And it's not possible without constructor?
phaseshift
phaseshift2y ago
you could have a protected virtual method instead (or virtual property, I guess)
popcorn
popcornOP2y ago
Okay, virtual property is what Im looking for I think. Thanks
Denis
Denis2y ago
Consider closing this question using /close
Want results from more Discord servers?
Add your server