// , // .
using System;
class {
public void Hello () {
Console.WriteLine("Hello");
// . class : { }
// . class '{ }
// ,
// Test, . class Test<T> where : {
obj;
public Test( ) { obj = ;
}
public void SayHelloO {
// Hello() , . obj.Hello();
}
}
class BaseClassConstraintDemo { static void Main() {
A a = new A();
b = new ()
= new ()j
// , .
Test<A> tl = new Test<A>(a);
Tl.SayHello();
// , .
Test<B> t2 = new Test<B> (b);
T2.SayHello();
// , .
// Test<C> t3 = new Test<C>(c); // !
// t3.SayHello(); // !
}
}
, . , Hello (), Test .
class Test<T> where : {
where : , , .
, Test SayHello (), .
public void SayHelloO {
// Hello () , . obj.Hello();
}
Hello () obj . , Hello () : , , , Hello (). , Hello (). , , Hello () . , where Test. , Hello () .
, , , . .
// Test<C> t3 = new Test<C>(c); // !
// t3.SayHello(); // !
, Test. , .
|
|
, . -, . -, , , .
, , , . , , . , , , , , .. PhoneNumber, . .
// , , class PhoneNumber {
public PhoneNumber(string n, string num) {
Name = n;
Number = num;
}
// , // , public string Number { get; set; } public string Name { get; set; }
, PhoneNumber: Friend Supplier. .
// , class Friend: -PhoneNumber {
public Friend(string n, string num, bool wk): base(n, num)
{
IsWorkNumber = wk;
}
public bool IsWorkNumber { get; private set; }
//...
}
// , class Supplier: PhoneNumber {
public Supplier(string n, string num): base(n, num) { }
//...
}