C#C
C#3y 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:
  3. Abstract Player Class```csabstract 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()); ```
  4. 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; } }```
Was this page helpful?