Usually within a class, you declare a private field data and provide a set of public SET and GET methods to access data fields. This is a Nice programming practice, since the data fields can not be obtained directly from the class side. You must use the set / get methods to access data fields.
In C #, properties are nothing but a natural extension of data fields. Usually known as "smart fields" in the community C #. They know that data encapsulation and hiding is that the fundamental characteristics of any language object-oriented programming. In C #, data encapsulation is possible through classes or structures. By using various access modifiers etc as private, public, protected, internal, you can control the accessibility of class members.
//SET/GET methods
//Author: rajeshvs@msn.com
using System;
class MyClass
{
private int x;
public void SetX(int i)
{
x = i;
}
public int GetX()
{
return x;
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.SetX(10);
int xVal = mc.GetX();
Console.WriteLine(xVal);//Displays 10
}
}
Static Properties
C # also keeps static attributes, which belongs to the class instead of the objects of the class. All principles applicable to a static member is applicable to static properties also.
The following program shows a class with a static property.
/C# : static Property
//Author: rajeshvs@msn.com
using System;
class MyClass
{
private static int x;
public static int X
{
get
{
return x;
}
set
{
x = value;
}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 10;
int xVal = MyClass.X;
Console.WriteLine(xVal);//Displays 10
}
}
Properties & Inheritance
The properties of a base class may be inherited by a derived class.
//C# : Property : Inheritance
//Author: rajeshvs@msn.com
using System;
class Base
{
public int X
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}
class Derived : Base
{
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.X = 10;
Console.WriteLine(d1.X);//Displays 'Base SET Base GET 10'
}
}
Properties & Polymorphism
A base class property polymorphicaly can be overridden in a derived class. But remember that as virtual switches, etc. being used to override property level, not at the level of access.
//C# : Property : Polymorphism
//Author: rajeshvs@msn.com
using System;
class Base
{
public virtual int X
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}
class Derived : Base
{
public override int X
{
get
{
Console.Write("Derived GET");
return 10;
}
set
{
Console.Write("Derived SET");
}
}
}
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.X = 10;
Console.WriteLine(b1.X);//Displays 'Derived SET Derived GET 10'
}
}
Property within a class can be declared as abstract using the abstract keyword. Remember that an abstract property in a class carries no code at all. The get / set access basically represented by a semicolon. In the derived class must apply to both set and get assessors.
If the abstract class contains only set access, they can implement two set of the derived class.
//C# : Property : Abstract
//Author: rajeshvs@msn.com
using System;
abstract class Abstract
{
public abstract int X
{
get;
set;
}
}
class Concrete : Abstract
{
public override int X
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
}
class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1.X = 10;
Console.WriteLine(c1.X);//Displays 'SET GET 10'
}
}
0 comments:
Post a Comment