C
C#17mo ago
Gyoei

✅ Difficulty understanding Inheritance and where to use Constructor

Hello people. I'm learning OOP and I've come across this problem. I'm creating a Football team creator console app. I have a abstract class called Player which has the base properties. I have four inherited classes (Defender, Midfielder, etc) which all have different skill properties(Passing, Shooting etc). I have two Questions: 1. Where(in Player or in inherited classes) and how do I put the constructor so it inherits all the properties of Player. 2. I have a DisplayInfo() method in Player Class. I want to use this method in inhertied classes but add additional functionalities to display the skill properties. Code: 1. Abstract Player Class
abstract class Player
{
//base properties
internal string Name { get; }
internal string Position { get; }
internal string Nationality { get; }

//physical properties
//all players are assigned these randomly
public int Pace { get { return SetPhysicalProperties(60,90); } }
public int Strength { get { return SetPhysicalProperties(60,90); } }

//Methods
static int SetPhysicalProperties(int min, int max)
{
var rand = new Random();
return rand.Next(min, max);
}

//display Info Method
public void DisplayInfo()
{
var info = new StringBuilder();
info.AppendLine("-----Personal Details----");
info.AppendLine($"Name: {this.Name}\tPosition: {this.Position}\t,Nationality: {this.Nationality}");
info.AppendLine("----Physical Attributes----");
info.AppendLine($"Pace: {this.Pace}\tStrength: {this.Strength}\t");
System.Console.WriteLine(info.ToString());
abstract class Player
{
//base properties
internal string Name { get; }
internal string Position { get; }
internal string Nationality { get; }

//physical properties
//all players are assigned these randomly
public int Pace { get { return SetPhysicalProperties(60,90); } }
public int Strength { get { return SetPhysicalProperties(60,90); } }

//Methods
static int SetPhysicalProperties(int min, int max)
{
var rand = new Random();
return rand.Next(min, max);
}

//display Info Method
public void DisplayInfo()
{
var info = new StringBuilder();
info.AppendLine("-----Personal Details----");
info.AppendLine($"Name: {this.Name}\tPosition: {this.Position}\t,Nationality: {this.Nationality}");
info.AppendLine("----Physical Attributes----");
info.AppendLine($"Pace: {this.Pace}\tStrength: {this.Strength}\t");
System.Console.WriteLine(info.ToString());
2. inherited Defender Class
internal class Defender : Player
{
public int Tackling { get; private set; }
public int SlideTackle { get; private set; }
public int Header { get; private set; }
}
internal class Defender : Player
{
public int Tackling { get; private set; }
public int SlideTackle { get; private set; }
public int Header { get; private set; }
}
3 Replies
Angius
Angius17mo ago
1. Do you mean the base constructor?
class Foo
{
private int _a;
public Foo(int a)
{
_b = b;
}
}
class Bar : Foo
{
private int _b;
public Bar(int a, int b): base(a)
{
_b = b;
}
}
class Foo
{
private int _a;
public Foo(int a)
{
_b = b;
}
}
class Bar : Foo
{
private int _b;
public Bar(int a, int b): base(a)
{
_b = b;
}
}
2. Parent has no knowledge of its children. You can make the method virtual maybe so it can be overriden in the children
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Gyoei
GyoeiOP17mo ago
thanks a lot. i got it now with base constructor
Want results from more Discord servers?
Add your server