In this example, the interface IPoint haves the property declaration, which is responsible for setting and getting the values. The class MyPoint haves the property execution.
Code:
// keyword_interface.cs
// Interface implementation
using System;
interface IPoint
{
// Property signatures:
int a
{
get;
set;
}
int b
{
get;
set;
}
}
class MyPoint : IPoint
{
// Fields:
private int myA;
private int myB;
// Constructor:
public MyPoint(int a, int b)
{
myA = a;
myB = b;
}
// Property implementation:
public int a
{
get
{
return myA;
}
set
{
myA = value;
}
}
public int yb
{
get
{
return myB;
}
set
{
myB = value;
}
}
}
class MainClass
{
private static void PrintPoint(IPoint p)
{
Console.WriteLine("a={0}, b={1}", p.a, p.b);
}
public static void Main()
{
MyPoint p = new MyPoint(2,3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
Bookmarks