- .
(multiple inheritance). CLR . CLR , System.Object. (implementation inheritance), : , . . ( ) .
(interface inheritance) , . , . , , .
System.Object- . - , , , . , ().
, , (), , . CLR , , .
CLS- , . # . , CLR .
. , .. . , , , . , . . ( ) , . . , , . , , "" , , . , , # , " - ".
|
|
interface.
:
interface {
_ _1 (_);
_ _2 (_);
//...
_ _N (_);}
. . , , - .
(public-), .
, .
public interface ISeries {
int getNext(); // ,
void setStart(int ); // .
}
ISeries. ISeries , .
"I" , , .
, . . , . , .
, . , , . , , :
class _: _ { // }
_. , , .. .
. . . .
, , . , , . , , .
ISeries, . ByTwos, , .
using System;
public interface ISeries {
int getNext(); // ,
void setStart(int x); // .
}
// ISeries,
class ByTwos: ISeries {
int val;
public ByTwos() {val =0;}
public int getNext() {val += 2; return val;}
public void setStart(int x) {val = x;}
}
class Demo
{public static void Main()
{ ByTwos ob = new ByTwos();
ob.setStart(100);
for (int i = 0; i < 5; i++)
Console.WriteLine(ob.getNext());
|
|
}
}
:
ByTwos , ISeries.
, , .
.
, . .
{ get;
set;}
, , get- set-, .
. :
_ this[int ] { get;
set;
}
, , get- set-, .
.NET Framework Class Library (FCL):
public interface System.IComparable {Int32 CompareTo(Object obj);}
public interface System.Collections.IEnumerable {IEnumerator GetEnumerator();}
public interface System.Collections.IEnumerator {Boolean MoveNext();
void Reset();
Object Current {get;} //
}
public interface System.Collections.ICollection: IEnumerable {
void CopyTo(Array array, Int32 index);
Int32 Count {get;} //
Boolean IsSynchronized {get;} //
Object SyncRoot {get;} //
}
- I. - public, protected, internal prívate - . , 99% public. . . #, virtual, - .
, ( ICollection, IEnumerable). . , .
, , ICollection, , Count, IsSynchronized SyncRoot. , GetEnumerator IEnumerable.
System.ICloneable ( MSCorLib.dll) :
public interface ICloneable { Object Clone();}
, , :
. , . , . , . .
, FCL- System.String System.Object IComparable, ICloneable, IConvertible IEnumerable. , String , Object. String Object, , . String , , () . . , , .
|
|
: // String.
String s = "Jeffrey";
// s, ,
// String, Object, IComparable, ICloneable, IConvertible, IEnumerable.
// IComparable, s.
IComparable comparable = s;
// comparable, , IComparable,
// ICloneable, s.
ICloneable cloneable = s;
// cloneable, , ICloneable.
// IEnumerable, s.
IEnumerable enumerable = (IEnumerable) cloneable;
//
// , .
, : String, s. .