看《Visual C#——从入门到精通》的问题
interface IToken{...}class DefaultTokenImp1{...}class IdentifierToken : DefaultTokenImp1 , IToken{...}
IdentifierToken it = new IdentifierToken();IToken iTok = it; //合法
void Process(IToken iTok){...}
using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{ interface ITok //接口 { int Get();//提供方法 } class DefaultTokenImp1 { public int c; public int d; } class IdentifierToken : DefaultTokenImp1, ITok { public int Get()//实现接口方法 { int j; j = 10; return j; } } class Program { static void Main(string[] args) { IdentifierToken it = new IdentifierToken(); // IdentifierToken its = new DefaultTokenImp1();出错,子类无法转换为基类型。 DefaultTokenImp1 itt = it;//基类引用子列,没出错,只当it是对象,且通过对子类实例的变量赋值而达到使itt基类实例中变量的值发生改变。 it.c = 10; it.d = 20; ITok itok = it;//合法,接口只提供方法,不提供实现,itok接口搜寻并调用it中的方法,来实现itok.GET()这个方法; Console.WriteLine(itok.Get());//输出 Console.WriteLine(itt.c);//父类变量 Console.WriteLine(itt.d); Console.ReadLine(); } }}