|
||
|
|||||||
| Script Support Get support for modifying RunUO Scripts, or writing your own! |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Forum Expert
|
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"; 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;
}
Code:
public class TestClass
{
private string Name = "TestClass";
public string GetName()
{
return Name;
}
}
Code:
string MyString; MyString = TestClass.GetName(); Console.WriteLine(MyString); 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.");
}
}
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. |
|
|
|
|
|
#2 (permalink) |
|
RunUO Forum Moderator
|
For a very good post you get +Sticky
![]() **thanks to aventae for bringing this thread to my attention**
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
|
#3 (permalink) | |
|
Newbie
Join Date: Mar 2005
Age: 22
Posts: 54
|
Quote:
Properties are actually big different from the fields 8) |
|
|
|
|
|
|
#4 (permalink) | |
|
Forum Expert
|
Quote:
Properties: Properties (C# Programming Guide) ![]() |
|
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
|
C# Scripting: breaking down some of the basics using a simple script as an example
You may have trouble reading the script here, so it is also attached below. PHP Code:
|
|
|
|
|
|
#6 (permalink) |
|
Newbie
|
You have no idea how much this post is helping me at the moment. I am keeping that chainlegs script open while looking at other scripts to actually start to understand them better.
I do have a question regarding attributes i.e. [FlipableAttribute(0x13be, 0x13c3)] [Constructable] Where exactly are those contained, and how do you know when you can use what attributes? Sort of like, how the overrides in that script, are derived from BaseArmor. Not sure if im making alot of sense. Anyway, great post, I hope you will add some more, I for one am learning quite a bit from it. +rep
__________________
- Deegs aka Citriz - Project Citriz |
|
|
|
|
|
#7 (permalink) | |
|
Forum Expert
|
Quote:
I STRONGLY recommend downloading Microsoft Visual C# Express Edition 2008. It's free, and can be found here: Downloads With it, you can right-click on a class name, its base class or any object, and navigate to the constructor or definition of that object. This will help you greatly with trying to figure out where a particular class is derived, what it's methods are, etc. |
|
|
|
|
|
|
#8 (permalink) |
|
Forum Expert
|
A class is always an "object" but never "Always" derived from another class.
You can create your own "SuperClasses" that have no specific parents. Examples: Code:
//A Derived "SubClass" using inheritance
public class SubClass : SuperClass
{
//This is known as a "Ctor" or "Constructor", since we inherit "SuperClass", we must call it's "Ctor" using "base()"
public SuperClass() : base(args) //"args" would be variables you pass to or set for, the base "Ctor", if any are required.
{
//Set the "SuperClass" variables here
}
}
Code:
//A custom "SuperClass", drop the inheritance
public class SuperClass
{
//"Constructor", since we do not inherit anything, we do not need to define "base()"
public SuperClass()
{
//Set Class Variables, which you need to define
}
}
Code:
//A custom "SuperClass", drop the inheritance, use custom variables
public class SuperClass
{
//Define your new variables
private int m_Int;
private double m_Double;
private bool m_Bool;
private string m_String;
//"Constructor", since we do not inherit anything, we do not need to define "base()"
public SuperClass()
{
//Set Class Variables, which you need to define as above
m_Int = 1234;
m_Double = 123.4;
m_Bool = true; //Or false
m_String = "Hello World";
}
}
__________________
![]() RPK.VORSPIRE.COM - The WoW-UO Cross-Over Shard Last edited by Vorspire; 05-18-2008 at 10:32 AM. |
|
|
|
|
|
#9 (permalink) | ||
|
RunUO Forum Moderator
|
I know that this thread isn't supposed to be in this scope, but I also know that Vorspire want to learn so I decided to correct minor errors:
Quote:
Take for example the Utility.cs class from the core. The class itself isn't static but all the methods inside it are static so making an object from that class won't serve any purpose. Quote:
That why before .NET 2.0 (which introduced generics) we could have a list of "objects" and insert any kind of object from any class we want into them. The list simply expected an object from type "object" (the class) and because every single class is derived from the "object" class then they all match the criteria (even int, double, bool...).
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|