C# 接口(知识要点归纳总结)
1.为什么需要接口?
- 接口可以使一个类做多项工作而不依赖于继承,
因为继承会引入不需要的字段、属性和方法; - 通过接口能使用任何一个完成任务的类,即接口的引用可以指向任何一个实现了该接口的类的实例。比如,当接口作为函数的形参时,任何一个实现了该接口的类的实例都可以传递给该函数(这一点和子类实例传递给基类形参挺相似的)。
- 接口允许一个类用在多种不同的情况下。
备注:
1. 接口并不能用来避免重复的代码;
2. 用冒号(:)操作符来实现接口,冒号后面首先是继承的类(如果有的话),后面是一组接口。基类和接口、接口和接口之间用逗号隔开;
3. 接口定义了属性、方法和事件。接口值包含成员的声明,实现接口的类要实现接口中的每一个声明;
4. 接口不能实例化,但可以用接口引用指向实现了该接口的对象;
5. 用is查看一个类是否实现了某个接口
2. 声明接口
接口默认是public的
public interface ITransactions
{
// 接口成员
void showTransaction();
double getAmount();
}
3. 实现接口
示例1:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
namespace InterfaceApplication
{
public interface ITransactions
{
// interface members
void showTransaction();
double getAmount();
}
public class Transaction : ITransactions
{
private string tCode;
private string date;
private double amount;
public Transaction()
{
tCode = " ";
date = " ";
amount = 0.0;
}
public Transaction(string c, string d, double a)
{
tCode = c;
date = d;
amount = a;
}
public double getAmount()
{
return amount;
}
public void showTransaction()
{
Console.WriteLine("Transaction: {0}", tCode);
Console.WriteLine("Date: {0}", date);
Console.WriteLine("Amount: {0}", getAmount());
}
}
class Tester
{
static void Main(string[] args)
{
Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}
}
输出:
Transaction: 001
Date: 8/10/2012
Amount: 78900
Transaction: 002
Date: 9/10/2012
Amount: 451900
示例1:
using System;
using System;
namespace 接口
{
internal class Program
{
private static void Main()
{
var class1 = new Class1();
var class2 = new Class2();
var class3 = new Class3();
if (class1 is IShowLove)//用is查看class1是否实现了IShowLove接口
F1(class1);
F2(class2);
F1(class3);
F2(class3);
Console.ReadKey();
}
private static void F1(IShowLove showLove)
{
//只能访问接口有的成员,不能访问继承接口的类中有而接口中没有的成员
showLove.Words = "I love you !";
showLove.SpeakOut(showLove.Words);
}
private static void F2(IWrite write)
{
write.Content = "I feel good !";
write.WriteDown(write.Content);
}
}
#region 接口
/// <summary>
/// 示爱
/// </summary>
internal interface IShowLove
{
/// <summary>
/// 示爱的话语
/// </summary>
string Words { get; set; }
/// <summary>
/// 说出示爱的话语
/// </summary>
/// <param name="words"></param>
void SpeakOut(string words);
}
/// <summary>
/// 写字
/// </summary>
internal interface IWrite
{
/// <summary>
/// 写字的内容
/// </summary>
string Content { get; set; }
/// <summary>
/// 写下要写的内容
/// </summary>
/// <param name="content"></param>
void WriteDown(string content);
}
#endregion
internal class Class1 : IShowLove
{
public string Words { get; set; }
public void SpeakOut(string words)
{
Console.WriteLine("The Objec of Class1 speak :' " + words + " '.");
}
public void F11()
{
}
}
internal class Class2 : IWrite
{
public string Content { get; set; }
public void WriteDown(string content)
{
Console.WriteLine("The Objec of Class2 Write down :' " + content + " '.");
}
public void F22()
{
}
}
internal class Class3 : IShowLove, IWrite
{
public string Words { get; set; }
public void SpeakOut(string words)
{
Console.WriteLine("The Objec of Class3 speak :' " + words + " '.");
}
public string Content { get; set; }
public void WriteDown(string content)
{
Console.WriteLine("The Objec of Class3 Write down :' " + content + " '.");
}
}
}
备注:接口用来实现多种特定的功能,从而可以用在多种不同的情况下。