Hello everyone!
I have been playing around with C# for the past few weeks and have been having fun making some pretty simple programs that I made to help with some OOP techniques and just generally learning new things about how C# works. The end goal is, by the end of the summer holidays, to create a simple text based RPG to help me learn more about user input and how objects work/interact with each other. My knowledge of OOP isn't all there but I understand the core basics of how it works... I was wondering if anyone could help me find some good resources on OOP (particularly interfaces) that are noob friendly and give great examples on how to use different techniques and when to use them.
Thanks!
It all depends on how you designed your application.
Interfaces are useful when you just need access to the methods, but their implementation varies. For example
[code]
public interface Item
{
public void Use();
public Item Combine(Item other);
}
public class HealthPotion : Item
{
public void Use()
{
player.HP += 100;
}
public Item Combine(Item other)
{
return this + other;
}
}
public class Key : Item
{
public void Use()
{
// key using code here
}
public Item Combine(Item other)
{
// item does not allow combination
}
}
[/code]
The implementation of HealthPotion and Key are completely different, but if you make a List<Item>, they can both fit in it. It also makes sure that every item has the Use and Combine methods with the signature defined in the interface.
You can also use a abstract class for this. This has pretty much the same use, except you can define a standard implementation and let the child class implement and/or override those methods when needed.
[code]
public abstract class Item
{
public virtual void Use()
{
Destroy();
}
public virtual void Combine(Item other)
{
return this + other;
}
public virtual void Destroy()
{
// remove the item from the player inventory
}
}
public class HealthPotion : Item
{
public override void Use()
{
player.HP += 100;
base.Use();
}
}
public class Key : Item
{
public override void Use()
{
// unlock door
}
public override Combine(Item other)
{
return this; // do not allow combining
}
}
[/code]
In this example, the HealthPotion.Use() method will call the base method. The base method will remove the item from the players inventory trough the Destroy method. It overrides the Use method, but still executes the base method with base.Use(). The Item.Use method already implements removing the item from the players inventory, so there is no need for the HealthPotion class to do so.
The key on the other hand, does not uses base.Use, because a key can be used multiple times. The healthpotion allows combining in the standard way, so it does not override the Combine method and lets the Item class handle it. The key on the other hand, returns itself, so it can't be combined.
Using the abstract class allows for mostly the same benefits as the Interface, with the exception of a base implementation. So every HealthPotion and Key instances, are also of the type Item. Knowing this, you can do something like this:
[code]
public abstract class Item
{
private static List<Item> _allItems;
public static List<Item> AllItems
{
get
{
if(_allItems == null)
{
_allItems = new List<Item>();
}
return _allItems;
}
}
}
public class HealthPotion : Item
{
public HealthPotion()
{
AllItems.Add(this);
}
}[/code]
Although in this case, it would be better the have the Item class not abstract, and put that in the constructor. Then call the base constructor in the child class. Don't know how popular of a thing this is, so people might dislike it.
This is not a complete realistic example of course. The key not allowing combining would be implemented in a completely different way.
I also rambled a bit on something for an explanation, so if something is not clear, lemme know.
Sorry, you need to Log In to post a reply to this thread.