标签:
1、代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { //为了便于观看,我就把接口和类都写在一个.cs文件中了 public interface ISay { void Say(); } class Student : ISay { public void Say() { Console.WriteLine("我是一个学生,我的任务是学习"); } } class Teacher : ISay { public void Say() { Console.WriteLine("我是一个老师,我的任务是教书育人"); } } class Program { static void Main(string[] args) { Introduce(new Student()); Introduce(new Teacher()); Console.ReadKey(); } public static void Introduce(ISay h) { h.Say(); } } }
2、效果
C#控制台基础 函数的参数是接口 实现接口的类都可以作为参数,很好用
标签:
原文地址:http://www.cnblogs.com/jinlingzi/p/5933641.html