An interface is defined as a reference type that specifies a set of function members without implementing a member, and other types and interfaces can inherit an interface. Definition is still well understood, but does not reflect the characteristics of the main interface has the following characteristics:
(1) Through the interface can achieve multiple inheritance, C # Interface members can not have public, protected, internal, private and other modifiers. The reason is simple, the interface inside the method needs to be implemented by the outside interface to implement the method body, then its modifier must be public. Members in the C # interface are public by default and can be public in Java.
(2) Interface members cannot have new, static, abstract, override, virtual modifiers. One thing to note is that when an interface implements an interface that has the same method in the 2 interfaces, the New keyword can be used to hide the method in the parent interface.
(3) The interface contains only the members ' signatures, the interfaces do not have constructors, and all interfaces cannot be instantiated directly using new. The interface can contain only a combination of methods, properties, events, and indexes. Once an interface is implemented, the implementing class must implement all the members of the interface, unless the implementation class itself is an abstract class.
(4) C # is a single inheritance, an interface that solves the problem that a class within C # can inherit multiple base classes at the same time.
Today, let's say how to use interfaces for sorting:
There are 3 ways of doing this:
1. Using icomparer<t>
2. Using icomparable<t>
3. Custom interface Implementations
This is the student class:
public class Student:person,icomparable<student>,ihomeworkcollector
{
private string Hoddy;
public string Hoddy { get { return hoddy; } set { hoddy = value; } } public Student() { } public Student(string name,int age,string hoddy):base(name,age) { this.Hoddy = hoddy; }
}
Now say how to use icomparer<t>:
Define 3 methods: Sort by name and age
public class Namecomparer:icomparer<student>
{
public int Compare (Student X, Student Y)
{
Return (X.name.compareto (y.name));
}
}public class NameComparerDesc : IComparer<Student>{ public int Compare(Student X, Student Y) { return (Y.Name.CompareTo(X.Name)); }}public class AgeComParer : IComparer<Student>{ public int Compare(Student X, Student Y) { return (X.Age.CompareTo(Y.Age)); }}
Call:
Sort using names:
Initstudents ();
Students. Sort (New Namecomparer ());
Printstudents (students);
Use Age to sort:
Initstudents ();
Students. Sort (New Agecomparer ());
Printstudents (students);
2. Using Icomparable<t>:
Define a non-implemented interface yourself IComparable
public interface Icomparable<in t>
{
int CompareTo(T other);}
In students define his method CompareTo:
public int CompareTo (Student other)
{
Return This.Name.CompareTo (other. Name);
}
To sort by calling the Sort method
3. Use a custom interface:
Define an interface yourself
public interface Ihomeworkcollector
{
void Collecthomework ();
}
Implemented in the Student class:
public void Collecthomework ()
{
MessageBox.Show ("Report Teacher! Homework collected!");
}
Implemented in the teacher class:
public void Collecthomework ()
{
MessageBox.Show ("The students, should hand in the homework");
}
To make a call:
Ihomeworkcollector coll1 = createhomeworkcollector ("student");
Ihomeworkcollector coll2 = createhomeworkcollector ("Teacher");
Coll1. Collecthomework ();
Coll2. Collecthomework ();
C # uses interfaces for sorting