DJRavine's Codebook:

[ADVERTISEMENT]
Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating And Managing Classes And Objects, C# Tutorial
03-03-2007, 04:54 PM (This post was last modified: 10-11-2007 06:36 AM by Untold.)
Post: #1
Creating And Managing Classes And Objects, C# Tutorial
Creating and Managing Classes and Objects


The Purpose of Encapsulation

Encapsulation is an important principle when defining classes. The idea is that a program that uses a class should not have to worry how that class actually works internally; the program simply creates an instance of a class, and calls the methods of that class. As long as those methods do what they say they will do, the program does not care how they are implemented. For example, when you call the Console.WriteLine method, you don't want to be bothered with all the intricate details of how the Console class physically arranges for data to be output to the screen. A class might need to maintain all sorts of internal state information in order to perform its various methods. This additional state information and activity is hidden from the program that is using the class. Therefore, encapsulation is sometimes referred to as information-hiding. Encapsulation actually has two purposes:
  1. To combine methods and data inside a class; in other words, to support classification.
  2. To control the accessibility of the methods and data; in other words, to control the use of the class.
A method or field is said to be private if it is accessible only from the inside of the class. To declare that a method or field is private, you write the keyword private before its declaration. This is actually the default, but it is good practice to explicitly state that fields and methods are private to avoid any confusion. This is an example of encapsulation.

A method or field is said to be public if it is accessible from both the inside and the outside of the class. To declare that a method or field is public, you write the keyword public before its declaration.

Don't get confused between the terms class and object. A class is the definition of a type. An object is an instance of that type, created when the program runs. For example, it is possible to create many instances of the Circle class in a program by using the new keyword, just as you can create many int variables in a program. Each instance of the Circle class is an object that occupies its own space in memory, and runs independently of all the other instances.




Now, on to the coding part.

Open up your favorite editor and type the following code



Here is a class called Circle. The double Area is declared as a public method and radius is declared as a private field:
Quote:class Circle {


public Circle() // default constructor

{ radius = 0.0; }

public double Area()

{ return 3.141592 * radius * radius; }

private double radius; }








Note that radius is declared as a private field; it is not accessible from outside the class. However, radius is accessible from inside the Circle class. This is why the Area method can access the radius field; Area is inside the Circle class, so the body of Area has access to radius.

Note that the constructor is marked as public. If this keyword is omitted, the constructor will be private (just like any other methods and fields). If the constructor is private, it cannot be used outside of the class, which will prevent you from being able to create Circle objects from methods that are not part of the Circle class.

Thanks for reading. I hope you learned something! [Image: smile.gif]
Visit this user's website Find all posts by this user
Add Thank You Quote this message in a reply
03-04-2007, 09:03 PM
Post: #2
Creating And Managing Classes And Objects, C# Tutorial
Your becoming quite the guide makerWink

Death awaits you..
[Image: sigug3.jpg]
[Image: wowps_admin.gif]
Visit this user's website Find all posts by this user
Add Thank You Quote this message in a reply
03-05-2007, 09:36 AM
Post: #3
Creating And Managing Classes And Objects, C# Tutorial
Wink

I Was bored lol....... ill get somemore guides later Tongue
Visit this user's website Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Forum Jump:


Contact Us | WoWps.org | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication | CSS Validated!