I am a student at Westwood college, and I am having trouble understanding inheritance in C#
I understand all of the parent class and child class rules, and the syntax. What has really been giving me issues is trying to pass control from one child class to another.
Perhaps I should elaborate:
I have my base class "character" and three derived classes "warrior", "mage", and "thief." Each of these classes has a special void function only it can access, like determining strength for the warrior, intellect for the mage, and dexterity for the thief. I am trying to figure out how to pass control from an void function input, where the user is given a choice between the three chracters, makes a choice, and then control passes to that chracter for the remainder of the program.
Ill post my code, it is very short as right now it is just in a command window, I havent added the GUI yet.
//charcter.cs
using System;
public class character
{
public int attribute;
public int healthPoints;
public string name;
public string species;
public char userChoice;
public void BaseCharacter(int atr, int hp, string nm, string spc)
{
attribute = atr;
healthPoints = hp;
name = nm;
species = spc;
}
public int Attributes
{
get
{
return attribute;
}
set
{
attribute = value;
}
}
public int HealthPoints
{
get
{
return healthPoints;
}
set
{
healthPoints = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Species
{
get
{
return species;
}
set
{
species = value;
}
}
public void calcPoints()
{
Random randomNumbers = new Random();
healthPoints = randomNumbers.Next(100);
}
public void display()
{
Console.WriteLine("Welcome to Heroes BETA.");
}
public void nameDisplay()
{
Console.WriteLine("Please type your name: ");
name = Console.ReadLine();
Console.WriteLine("Hello {0}", name);
}
}//end character.cs
//warrior .cs is empty at the moment
//main.cs
sing System;
public class main
{
public static void Main(string[] args)
{
character warChar = new character();
warChar.display();
warChar.nameDisplay();
Console.WriteLine("Please choose your character.\n'w' for warrior.\n'm' for mage.\n't' for thief.");
Console.ReadLine();
if (Console.ReadLine() = 'w')
character warChar = new character();//error CS1023
}
Any help to just give me the syntax for something like this would be greatly appreciated!
}
Loading
Satya DevPosted Sep 15, 2008, 4:33 PM
I would design like this:
public abstract Character
{
// Action is a delegate that takes NO parameters and returns VOID that is defined by .Net in System name space
// There more variation on Action like Action, Action etc. Refer to MSDN.
public abstract Action SpecialSkill
{
get;
}
...
}
// Please define other Characters like "mage:, "king" "queen" or whatever you like
// similar to how I did below:
public class Theif: Character
{
public Action SpecialSkill
{
get{return Dextiry;}
}
public void ToString(){return "Theif";}
public void Dextiriy()
{
}
}
public static void Main()
{
List characters = new List{new Theif(), new Mage(), ...};
Character character = GetSelectedCharacter();
character.SpecialSkill();
}
AlanPosted Sep 15, 2008, 2:39 PM
Well, if you change the Main() method to this, then it should compile and run OK:
public static void Main(string[] args)
{
character warChar = new character();
warChar.display();
warChar.nameDisplay();
Console.WriteLine("Please choose your character.\n'w' for warrior.\n'm' for mage.\n't' for thief.");
char choice;
while(true)
{
string input = Console.ReadLine();
if(input.Length == 1)
{
choice = input.ToLower()[0]; // allows for upper/lower case characters
if (choice =='w' || choice == 'm' || choice == 't')
{
break; // OK to proceed
}
}
Console.WriteLine("Invalid input, please re-enter.");
}
warChar.userChoice = choice;
Console.ReadLine(); // to prevent console closing till enter pressed
}
However, if each class has a uniquely named void method, then you'll need to create both variables and objects of those classes (rather than the base class) in order to call that method.
An alternative approach would be to use a method which has the same name and parameter signature in each class including the base class. If the method were defined as 'virtual' in the base class and 'override' in the derived classes, you could then write code like this:
character warChar = new Warrior();
warChar.MyMethod();
The version of MyMethod in the Warrior class would then get called because the runtime type of warChar was Warrior rather than character. This is what's called polymorphism and hopefully you'll have come across it already in the course of your studies.