I'm currently working on a very simple Pokémon application in C#. I was uncertain on how to set up the relationship between the Pokémon and their fighting type but I just ended up doing the following:
abstract class Pokemon
{
private int number;
private string name;
protected string weakness;
protected string strength;
public Pokemon(int number, string name)
{
this.number = number;
this.name = name;
weakness = "none";
strength = "none";
}
}
I then made seperate classes that inherit Pokemon only to change the specific weakness and strength of the type.
class Fire : Pokemon
{
public Fire(int number, string name) : base(number, name)
{
weakness = "Water";
strength = "Grass";
}
}
Is it bad etiquette to create subclasses with the sole purpose of simply changing some of the initial values of the parent?
From here on, I just intend on doing the following when "creating" Pokémon for the application
pokemon = new Pokemon[6];
pokemon[0] = new Grass(001, "Bulbasaur");
pokemon[1] = new Grass(002, "Ivysaur");
pokemon[2] = new Grass(003, "Venusaur");
pokemon[3] = new Fire(004, "Charmander");
pokemon[4] = new Fire(005, "Charmeleon");
pokemon[5] = new Fire(006, "Charizard");
Any and all advice on how to improve the application or how to use inheritance properly is much appreciated :)
The inheritance looks OK but you may do some improvements. First, you don't need to define protected fields for weakness and strength, use protected properties instead. Second, using string type for weakness/strength doesn't seem to be the best choice. I would go with an Enum type.
enum PokemonComponent {
Water,
Grass
}
abstract class Pokemon
{
private int number;
private string name;
protected Pokemon(int number, string name)
{
this.number = number;
this.name = name;
}
protected abstract PokemonComponent Weakness {
get;
}
protected abstract PokemonComponent Strength {
get;
}
}
class Fire : Pokemon
{
public Fire(int number, string name) : base(number, name)
{
}
protected override PokemonComponent Weakness {
get {
return PokemonComponent.Water;
}
}
protected override PokemonComponent Strength {
get {
return PokemonComponent.Grass;
}
}
}
Related
I am trying to get bug to work, but am having no luck, the parts marked as abstract need to stay that way seeing as it is specified as such in the question I am trying to answer.
abstract class Worker
{
protected string name;
protected int ID;
protected abstract int expLevel(); // create abstract property for experience field
public abstract string Experience(int expLevel); //create abstract method called experience
public Worker(string name, int ID) //constructor for worker
{
this.name = name;
this.ID = ID;
}
public Worker() { } // how I tried to fix error
}
class Labourer : Worker
{
Worker worker1 = new Worker(); // line in which bug occurs
protected override int expLevel()
{
return expLevel();
}
public override string Experience(int expLevel) // returns strings to be used later
{
if (expLevel > 5)
{
return "Senior";
}
return "Junior";
}
}
}
You should add a constructor in your derived class as well that calls the base-class one:
abstract class Worker
{
protected string name;
protected int ID;
protected abstract int expLevel(); // create abstract property for experience field
public abstract string Experience(int expLevel); //create abstract method called experience
public Worker(string name, int ID) //constructor for worker
{
this.name = name;
this.ID = ID;
}
// no need for a parameter-less ctor here
}
class Labourer : Worker
{
public Labourer(string name, int ID) : base(name, id) { }
protected override int expLevel()
{
return expLevel();
}
public override string Experience(int expLevel) // returns strings to be used later
{
if (expLevel > 5)
{
return "Senior";
}
return "Junior";
}
}
Now within your client-code you can instantiate a worker like this:
Worker worker = new Labourer("Me", 1);
You can not create an instance of an abstract type (Worker).
When you create a concrete type based on an abstract type, like you do in the Labourer class, you can create an instance of that type.
Labourer labourer = new Labourer();
I am not sure you what your intent is with the new Worker() but I can only guess.
Do you want to call the constructor of the Worker base class? You can do it like this:
class Labourer : Worker
{
public Labourer(string name, string id) : base(name, string)
{
}
// rest of implementation
}
I have a base class called Message like this:
public abstract class Message
{
protected int m_id;
protected bool m_localized;
protected string m_metaData;
public int GetID() { return m_id; }
public bool GetLocalized() { return m_localized; }
public string GetMetadata() { return m_metaData; }
}
Then, i have two more classes that inherit from Message for example:
public class ClassicMessage : Message
{
private string m_title;
private string m_content;
public void SetTitle(string title) { m_title = title; }
public void SetContent(string content) { m_content = content; }
public string GetTitle() { return m_title; }
public string GetContent() { return m_content; }
}
public class MessageWithCustomContent : Message
{
private List<CustomContent> m_content;
public MessageWithCustomContent()
{
m_content = new List<CustomContent>();
}
public List<CustomContent> GetContent()
{
return m_content;
}
public CustomContent GetContentEntry(int id)
{
return m_content.find(x => x.ID.Equals(id));
}
}
public class CustomContent
{
private int m_id;
public int ID { get; set { m_id = value; } }
private string m_body;
public string Body { get { return m_body; } set { m_body = value; }
private Image m_image;
public Image Image { get { return m_image; } set { m_image = value; } }
}
In a case like this, how can i unify the app interface if the derived classes has similar methods but these methods have different return types? (even when the methods try to do the same)
I know that with the example i'm breaking the Liskov Substitution Principle and the Open/Closed principle, what's the best approach to get around with that?
Thanks for your help!
Edit:
For more clarity, what i'm trying to achieve is to create a common interface to manage all the possible messages as the base "Message", because i want to avoid using typeof in the consumer class.
for example:
if(message is MessageWithCustomContent)
{
// do something with the contents.
}
else if(message is MessageWithCustomContent)
{
// do another thing with the contents.
}
etc...
You could change Message to be generic, and the T would specify the Content return type. See example below.
Edit
You could use a "IMessage" and a "Message: IMessage" as base.
You would then be able to create a IMessage list like so
var messages = new List<IMessage>
{
new ClassicMessage(),
new MessageWithCustomContent()
};
foreach (var message in messages)
{
message.GetContent();
}
Below is how the implementation of IMessagecould be done.
public interface IMessage
{
int GetID();
bool GetLocalized();
string GetMetadata();
object GetContent();
}
public abstract class Message<T> : IMessage
{
protected int m_id;
protected bool m_localized;
protected string m_metaData;
public int GetID() { return m_id; }
public bool GetLocalized() { return m_localized; }
public string GetMetadata() { return m_metaData; }
object IMessage.GetContent()
{
return GetContent();
}
public abstract T GetContent();
}
public class ClassicMessage : Message<string>
{
private string m_title;
private string m_content;
public void SetTitle(string title) { m_title = title; }
public void SetContent(string content) { m_content = content; }
public string GetTitle() { return m_title; }
public override string GetContent()
{
return m_content;
}
}
public class MessageWithCustomContent : Message<List<CustomContent>>
{
private List<CustomContent> m_content;
public MessageWithCustomContent()
{
m_content = new List<CustomContent>();
}
public CustomContent GetCustomContent(int id)
{
return null;
}
public override List<CustomContent> GetContent()
{
return m_content;
}
}
public class CustomContent
{
private int m_id;
public int ID { get; set; }
private string m_body;
public string Body
{
get { return m_body; }
set { m_body = value; }
}
}
I will explain how you break LSP below but before I do that, you are not really doing any inheriting. Yes you are declaring your classes to be inheriting but you are not really inheriting anything. So before learning LSP, perhaps you need to get a grip on inheritance firstly.
How do I know if I am breaking LSP?
Lest say your Message class was like this, notice the virtual and abstract methods:
public abstract class Message
{
protected int m_id;
protected bool m_localized;
protected string m_metaData;
public virtual int GetID() { return m_id; }
public virtual bool GetLocalized() { return m_localized; }
public abstract string GetMetadata();
}
Create a list like this:
var messages = new List<Message>();
Then add concrete types to that list of all the inheriting types. Then do this:
foreach(var thisMessage in messages)
{
var id = thisMessage.GetID();
var loc = GetLocalized();
var meta = GetMetadata();
}
If you get no exception thrown because one of the inheriting classes decided it does not need one of those methods, then you have not broken LSP. The idea is that if something is inheriting Message, then it should inherit everything. Otherwise, we cannot safely and with confidence substitute the inherited one for the parent one.
The reason this principle is important is because there may be existing code which is using Message, as shown in the foreach above, where it is treating all the types polymorphically and a developer decides to inherit it like this:
public abstract class BadMessage
{
public override int GetID()
{
throw new InvalidOperationException
("This method is not needed for BadMessage and should not be called");
}
public override bool GetLocalized() { ... }
public override string GetMetadata() { ... }
}
You see this will break existing code. And the worst part is, the compiler will not even be able to catch it, until it surfaces like an ugly bug in production.
Well, you're missing the interface methods in de base class. Abstract functions, that get implemented in the derivative classes. If you get a Message, not knowing what kind it is, how would you request its contents?
You could add derivative-specific methods to your base, but you'd have to implement an not_implemented exception in a virtual implementation in the base class to compensate for all derivatives not implementing it, and add exception handling. But then you should ask yourself: " is this class really a derivative? What do I want to achieve."
It might be a silly question, but i'm new to C#. I am wondering if there is a way to use directly parent's attributes in child class. I did a lot of Eiffel and when a class is inherited by one or more classes(cause yes Eiffel don't have interfaces you can inherit multiple classes).
Just like that exemple:(Eiffel langage)
Parent Class:
class Parent
features
int id
string lastName
Child Class:
class Child inherit
PARENT
feature
bool isChild
In that case, Child class already got access to id and lastName and can be set directly as part of Child attributes, don't have to create a Parent.
But so far i made this(C# langage):
Parent class:
public class Character
{
Int32 Id;
String name;
List<String> images;
public Character()
{
name = "";
images = null;
}
public Character(string a_name, List<String> imagePaths)
{
name = a_name;
images = imagePaths;
}
public Character(Int32 a_id, string a_name, List<String> imagePaths)
{
Id = a_id;
name = a_name;
images = imagePaths;
}
}
Child class:
public class NPC : Character
{
public bool isVender;
public NPC()
{
Character character = new Character();
isVender = false;
}
public NPC(string a_name, List<String> images)
{
Character caracter = new Character(a_name, images);
isVender = false;
}
public NPC(string a_name, List<string> images, bool a_bool)
{
Character caracter = new Character(a_name, images);
isVender = a_bool;
}
}
So there is my question, is there a way to get acces directly to parent's attributes in C# just like Eiffel?
Declare the fields you want to use in child classes as protected. Learn more about protected visibility modifier here.
public class Character
{
protected Int32 Id;
protected String name;
protected List<String> images;
public Character()
{
name = "";
images = null;
}
public Character(string a_name, List<String> imagePaths)
{
name = a_name;
images = imagePaths;
}
public Character(Int32 a_id, string a_name, List<String> imagePaths)
{
Id = a_id;
name = a_name;
images = imagePaths;
}
}
Then you can use the protected fields in child classes.
What you really seem to want is to use the base class's constructor to set the base class's fields, which is a slightly different syntax in C#:
public class NPC : Character
{
public bool isVender;
public NPC() : base()
{
isVender = false;
}
public NPC(string a_name, List<String> images) : base(a_name, images)
{
isVender = false;
}
public NPC(string a_name, List<string> images, bool a_bool) : base(a_name, images)
{
isVender = a_bool;
}
}
is there a way to get acces directly to parent's attributes in C# just like Eiffel?
If you do not specify an accessibility for a class member, they are private by default, which means that they are only accessible within the class itself. So the simplest change is to make them protected.
public class Character
{
protected Int32 Id;
protected String name;
protected List<String> images;
A better solution may be to add protected properties on top of the fields, which will still protect the actual fields but allow inherited classes to change the behavior of the properties.
My brain is gonna to explode. :) So I would like to get help from you.
Please, think about my question like about just programmer puzzle. (Actually. perhaps it is very easy question for you, but not for me.)
It is needed to create array of objects. For example List where T is class. (I will describe Class T below). Also it is needed create “container” that will contain this array and some methods for work with this array. For example Add(), Remove(int IndexToRemove).
Class T must have field "Container", this way each elements of our array would be able to know where is it contained and has access its container's fields and methods. Notice, that in this case Class T should have type parameter. Indeed, it is not known beforehand which container's type is used.
Let us denote this class container as A and class element (class T) as AUnit.
Code:
class Program
{
static void Main(string[] args)
{
A a = new A();
a.Add();
a.Units[0].SomeField +=100;
Console.ReadKey();
}
}
class A
{
public List<AUnit> Units;
public A()//ctor
{
Units = new List<AUnit>();
}
public void Add()
{
this.Units.Add(new AUnit(this));
}
}
class AUnit
{
public int SomeField;
public A Container;
public string Name { get; private set; }
public AUnit(A container)
{
this.SomeField = 43;
this.Container = container;
this.Name = "Default";
}
}
Public fields should be protected or private of course, but let think about this later.
You can ask “why we create public A Container field in AUnit”? We create field public string Name{get;private set;} (actually property but nevermind). And also we would like to be able to change value of this field for example method [Class AUnit] public bool Rename(string newName)();. The main idea of this method is changing Name field only that case if no one element in array (public List Units; ) has the same name like newName. But to achieve this, Rename method has to have access to all names that is currently used. And that is why we need Container field.
Code of extended version AUnit
class AUnit
{
public int SomeField;
public A Container;
public string Name { get; private set; }
public AUnit(A container)
{
this.SomeField = 43;
this.Container = container;
this.Name = "Default";
}
public bool Rename(String newName)
{
Boolean res = true;
foreach (AUnit unt in this.Container.Units)
{
if (unt.Name == newName)
{
res = false;
break;
}
}
if (res) this.Name = String.Copy(newName);
return res;
}
}
Ok. If you still read it let's continue. Now we need to create Class B and class BUnit which will be very similar like Class A and Class Aunit. And finally the main question of this puzzle is HOW WE CAN DO IT? Of course, I can CopyPaste and bit modify A and AUnit and create this code.
class B
{
public List<BUnit> Units; //Only Type Changing
public B()//ctor Name changing...
{
Units = new List<BUnit>();//Only Type Changing
}
public void Add()
{
this.Units.Add(new BUnit(this));//Only Type Changing
}
}
class BUnit
{
public int SomeField;
public B Container;//Only Type Changing
public string Name { get; private set; }
public A a; //NEW FIELD IS ADDED (just one)
public BUnit(B container) //Ctor Name and arguments type changing
{
this.SomeField = 43;
this.Container = container;
this.Name = "Default";
this.a=new A(); //New ROW (just one)
}
public bool Rename(String newName)
{
Boolean res = true;
foreach (BUnit unt in this.Container.Units) //Only Type Changing
{
if (unt.Name == newName)
{
res = false;
break;
}
}
if (res) this.Name = String.Copy(newName);
return res;
}
}
And I can to use this classes this way.
static void Main(string[] args)
{
B b = new B();
b.Add();
b.Units[0].a.Add();
b.Units[0].a.Units[0].SomeField += 100;
bool res= b.Units[0].a.Units[0].Rename("1");
res = b.Units[0].a.Units[0].Rename("1");
Console.ReadKey();
}
This construction is can be used to create “non-homogeneous trees”.
Help, I need somebody help, just no anybody…. [The Beatles]
I created B and BUnit using CopyPaste.
But how it can be done using “macro-definitions” or “Generic”, inherit or anything else in elegant style? (C# language)
I think that there is no reason to describe all my unsuccessful attempts and subquestions. Already topic is too long. : )
Thanks a lot if you still read it and understand what I would like to ask.
You need to implement a base type, lets call it UnitBase, with all common functionality. I'd structure your code the following way:
Create an interface for your container, this way you can change implementation to more performant solutions without modifying the elements you will be adding to the container.
public interface IContainer
{
Q Add<Q>() where Q : UnitBase, new();
IEnumerable<UnitBase> Units { get; }
}
Following the idea stated in 1, why not make the search logic belong to the container? It makes much more sense, as it will mostly depend on how the container is implemented:
public interface IContainer
{
Q Add<Q>() where Q : UnitBase, new();
IEnumerable<UnitBase> Units { get; }
bool Contains(string name);
}
A specific implementation of IContainer could be the following:
public class Container : IContainer
{
public Container()
{
list = new List<UnitBase>();
}
private List<UnitBase> list;
public Q Add<Q>() where Q: UnitBase, new()
{
var newItem = Activator.CreateInstance<Q>();
newItem.SetContainer(this);
list.Add(newItem);
return newItem;
}
public IEnumerable<UnitBase> Units => list.Select(i => i);
public bool Contains(string name) =>
Units.Any(unit => unit.Name == name);
}
Create a base class for your AUnit and BUnit types condensing all common functionality:
public abstract class UnitBase
{
protected UnitBase()
{
}
public IContainer Container { get; private set; }
public int SomeField;
public string Name { get; private set; }
public void SetContainer(IContainer container)
{
Container = container;
}
public bool Rename(String newName)
{
if (Container.Contains(newName))
return false;
this.Name = newName; //No need to use String.Copy
return true;
}
}
Implement your concrete types:
public class BUnit : UnitBase
{
public int SpecificBProperty { get; private set; }
public BUnit()
{
}
}
Shortcomings of this approach? Well, the container must be of type <UnitBase>, I've removed the generic type because it really wasn't doing much in this particular case as it would be invariant in the generic type.
Also, keep in mind that nothing in the type system avoids the following:
myContainer.Add<BUnit>();
myContainer.Add<AUnit>();
If having two different types in the same container is not an option then this whole set up kind of crumbles down. This issue was present in the previous solution too so its not something new, I simply forgot to point it out.
InBetween , I am very thankful to you for your advices. Actually I can't say that I understood your answer in full, but using your ideas I have done what I want.
Looks like my variant works well. However I would like to hear your (and everyone) opinions about code described below. The main goal of this structure is creating non-homogeneous trees. So could you estimate it from this side.
First of all. We need to create interfaces for both classes. We describe there all "cross-used" functions.
public interface IUnit<T>
{
string Name { get;}
void SetContainer(T t);
bool Rename(String newName);
}
public interface IContainer
{
bool IsNameBusy(String newName);
int Count { get; }
}
Next. Create Base for Unit Classes for future inheritance. We will use in this inheritors methods from Container Base so we need generic properties and IUnit interface.
class UnitBase<T> : IUnit<T> where T : IContainer
Unfortunately I don't know yet how to solve the problem with Constructor parameters. That is why I use method
SetContainer(T container).
Code:UnitBase
class UnitBase<T> : IUnit<T> where T : IContainer
{
protected T Container;
public string Name { get; private set; }
public UnitBase()
{
this.Name = "Default";
}
public void SetContainer(T container)
{
this.Container = container;
}
public bool Rename(String newName)
{
bool res = Container.IsNameBusy(newName);
if (!res) this.Name = String.Copy(newName);
return !res;
}
}
Next. Create ContainerBase
ContainerBase should:
1) has IContainer interface.
2)has information about what it will contain:
... where U : IUnit<C>, new()
3)and .... has information about what itself is. This information we need to pass as parameter to SetContainer() method.
Code ContainerBase:
class ContainerBase<U, C> : IContainer //U - Unit Class. C-Container Class
where U : IUnit<C>, new()
where C : ContainerBase<U, C>
{
protected List<U> Units;
public U this[int index] { get { return Units[index]; } }
public ContainerBase()//ctor
{
this.Units = new List<U>();
}
public void Add()
{
this.Units.Add(new U());
this.Units.Last().SetContainer(((C)this));//may be a bit strange but actualy this will have the same type as <C>
}
public bool IsNameBusy(String newName)
{
bool res = false;
foreach (var unt in this.Units)
{
if (unt.Name == newName)
{
res = true;
break;
}
}
return res;
}
public int Count { get { return this.Units.Count; } }
}
Cast ((TContainer)(this)) may be is a bit strange. But using ContainerBase we always should use NewInheritorContainer. So this cast is just do nothing…looks like...
Finally. This classes can be used like in this example.
class SheetContainer : ContainerBase<SheetUnit,SheetContainer> {public SheetContainer(){}}
class SheetUnit : UnitBase<SheetContainer>
{
public CellContainer Cells;
public PictureContainer Pictures;
public SheetUnit()
{
this.Cells = new CellContainer();
this.Pictures = new PictureContainer();
}
}
class CellContainer : ContainerBase<CellUnit, CellContainer> { public CellContainer() { } }
class CellUnit : UnitBase<CellContainer>
{
public string ValuePr;//Private Field
private const string ValuePrDefault = "Default";
public string Value//Property for Value
{
//All below are Just For Example.
get
{
return this.ValuePr;
}
set
{
if (String.IsNullOrEmpty(value))
{
this.ValuePr = ValuePrDefault;
}
else
{
this.ValuePr = String.Copy(value);
}
}
}
public CellUnit()
{
this.ValuePr = ValuePrDefault;
}
}
class PictureContainer : ContainerBase<PictureUnit, PictureContainer> { public PictureContainer() { } }
class PictureUnit : UnitBase<PictureContainer>
{
public int[,] Pixels{get;private set;}
public PictureUnit()
{
this.Pixels=new int[,]{{10,20,30},{11,12,13}};
}
public int GetSizeX()
{
return this.Pixels.GetLength(1);
}
public int GetSizeY()
{
return this.Pixels.GetLength(0);
}
public bool LoadFromFile(string path)
{
return false;
}
}
static void Main(string[] args)
{
SheetContainer Sheets = new SheetContainer();
Sheets.Add();
Sheets.Add();
Sheets.Add();
Sheets[0].Pictures.Add();
Sheets[1].Cells.Add();
Sheets[2].Pictures.Add();
Sheets[2].Cells.Add();
Sheets[2].Cells[0].Value = "FirstTest";
bool res= Sheets[0].Rename("First");//res=true
res=Sheets[2].Rename("First");//res =false
int res2 = Sheets.Count;
res2 = Sheets[2].Pictures[0].Pixels[1, 2];//13
res2 = Sheets[2].Pictures.Count;//1
res2 = Sheets[1].Pictures.Count;//0
res2 = Sheets[0].Pictures[0].GetSizeX();//3
Console.ReadKey();
}
Looks like it works like I want. But I didn’t test it full.
Let me say Thank you again, InBetween.
Is it somehow possible that base class can access fields in inherited class (has-a relationship)?
class BasicClass
{
public InheritedClass objTemp = new InheritedClass();
public BasicClass()
{
//Now i want to get some information from objTemp fields.
//But Name is protected, what should I do here?
objTemp.Name.Length();
}
}
class InheritedClass
{
protected string Name;
}
Maybe there are some tricky things that I don't know how to manage, or maybe it is better to create some more clever class hierarchy. Anyway thank you in advance.
Sorry for missunderstanding.In few words i have class Game which consist another class WordsContainer.
class Game
{
private Player objGamer;
private WordsContainer objWordsClass = new WordsContainer();
public Game()
{
Console.Title = "Hangman";
Console.Write("Player name information:");
string localName = Console.ReadLine();
objGamer = new Player(localName);
StringBuilder bumpWord = new StringBuilder(objWordsClass.getKeyWord().Length);
}
class WordsContainer
{
/// <summary>
/// WordsContainer class contains a list of strings from wich with getKeyWord method
/// we could get string type key.
/// </summary>
private List<string> wordBank = new List<string>() {"stack","queue","heap","git","array"};
public WordsContainer(){}
public string getKeyWord()
{
Random random = new Random((int)DateTime.Now.Ticks);
return wordBank[random.Next(0, wordBank.Count)];
}
So is it possible in this way somehow hide public string getKeyWord().
If you want to keep going on the code you have now, you could just define a public string GetName() function in InheritedClass and call it from the object that you create in the BasicClass
class BasicClass
{
public InheritedClass objTemp = new InheritedClass();
public BasicClass()
{
int nameLength = objTemp.GetName().Length();
}
}
class InheritedClass
{
protected string Name;
public string GetName()
{
return Name;
}
}