資源簡介
定義一個接口,用于描述動物的生活習性,包含食性、捕食、分布區域、睡覺方式、群居方式等;接口里面包括屬性和方法。
例如:
interface IHabit
{
void CatchFood();
string food{get;set;}
……
}
然后定義貓、老虎、獅子、大象等幾個動物類來實現該接口;
最后定義一個調用類,來運行程序,查看接口的使用情況。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?S4_1
{
????interface?IHabit
????{
????????///?
????????///?撲食
????????///?
????????void?CatchFood();
????????///?
????????///?食性
????????///?
????????string?Food?{?get;?set;?}
????????///?
????????///?分布區域
????????///?
????????void?AreaDistribution();
????????///?
????????///?睡覺方式
????????///?
????????void?SleepPattern();
????????///?
????????///?群居方式
????????///?
????????void?SocialPattern();
????}
????///?
????///?貓
????///?
????class?Cat?:?IHabit
????{
????????private?string?food?=?“Cat?Food“;
????????public?void?CatchFood()
????????{
????????????Console.WriteLine(“From?Cat.CatchFood“);
????????}
????????public?string?Food
????????{
????????????get
????????????{
????????????????return?food;
????????????}
????????????set
????????????{
????????????????food?=?value;
????????????}
????????}
????????public?void?AreaDistribution()
????????{
????????????Console.WriteLine(“From?Cat?AreaDistribution“);
????????}
????????public?void?SleepPattern()
????????{
????????????Console.WriteLine(“From?Cat?SleepPattern“);
????????}
????????public?void?SocialPattern()
????????{
????????????Console.WriteLine(“From?Cat?SocialPattern“);
????????}
????}
????///?
????///?老虎
????///?
????class?Tiger?:?IHabit
????{
????????private?string?food?=?“Tiger?Food“;
????????public?void?CatchFood()
????????{
????????????Console.WriteLine(“From?Tiger.CatchFood“);
????????}
????????public?string?Food
????????{
????????????get
????????????{
????????????????return?food;
????????????}
????????????set
????????????{
????????????????food?=?value;
????????????}
????????}
????????public?void?AreaDistribution()
????????{
????????????Console.WriteLine(“From?Tiger?AreaDistribution“);
????????}
????????public?void?SleepPattern()
????????{
????????????Console.WriteLine(“From?Tiger?SleepPattern“);
????????}
????????public?void?SocialPattern()
????????{
????????????Console.WriteLine(“From?Tiger?SocialPattern“);
????????}
????}
????///?
????///?獅子
????///?
????class?Lion?:?IHabit
????{
????????private?string?food?=?“Lion?Food“;
????????public?void?CatchFood()
????????{
????????????Console.WriteLine(“From?Lion.CatchFood“);
????????}
????????public?string?Food
????????{
????????????get
????????????{
????????????????return?food;
????????????}
????????????set
????????????{
????????????????food?=?value;
????????????}
????????}
????????public?void?AreaDistribution()
????????{
????????????Console.WriteLine(“From?Lion?AreaDistribution“);
????????}
????????public?void?SleepPattern()
????????{
????????????Console.WriteLine(“From?Lion?SleepPattern“);
????????}
????????public?void?SocialPattern()
????????{
????????????Console.WriteLine(“F
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????7168??2013-12-29?19:31??S4-1\S4-1\bin\Debug\S4-1.exe
?????文件??????22016??2013-12-29?19:31??S4-1\S4-1\bin\Debug\S4-1.pdb
?????文件??????11592??2013-12-29?19:33??S4-1\S4-1\bin\Debug\S4-1.vshost.exe
?????文件????????490??2009-08-31?00:40??S4-1\S4-1\bin\Debug\S4-1.vshost.exe.manifest
?????文件???????5159??2013-12-29?19:31??S4-1\S4-1\Class1.cs
?????文件???????5673??2013-12-29?19:31??S4-1\S4-1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????336??2013-12-29?19:33??S4-1\S4-1\obj\Debug\S4-1.csproj.FileListAbsolute.txt
?????文件???????7168??2013-12-29?19:31??S4-1\S4-1\obj\Debug\S4-1.exe
?????文件??????22016??2013-12-29?19:31??S4-1\S4-1\obj\Debug\S4-1.pdb
?????文件???????1340??2013-12-29?19:30??S4-1\S4-1\Properties\AssemblyInfo.cs
?????文件???????2354??2013-12-29?19:31??S4-1\S4-1\S4-1.csproj
?????文件????????902??2013-12-29?19:30??S4-1\S4-1.sln
????..A..H.?????15360??2013-12-29?19:33??S4-1\S4-1.suo
?????目錄??????????0??2013-12-29?19:30??S4-1\S4-1\obj\Debug\TempPE
?????目錄??????????0??2013-12-29?19:33??S4-1\S4-1\bin\Debug
?????目錄??????????0??2013-12-29?19:31??S4-1\S4-1\obj\Debug
?????目錄??????????0??2013-12-29?19:30??S4-1\S4-1\bin
?????目錄??????????0??2013-12-29?19:30??S4-1\S4-1\obj
?????目錄??????????0??2013-12-29?19:30??S4-1\S4-1\Properties
?????目錄??????????0??2013-12-29?19:30??S4-1\S4-1
?????目錄??????????0??2013-12-29?19:30??S4-1
-----------?---------??----------?-----??----
???????????????101574????????????????????21
評論
共有 條評論