Lokai
Knight
C# Scripting: Objects, Classes, Constructors, and Inheritance
Objects, Classes, Constructors, and Inheritance
Objects:
An object is the base class of all classes.
Think of it this way: every Item, Mobile, Integer, String, Packet, Socket, etc. is an object. As such, each of these 'Inherits' certain things which define how they can be dealt with in code, how they interact with eachother, and how we get information into or out of them, from their ultimate Parent type -- object.
What does an object look like?
An object is defined by its characteristics, just like food, or government, or a person is defined by their characteristics. What defines every object are the properties it keeps, the methods we use to access those properties, and the methods that define what the object can do, think, say, write, or read. For example: PlayerMobile is a class derived from the Mobile class, which is derived from the object class. So, PlayerMobile is an object, which inherits some things from object, more things from Mobile, and finally has even more characteristics not found in either object or Mobile, which define who or what it is.
Class:
The class is the definition of a new type of object.
A class is always derived from a Parent class of some type, and can sometimes have Child classes which are derived from it. We will call a Parent class a "Superclass", and a Child class a "subclass". For example, as stated earlier, every class derives from the ultimate superclass - object, and Item, Mobile, etc. are considered subclasses. PlayerMobile is a subclass of Mobile, BaseContainer is a subclass of Item, and Backpack is a subclass of BaseContainer. Every subclass "inherits" all of the properties, attributes, methods, or characteristics, of its Parent or Parents (its superclasses.) These characteristics can be overriden in the subclass, but if not otherwise specified, they will behave like their superclass counterparts.
Property:
Properties are the bits and pieces that describe the basic parts of a class. Properties have a datatype, a variable name, and a value. They can also have an accessibility defined. If not specified, the accessibility is Private. It can also be Public, or Protected. Properties are defined, and usually initialized in the beginning of the definition of a class, and serve to create a "description" of the class. For example:
Method:
A method is a means of interacting with the class.
Methods also have accessibility, datatype (called "return type") and name. In addition, they can have zero or more parameters passed to them which help control what happens when the method is "called" from the outside. "Calling" a method simply means using the name of the method, and giving it any parameters it is expecting to receive. For example, this method is called "GetName" and it accepts no parameters, and it returns a datatype 'String' back to the program that called it:
Here is what this method might look like inside a class:
And here is what the code of a program might look like that calls this method:
That code should output the word "TestClass" to the Console.
If no return type is specified, the word 'void' must be used in its place. This indicates that the method will not return any values to the program calling it, but will instead simply perform whatever operations it needs to perform on its own.
Constructors:
A constructor is the method of initializing an instance of a class.
A constructor is called when the 'new' keyword is used or when a System Reflection is used to instantiate an object.
The constructor resembles a method, but has no return type, and the name of the method is always the same as the class itself. Every constructor of every class other than the 'object' class either explicitly or implicitly calls a base class, or another constructor from the same class.
There are two forms of constructor initializer - one which calls a base class constructor and one which calls another constructor from this class, using the this (...) syntax. There must always be a "chain" of constructors which runs all the way up the class hierarchy. Every class in the hierarchy will have a constructor invoked, although some of those constructors may not explicitly appear in the code. The parameters (if any) within the brackets of base(...) or this(...) are passed as the parameters to the invoked constructors. They can be the parameters given in the constructor declaration, but don't have to be. Here's an example:
With the above code, a bit of code saying new MyDerivedTestClass(); would invoke the MyDerivedTestClass parameterless constructor, which would in turn invoke the MyDerivedTestClass constructor which takes an int parameter (with 10 as that parameter value), which would in turn invoke the MyTestClass constructor which takes an int parameter (with 10 * 5 as that parameter value).
Not all constructors in the hierarchy need to be invoked, as demonstrated above - the constructor taking a string parameter is not invoked at all when you do new MyDerivedTestClass() - but as stated earlier, there must be at least one constructor invoked in each class in the hierarchy.
Objects, Classes, Constructors, and Inheritance
Objects:
An object is the base class of all classes.
Think of it this way: every Item, Mobile, Integer, String, Packet, Socket, etc. is an object. As such, each of these 'Inherits' certain things which define how they can be dealt with in code, how they interact with eachother, and how we get information into or out of them, from their ultimate Parent type -- object.
What does an object look like?
An object is defined by its characteristics, just like food, or government, or a person is defined by their characteristics. What defines every object are the properties it keeps, the methods we use to access those properties, and the methods that define what the object can do, think, say, write, or read. For example: PlayerMobile is a class derived from the Mobile class, which is derived from the object class. So, PlayerMobile is an object, which inherits some things from object, more things from Mobile, and finally has even more characteristics not found in either object or Mobile, which define who or what it is.
Class:
The class is the definition of a new type of object.
A class is always derived from a Parent class of some type, and can sometimes have Child classes which are derived from it. We will call a Parent class a "Superclass", and a Child class a "subclass". For example, as stated earlier, every class derives from the ultimate superclass - object, and Item, Mobile, etc. are considered subclasses. PlayerMobile is a subclass of Mobile, BaseContainer is a subclass of Item, and Backpack is a subclass of BaseContainer. Every subclass "inherits" all of the properties, attributes, methods, or characteristics, of its Parent or Parents (its superclasses.) These characteristics can be overriden in the subclass, but if not otherwise specified, they will behave like their superclass counterparts.
Property:
Properties are the bits and pieces that describe the basic parts of a class. Properties have a datatype, a variable name, and a value. They can also have an accessibility defined. If not specified, the accessibility is Private. It can also be Public, or Protected. Properties are defined, and usually initialized in the beginning of the definition of a class, and serve to create a "description" of the class. For example:
Code:
private int Size = 4;
int Color = 302;
string Name = "TestClass";
Method:
A method is a means of interacting with the class.
Methods also have accessibility, datatype (called "return type") and name. In addition, they can have zero or more parameters passed to them which help control what happens when the method is "called" from the outside. "Calling" a method simply means using the name of the method, and giving it any parameters it is expecting to receive. For example, this method is called "GetName" and it accepts no parameters, and it returns a datatype 'String' back to the program that called it:
Code:
public string GetName()
{
return Name;
}
Here is what this method might look like inside a class:
Code:
public class TestClass
{
private string Name = "TestClass";
public string GetName()
{
return Name;
}
}
And here is what the code of a program might look like that calls this method:
Code:
string MyString;
MyString = TestClass.GetName();
Console.WriteLine(MyString);
That code should output the word "TestClass" to the Console.
If no return type is specified, the word 'void' must be used in its place. This indicates that the method will not return any values to the program calling it, but will instead simply perform whatever operations it needs to perform on its own.
Constructors:
A constructor is the method of initializing an instance of a class.
A constructor is called when the 'new' keyword is used or when a System Reflection is used to instantiate an object.
The constructor resembles a method, but has no return type, and the name of the method is always the same as the class itself. Every constructor of every class other than the 'object' class either explicitly or implicitly calls a base class, or another constructor from the same class.
There are two forms of constructor initializer - one which calls a base class constructor and one which calls another constructor from this class, using the this (...) syntax. There must always be a "chain" of constructors which runs all the way up the class hierarchy. Every class in the hierarchy will have a constructor invoked, although some of those constructors may not explicitly appear in the code. The parameters (if any) within the brackets of base(...) or this(...) are passed as the parameters to the invoked constructors. They can be the parameters given in the constructor declaration, but don't have to be. Here's an example:
Code:
public class MyTestClass
{
public MyTestClass (int x) : base() // Invokes the parameterless constructor in object ( the " : base()" can be omitted.)
{
Console.WriteLine ("In the base class constructor taking an int, which is " + x);
}
}
public class MyDerivedTestClass : MyTestClass
{
public MyDerivedTestClass () : this (10) // Invokes the MyDerivedTestClass constructor taking an int
{
Console.WriteLine ("Received no parameters, and passed 10 to this class.");
}
public MyDerivedTestClass (int y) : base (y * 5) // Invokes the MyTestClass constructor, gets an int, and passes an int
{
Console.WriteLine ("Received {0}, and passed {1} to the base class.", y, y * 5);
}
public MyDerivedTestClass (string x) : base (50) // Invokes the MyTestClass constructor, gets a string, and passes an int
{
Console.WriteLine ("Received a string, and passed 50 to the base class.");
}
}
With the above code, a bit of code saying new MyDerivedTestClass(); would invoke the MyDerivedTestClass parameterless constructor, which would in turn invoke the MyDerivedTestClass constructor which takes an int parameter (with 10 as that parameter value), which would in turn invoke the MyTestClass constructor which takes an int parameter (with 10 * 5 as that parameter value).
Not all constructors in the hierarchy need to be invoked, as demonstrated above - the constructor taking a string parameter is not invoked at all when you do new MyDerivedTestClass() - but as stated earlier, there must be at least one constructor invoked in each class in the hierarchy.