, , . , , . new, .
. , . , IMylF
12.10
interface IMyIF
{
int MyMeth(int x);
}
:
12.11
class MyClass: IMyIF
{
int IMyIF.MyMeth(int x)
{
return x / 3;
}
}
, MyMeth() IMyIF , .
. -, , , , . , , , . -, , . . .
IEven, : IsEven() IsOdd(). , - . IEven MyClass. IsOdd() .
12.12
// .
using System;
interface IEven
{
bool IsOdd(int x);
bool IsEven(int x);
}
class MyClass: IEven
{
// . ,
// .
bool IEven.IsOdd(int x)
{
if((x%2)!= 0) return true;
else return false;
}
// .
public bool IsEven(int x)
{
IEven o = this; // .
return!o.IsOdd(x);
}
}
class Demo {
static void Main() {
MyClass ob = new MyClass();
bool result;
result = ob.IsEven(4);
if(result) Console.WriteLine("4 .");
// result = ob.IsOdd(4); // , IsOdd
// IEven .
// ,
|
|
// IEven MyClass,
// IsOdd().
IEven iRef = (IEven) ob;
result = iRef.IsOdd(3);
if(result) Console.WriteLine("3 .");
}
}
IsOdd() , , MyClass. , . IEven IsEven().
, , Meth(). , .
12.13
// .
using System;
interface IMyIF_A
{
int Meth(int x);
}
interface IMyIF_B
{
int Meth(int x);
}
// MyClass.
class MyClass: IMyIF_A, IMyIF_B
{
// Meth().
int IMyIF_A.Meth(int x)
{
return x + x;
}
int IMyIF_B.Meth(int x)
{
return x * x;
}
// Meth() .
public int MethA(int x)
{
IMyIF_A a_ob;
a_ob = this;
return a_ob.Meth(x); // IMyIF_A
}
public int MethB(int x)
{
IMyIF_B b_ob;
b_ob = this;
return b_ob.Meth(x); // IMyIF_B
}
}
class FQIFNames
{
static void Main()
{
MyClass ob = new MyClass();
Console.Write(" IMyIF_A.Meth(): ");
Console.WriteLine(ob.MethA(3));
Console.Write(" IMyIF_B.Meth(): ");
Console.WriteLine(ob.MethB(3));
}
}
.
IMylF A.Meth(): 6
IMyIF_B.Meth(): 9
, Meth () , IMyIF_A IMyIF_B. MyClass, , .. Meth(). , MyClass : - IMyIF_, - IMyIF_. , .