接口的实现方式和抽象类一样,都是用“:”表示即可,光实现的话":"后其他的接口以逗号隔开即可,如果此类不光实现接口还要有继承,那么继承必须跟在最前面。
测试类
using System; namespace Demo3 { class Program { static void Main(string[] args) { Apple apple = new Apple(); Grape grape = new Grape(); apple.describe().colourDescribe().tasteDescribe(); grape.describe().colourDescribe().tasteDescribe(); } } }
测试结果:
实现类:苹果
using System; using System.Collections.Generic; using System.Text; namespace Demo3 { /// <summary> /// 苹果类 /// </summary> public class Apple : Fruits<Apple>, Colour<Apple>, Taste<Apple> { public Apple colourDescribe() { Console.Write("颜色红红的"); return this; } public override Apple describe() { Console.Write("我是一个大苹果"); return this; } public Apple tasteDescribe() { Console.WriteLine("味道甜甜糯糯的"); return this; } } }
实现类:葡萄
using System; using System.Collections.Generic; using System.Text; namespace Demo3 { /// <summary> /// 葡萄类 /// </summary> public class Grape : Fruits<Grape>, Colour<Grape>, Taste<Grape> { public Grape colourDescribe() { Console.Write("紫色的颜色"); return this; } public override Grape describe() { Console.Write("我是一串大葡萄"); return this; } public Grape tasteDescribe() { Console.WriteLine("酸酸甜甜的味道"); return this; } } }
抽象类:抽象基类水果类
using System; using System.Collections.Generic; using System.Text; namespace Demo3 { public abstract class Fruits<T> { /// <summary> ///描述 /// </summary> public abstract T describe(); } }
接口:味道接口
using System; using System.Collections.Generic; using System.Text; namespace Demo3 { /// <summary> /// 味道接口 /// </summary> public interface Taste<T> { /// <summary> /// 味道描述 /// </summary> T tasteDescribe(); } }
接口:颜色接口
using System; using System.Collections.Generic; using System.Text; namespace Demo3 { /// <summary> /// 颜色接口 /// </summary> interface Colour<T> { /// <summary> /// 颜色描述 /// </summary> T colourDescribe(); } }