Saturday, 26 April 2014

Beginner's C# Course, Part 4

The basics of C#: Partial classes and Inherited classes

What is a partial class?
Partial classes are classes that span more than one file. Usually, you would define a class in one .cs file, and it would be contained entirely within that file. However with Partial classes, you can define the class in two or more separate .cs files and have separate methods in each.

I won't be touching on partial classes much in this post, and will mostly be focusing on extended classes. Simply because I have yet to find a valid use other than windows forms for partial classes.

What is an inherited class?
An inherited class is a class that is considered an extension or derivative of another class, some people refer to these as "sub-classes". A common example of inheritance use in game development is entities.
For example

##File Start#
using [Insert usual using statements here]

namespace game
{
    public class Entity
    {
        public Entity(int hp, string name)
        {
            //Do some stuff here
        }
    }
}
##File End##

##File Start##
using [Insert usual using statements here]

namespace game
{
    public class Player : Entity
    {
        public Player(int level, int exp, int hp, string name)
        :base (hp, name) //This runs the constructor for the Entity class, and gives whatever was given to player as hp and name
        {
            //Do some more stuff here
        }
    }
}
##File End##

And the same could also be done for enemies and NPC's.
This lesson was a very short one, and mostly taught by example. Remember if there's anything you don't understand in any of these lessons feel free to comment on them.

Also apologies this one was so late, almost a day late in fact.
On Sunday It'll be another source post, the InitLoad class this time.

No comments:

Post a Comment