|
對(duì)于一些C#的初學(xué)者,類似于面向?qū)ο笾械亩鄳B(tài)、接口、抽象、不是能很好的理解,本帖只針對(duì)一些初學(xué)者對(duì)這幾個(gè)概念不是很理解的人看,
牛人請(qǐng)繞道額就不要浪費(fèi)時(shí)間了哈!我一個(gè)一個(gè)來就舉一些簡(jiǎn)單易懂的例子!
多態(tài)
直接上例子:

using System;//包含基本類和基類
using System.Collections.Generic;//定義泛型集合的接口和類
using System.Linq;//支持語言集成查詢
using System.Text;//包含用于字符編碼等功能的對(duì)象
namespace Test
{
class Program
{
static voidMain(string[] args)
{
MyHobby[]hobbies=new MyHobby[4];
hobbies[0] = new MyHobby();
hobbies[1] = new Writing();
hobbies[2] = new Programing();
hobbies[3] = new Music();
foreach(MyHobby obj in hobbies)
{
obj.Interest();
}
Console.ReadKey();
}
}
/// <summary>
/// 基類 MyHobby
/// </summary>
class MyHobby
{
public virtual void Interest()
{
Console.WriteLine("我的愛好是:");
}
}
/// <summary>
/// Writing 繼承 MyHobby
/// </summary>
class Writing : MyHobby
{
public override void Interest()//重載方法Interest()
{
Console.WriteLine("寫作");
}
}
/// <summary>
/// Programing 繼承 MyHobby
/// </summary>
class Programing : MyHobby
{
public override void Interest()//重載方法Interest()
{
Console.WriteLine("編程");
}
}
/// <summary>
/// Music 繼承 MyHobby
/// </summary>
class Music : MyHobby
{
public override void Interest()//重載方法Interest()
{
Console.WriteLine("音樂");
}
}
}

運(yùn)行結(jié)果:

總結(jié):思想是,從基類中派生出三個(gè)類,在這三個(gè)類中重載基類的方法,然后重新調(diào)用派生類中的方法。因?yàn)槊恳淮沃剌d的功能都不一樣,所以就實(shí)現(xiàn)了多態(tài)!
接口
還是用一個(gè)例子來說明:
using System;//包含基本類和基類
using System.Collections.Generic;//定義泛型集合的接口和類
using System.Linq;//支持語言集成查詢
using System.Text;//包含用于字符編碼等功能的對(duì)象
namespace Test
{
class Program
{
static voidMain(string[] args)
{
UseInterface u = new UseInterface();//創(chuàng)建對(duì)象,調(diào)用方法
u.SayHello();
Console.ReadKey();
}
}
/// <summary>
/// 定義了一個(gè)接口FirstInterface
/// </summary>
interface FirstInterface
{
string Name//定義了一個(gè)屬性
{
get;
set;
}
void SayHello();//關(guān)鍵定義了方法,但是沒有方法體!
}
/// <summary>
/// 既然已經(jīng)繼承了接口,那必須要實(shí)現(xiàn)接口中的全部屬性,方法等,缺一不可,
/// 例如如果不實(shí)現(xiàn)Name屬性,程序就會(huì)報(bào)錯(cuò)!
/// </summary>
class UseInterface : FirstInterface
{
string name = "我的名字叫龍騰!";
public string Name//重新實(shí)現(xiàn)接口中的屬性
{
get { return name; }
set { name = value; }
}
/// <summary>
/// 實(shí)現(xiàn)接口中的方法,這里不需要像多態(tài)中用到的 override 改寫方法,直接拿過來加上方法體就可!
/// </summary>
public void SayHello()
{
Console.WriteLine("大家好!"+this.name);
}
}
}
運(yùn)行結(jié)果:

總結(jié):接口的本質(zhì)就是一些方法!但是要使用這些方法必須要按照一定的規(guī)則,否則就不能使用。定義接口,其實(shí)質(zhì)就是定義規(guī)則,要使用接口,必須在它的規(guī)則下進(jìn)行!
抽象
抽象類和接口有一些相似之處,它們都包含由派生類繼承的成員,接口和抽象類都不能直接實(shí)例化?。ㄌ摰臇|西你怎么能拿來使用呢對(duì)吧?必須要使用第三方的東西來搭橋)
using System;//包含基本類和基類
using System.Collections.Generic;//定義泛型集合的接口和類
using System.Linq;//支持語言集成查詢
using System.Text;//包含用于字符編碼等功能的對(duì)象
namespace Test
{
class Program
{
static voidMain(string[] args)
{
UseMyAbstract u = new UseMyAbstract();//創(chuàng)建對(duì)象,調(diào)用方法
u.SayHello();
Console.ReadKey();
}
}
/// <summary>
/// 定義一個(gè)抽象類MyAbstract
/// </summary>
abstract class MyAbstract
{
public abstract void SayHello();//定義一個(gè)抽象方法,同樣沒有方法體!
}
/// <summary>
///定義一個(gè) UseMyAbstract類 繼承 MyAbstract抽象類
/// </summary>
class UseMyAbstract : MyAbstract
{
public override void SayHello()//改寫抽象類MyAbstract中的方法SayHello()
{
Console.WriteLine("大家好!我是龍騰!");
}
}
}
運(yùn)行結(jié)果:

總結(jié):抽象類可以擁有抽象成員,但是不能直接被實(shí)例化,并且沒有代碼體,它的執(zhí)行方法是在其派生類中實(shí)現(xiàn)的!注意,只要這個(gè)類中有一個(gè)方法定義為抽象方法,那么這個(gè)類必須定義為抽象類!
以上只是我個(gè)人的一些小的學(xué)習(xí)感悟!希望能幫到那些需要幫到的人!
|