Introduction:
In our world we have classes and objects for those classes. Everything in our world is considered to be an object. For example, people are objects, animals are objects too, minerals are objects; everything in the world is an object. Easy, right? But what about classes?
In our world we have to differentiate between objects that we are living with. So we must understand that there are classifications (this is how they get the name and the concepts of the Class) for all of those objects. For example, I'm an object, David is object too, Maria is another object. So we are from a people class (or type). I have a dog called Ricky so it's an object. My friend's dog, Doby, is also an object so they are from a Dogs class (or type).
A C# Class is considered to be the primary building block of the language.
We use classes as a template to put the properties and functionalities or behaviors in one building block for a group of objects and after that we use the template to create the objects we need.
The class: A building block that contains the properties and functionalities that describe some group of objects. We can create a class Person that contains:
1. The properties of any normal person on the earth like: hair color, age, height, weight, eye color.
2. The functionalities or behaviors of any normal person on the earth like: drink water, eat, go to the work.
There are 2 kinds of classes: The built-it classes that come with the .NET Framework, called Framework Class Library, and the programmer defined-classes which we create ourselves.
The class contains data (in the form of variables and properties) and behaviors (in the form of methods to process these data).
The object: It's an object of some classification (or class, or type) and when you create the object you can specify the properties of that object. What I mean here is: I, as an object, can have different properties (hair color, age, height, weight) than you as another object. For example, I have brown eyes and you have green eyes. When I create 2 objects I will specify a brown color for my object's eye color property and I will specify a green color for your object's eye color property.
class Person
{
public int Age;
public string HairColor;
}
This is our simple class which contains 2 variables.
static void Main(string[] args)
{
Person Michael = new Person();
Person Mary = new Person();
// Specify some values for the instance variables
Michael.Age = 20;
Michael.HairColor = "Brown";
Mary.Age = 25;
Mary.HairColor = "Black";
// print the console's screen some of the variable's values
Console.WriteLine("Michael's age = {0}, and Mary's age = {1}",Michael.Age,
Mary.Age);
Console.ReadLine();
}
So we begin our Main method by creating 2 objects of type Person. After creating the 2 objects we initialize the instance variables for object Michael and then for object Mary. Finally we print some values to the console. Here, when you create the Michael object, the C# compiler allocates a memory location for the 2 instance variables to put the values there. Also, the same thing with the Mary object; the compiler will create 2 variables in memory for Mary object. So each object now contains different data.
Using Properties in Functions:
class Person
{
private int age;
private string hairColor;
public int Age
{
get
{
return age;
}
set
{
if(value <= 65 && value >= 18)
{
age = value;
}
else
age = 18;
}
}
public string HairColor
{
get
{
return hairColor;
}
set
{
hairColor = value;
}
}
}
I made some modifications, but focus on the new 2 properties that I created. So the property consists of 2 accessors. The get accessor, responsible of retrieving the variable value, and the set accessor, responsible of modifying the variable's value. So the get accessor code is very simple. We just use the keyword return with the variable name to return its value. So the following code:
get
{
return hairColor;
}
returns the value stored in hairColor.
static void Main(string[] args)
{
Person Michael = new Person();
Person Mary = new Person();
// Specify some values for the instance variables
Michael.Age = 20;
Michael.HairColor = "Brown";
Mary.Age = 25;
Mary.HairColor = "Black";
// print the console's screen some of the variable's values
Console.WriteLine("Michael's age = {0}, and Mary's age = {1}",Michael.Age,
Mary.Age);
Console.ReadLine();
}
Here I created the same objects from the last example, except that I used only properties to access the variable instead of accessing it directly. Look at the following line of code
Michael.Age = 20;
When you assign a value to the property like that C# will use the set accessor. The great thing with the set accessor is that we can control the assigned value and test it; and maybe change to in some cases. When you assign a value to a property C# changes the value in a variable and you can access the variable's value using the reserved keyword value exactly as I did in the example. Let's see it again here.
set
{
if(value <= 65 && value >= 18)
{
age = value;
}
else
age = 18;
}
Here in the code I used if statement to test the assigned value because for some reason I want any object of type Person to be aged between 18 and 65. Here I test the value and if it is in the range then I will simply store it in the variable age. If it's not in the range I will put 18 as a value to age.
We create a class by defining it using the keyword class followed by the class name:
class Person
Then we open a left brace "{" and write our methods and properties. We then close it with a right brace "}". That's how we create a class. Let's see how we create an instance of that class.
In the same way as we declare a variable of type int we create an object variable of Person type with some modifications:
int age;
Person Michael = new Person();
In the first line of code we specified integer variable called age. In the second line we first specified the type of Object we need to create, followed by the object's name, followed by a reserved operator called new. We end by typing the class name again followed by parentheses "()".
Let's understand it step-by-step. Specifying the class name at the beginning tells the C# Compiler to allocate a memory location for that type (the C# compiler knows all the variables and properties and methods of the class so it will allocate the right amount of memory). Then we followed the class name by our object variable name that we want it to go by. The rest of the code "= new Person();" calls the object's constructor. We will talk about constructors later but for now understand that the constructor is a way to initialize your object's variable while you are. For example, the Michael object we created in the last section can be written as following :
Person Michael = new Person(20, "Brown");
Here I specified the variable's values in the parameter list so I initialized the variables while creating the object.
Source: DevArticles
Tuesday, November 24, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment