【转】接口 与 抽象类
生活随笔
收集整理的這篇文章主要介紹了
【转】接口 与 抽象类
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文轉(zhuǎn)自:http://blog.***/article.asp?id=89
介紹:
?? 在本文中,我將借一個(gè)DEMO討論下接口和抽象類。抽象類和接口的的概念對(duì)初學(xué)面向?qū)ο缶幊痰娜藖?lái)說(shuō),總?cè)菀酌曰蟆K?#xff0c;我試著討論下兩者的
理論并比較他們的使用。最后我將演示下怎么用C#使用它們。
背景:
一個(gè)抽象類在沒(méi)有執(zhí)行時(shí)就象一個(gè)接口一樣。但在它們間有很多不同點(diǎn),下面就解釋它們的概念,比較他們的相似點(diǎn)和不同點(diǎn)。
什么是抽象類?
一個(gè)抽象類是一種特殊的類,它無(wú)法實(shí)例化。所以,我們會(huì)問(wèn),那我們?yōu)槭裁催€需要一個(gè)無(wú)法實(shí)例化的類呢?一個(gè)抽象類僅用來(lái)被繼承的,即它只
允許其他類繼承它,但自己不能實(shí)例化。對(duì)繼承的子集,抽象類的優(yōu)勢(shì)是增強(qiáng)了某些層次結(jié)構(gòu)。簡(jiǎn)而言之,它是一種契約,使所有繼承它的子類都帶有
一樣的層次結(jié)構(gòu)或標(biāo)準(zhǔn)。
什么是接口?
接口不是一個(gè)類。一個(gè)接口沒(méi)有執(zhí)行機(jī)制。它只能有一個(gè)信號(hào),換句話說(shuō),它只能夠定義方法名字,方法怎么實(shí)現(xiàn)的一無(wú)所有。有一點(diǎn)是和抽象類
相似的,它也具有一種契約,接口被用來(lái)定義所有子類的結(jié)構(gòu),或者說(shuō)是定義一套子類方法。它們間的主要區(qū)別就是,一個(gè)類能實(shí)現(xiàn)或執(zhí)行多個(gè)接口,
而只能從一個(gè)抽象類繼承。在C#中,不支持向C++那樣一個(gè)類可以多重繼承,但接口的運(yùn)用,從另外的角度看,以用來(lái)實(shí)現(xiàn)了多繼承。
接口和抽象類:
?? 當(dāng)我們創(chuàng)建一個(gè)接口,相當(dāng)于我們基本上創(chuàng)建了一套沒(méi)有執(zhí)行的方法,需要我們?cè)谧宇愔羞^(guò)載(Overriden)實(shí)現(xiàn)。這樣做有一個(gè)非常顯著的優(yōu)勢(shì),
它提供了一種方法,使一個(gè)類可以成為兩個(gè)類的一部分.
?? 當(dāng)我們創(chuàng)建一個(gè)抽象類,相當(dāng)于我們創(chuàng)建了一個(gè)基類,它擁有一個(gè)或著多個(gè)完整的方法,??
?? 但至少有一個(gè)方法沒(méi)有實(shí)現(xiàn),即被聲明為抽象方法。若所有的方法都沒(méi)實(shí)現(xiàn),那它和接口的功能是一樣的,只是它還遵守?zé)o法被繼承的規(guī)則。
?? 抽象類的目的是提供了一個(gè)基本類,定義了子類將如何設(shè)計(jì),允許程序員在子類中實(shí)現(xiàn)這些方法。
特點(diǎn)
接 口
????????抽象類
多繼承
一個(gè)類能從幾個(gè)接口繼承
一個(gè)類只能繼承于一個(gè)抽象類????????????
實(shí)現(xiàn)方式
一個(gè)接口沒(méi)有任何實(shí)現(xiàn)的代碼只顯示方法名
抽象類可以提供完整的方法實(shí)現(xiàn)的代碼
核心與外圍
接口用來(lái)定義類的外圍功能.
比如人類和交通工具兩個(gè)類都能從IMOVABLE接口繼承。??
抽象類多用來(lái)定義一個(gè)類的核心層
使用場(chǎng)合
如果很多實(shí)現(xiàn)都共享一些方法,則??????
用接口比較
如果很多實(shí)現(xiàn)使用同一系列方法,使用一樣的屬性,則用抽象類較好
速度??
要求更多的時(shí)間,去找到實(shí)際的子類實(shí)現(xiàn)的方法
比較快
功能擴(kuò)展性
如果我們要給接口添加一個(gè)方法,我們要捕捉所有使用該接口的實(shí)現(xiàn)子類,并分別添加一個(gè)新的方法,并實(shí)現(xiàn)它.
如果我們要給抽象類添加一個(gè)新的方法我們可以在抽象實(shí)現(xiàn),也可以在子類實(shí)現(xiàn)
代碼實(shí)例:
用代碼來(lái)實(shí)現(xiàn)會(huì)更簡(jiǎn)單些。這里有一個(gè)Employee抽象類,和一個(gè)IEmployee接口。我會(huì)用實(shí)現(xiàn)的對(duì)象分別繼承于Employee和IEmployee,Emp_Fulltime繼承于Employee,Emp_Fulltime2繼承于IEmployee。
在測(cè)試中,我創(chuàng)建兩者的實(shí)例,并設(shè)置他們的屬性,調(diào)用一個(gè)calculateWage的方法。
Abstract Class Employee
1 using System;
2
3 namespace AbstractsANDInterfaces
4 {
5???? ///
6
7???? /// Summary description for Employee.
8
9???? ///
10
11????
12???? public abstract class Employee
13???? {
14???????? //we can have fields and properties
15
16???????? //in the Abstract class
17
18???????? protected String id;
19???????? protected String lname;
20???????? protected String fname;
21
22???????? //properties
23
24???????? public abstract String ID
25???????? {
26???????????? get;
27???????????? set;
28???????? }
29
30???????? public abstract String FirstName
31???????? {
32???????????? get;
33???????????? set;
34???????? }
35????????
36???????? public abstract String LastName
37???????? {
38???????????? get;
39???????????? set;
40???????? }
41???????? //completed methods
42
43???????? public String Update()
44???????? {
45???????????? return "Employee " + id + " " +
46?????????????????????? lname + " " + fname +
47?????????????????????? " updated";
48???????? }
49???????? //completed methods
50
51???????? public String Add()
52???????? {
53???????????? return "Employee " + id + " " +
54?????????????????????? lname + " " + fname +
55?????????????????????? " added";
56???????? }
57???????? //completed methods
58
59???????? public String Delete()
60???????? {
61???????????? return "Employee " + id + " " +
62?????????????????????? lname + " " + fname +
63?????????????????????? " deleted";
64???????? }
65???????? //completed methods
66
67???????? public String Search()
68???????? {
69???????????? return "Employee " + id + " " +
70?????????????????????? lname + " " + fname +
71?????????????????????? " found";
72???????? }
73
74???????? //abstract method that is different
75
76???????? //from Fulltime and Contractor
77
78???????? //therefore i keep it uncompleted and
79
80???????? //let each implementation
81
82???????? //complete it the way they calculate the wage.
83
84???????? public abstract String CalculateWage();
85????????
86???? }
87 }
88
Interface Employee??1 using System;
2
3
4 namespace AbstractsANDInterfaces
5 {
6???? /// <summary>
7
8???? /// Summary description for IEmployee.
9
10???? /// </summary>
11
12???? public interface IEmployee
13???? {
14???????? //cannot have fields. uncommenting
15
16???????? //will raise error!
17
18 //????????protected String id;
19
20 //????????protected String lname;
21
22 //????????protected String fname;
23
24
25???????? //just signature of the properties
26
27???????? //and methods.
28
29???????? //setting a rule or contract to be
30
31???????? //followed by implementations.
32
33???????? String ID
34???????? {
35???????????? get;
36???????????? set;
37???????? }
38
39???????? String FirstName
40???????? {
41???????????? get;
42???????????? set;
43???????? }
44????????
45???????? String LastName
46???????? {
47???????????? get;
48???????????? set;
49???????? }
50????????
51???????? // cannot have implementation
52
53???????? // cannot have modifiers public
54
55???????? // etc all are assumed public
56
57???????? // cannot have virtual
58
59
60???????? String Update();
61
62???????? String Add();
63
64???????? String Delete();
65
66???????? String Search();
67
68???????? String CalculateWage();
69???? }
70 }
71
Inherited Objects
Emp_Fulltime:
??1 using System;
??2
??3 namespace AbstractsANDInterfaces
??4 {
??5???? ///
??6
??7???? /// Summary description for Emp_Fulltime.
??8
??9???? ///
10
11??????
12???? //Inheriting from the Abstract class
13
14???? public class Emp_Fulltime : Employee
15???? {
16???????? //uses all the properties of the
17
18???????? //Abstract class therefore no
19
20???????? //properties or fields here!
21
22
23???????? public Emp_Fulltime()
24???????? {
25???????? }
26
27
28???????? public override String ID
29???????? {
30???????????? get
31???????????? {
32???????????????? return id;
33???????????? }
34???????????? set
35???????????? {
36???????????????? id = value;
37???????????? }
38???????? }
39????????
40???????? public override String FirstName
41???????? {
42???????????? get
43???????????? {
44???????????????? return fname;
45???????????? }
46???????????? set
47???????????? {
48???????????????? fname = value;
49???????????? }
50???????? }
51
52???????? public override String LastName
53???????? {
54???????????? get
55???????????? {
56???????????????? return lname;
57???????????? }
58???????????? set
59???????????? {
60???????????????? lname = value;
61???????????? }
62???????? }
63
64???????? //common methods that are
65
66???????? //implemented in the abstract class
67
68???????? public new String Add()
69???????? {
70???????????? return base.Add();
71???????? }
72???????? //common methods that are implemented
73
74???????? //in the abstract class
75
76???????? public new String Delete()
77???????? {
78???????????? return base.Delete();
79???????? }
80???????? //common methods that are implemented
81
82???????? //in the abstract class
83
84???????? public new String Search()
85???????? {
86???????????? return base.Search();
87???????? }
88???????? //common methods that are implemented
89
90???????? //in the abstract class
91
92???????? public new String Update()
93???????? {
94???????????? return base.Update();
95???????? }
96????????
97???????? //abstract method that is different
98
99???????? //from Fulltime and Contractor
100
101???????? //therefore I override it here.
102
103???????? public override String CalculateWage()
104???????? {
105???????????? return "Full time employee " +
106?????????????????? base.fname + " is calculated " +
107?????????????????? "using the Abstract class";
108???????? }
109???? }
110 }
111
Emp_Fulltime2
??1 using System;
??2
??3 namespace AbstractsANDInterfaces
??4 {
??5???? ///
??6
??7???? /// Summary description for Emp_fulltime2.
??8
??9???? ///
10
11????
12???? //Implementing the interface
13
14???? public class Emp_fulltime2 : IEmployee
15???? {
16???????? //All the properties and
17
18???????? //fields are defined here!
19
20???????? protected String id;
21???????? protected String lname;
22???????? protected String fname;
23
24???????? public Emp_fulltime2()
25???????? {
26???????????? //
27
28???????????? // TODO: Add constructor logic here
29
30???????????? //
31
32???????? }
33
34???????? public String ID
35???????? {
36???????????? get
37???????????? {
38???????????????? return id;
39???????????? }
40???????????? set
41???????????? {
42???????????????? id = value;
43???????????? }
44???????? }
45????????
46???????? public String FirstName
47???????? {
48???????????? get
49???????????? {
50???????????????? return fname;
51???????????? }
52???????????? set
53???????????? {
54???????????????? fname = value;
55???????????? }
56???????? }
57
58???????? public String LastName
59???????? {
60???????????? get
61???????????? {
62???????????????? return lname;
63???????????? }
64???????????? set
65???????????? {
66???????????????? lname = value;
67???????????? }
68???????? }
69
70???????? //all the manipulations including Add,Delete,
71
72???????? //Search, Update, Calculate are done
73
74???????? //within the object as there are not
75
76???????? //implementation in the Interface entity.
77
78???????? public String Add()
79???????? {
80???????????? return "Fulltime Employee " +
81?????????????????????????? fname + " added.";
82???????? }
83
84???????? public String Delete()
85???????? {
86???????????? return "Fulltime Employee " +
87???????????????????????? fname + " deleted.";
88???????? }
89
90???????? public String Search()
91???????? {
92???????????? return "Fulltime Employee " +
93????????????????????????fname + " searched.";
94???????? }
95
96???????? public String Update()
97???????? {
98???????????? return "Fulltime Employee " +
99???????????????????????? fname + " updated.";
100???????? }
101????????
102???????? //if you change to Calculatewage().
103
104???????? //Just small 'w' it will raise
105
106???????? //error as in interface
107
108???????? //it is CalculateWage() with capital 'W'.
109
110???????? public String CalculateWage()
111???????? {
112???????????? return "Full time employee " +
113?????????????????? fname + " caluculated using " +
114?????????????????? "Interface.";
115???????? }
116???? }
117 }
118
Code for testing
1 private void InterfaceExample_Click(object sender,
2???????????????????????????????? System.EventArgs e)
3 {
4???? try
5???? {
6
7???????? IEmployee emp;
8
9???????? Emp_fulltime2 emp1 = new Emp_fulltime2();
10???????? //has to be casted because of the interface!
11
12???????? emp = (IEmployee) emp1;
13???????? emp.ID = "2234";
14???????? emp.FirstName= "Rahman" ;
15???????? emp.LastName = "Mahmoodi" ;
16???????? //call add method od the object
17
18???????? MessageBox.Show(emp.Add().ToString());
19????????
20???????? //call the CalculateWage method
21
22???????? MessageBox.Show(emp.CalculateWage().ToString());
23
24
25???? }
26???? catch(Exception ex)
27???? {
28???????? MessageBox.Show(ex.Message);
29???? }
30
31 }
32
33 private void cmdAbstractExample_Click(object sender,
34????????????????????????????????????System.EventArgs e)
35 {
36
37???? Employee emp;
38???? //no casting is requird!
39
40???? emp = new Emp_Fulltime();
41????
42
43???? emp.ID = "2244";
44???? emp.FirstName= "Maria" ;
45???? emp.LastName = "Robinlius" ;
46???? MessageBox.Show(emp.Add().ToString());
47
48???? //call the CalculateWage method
49
50???? MessageBox.Show(emp.CalculateWage().ToString());
51
52 }
53
結(jié)論:
我已經(jīng)解釋了接口和抽象類的不同點(diǎn),并用一個(gè)Demo Project 講解了他們實(shí)現(xiàn)的不同點(diǎn).
介紹:
?? 在本文中,我將借一個(gè)DEMO討論下接口和抽象類。抽象類和接口的的概念對(duì)初學(xué)面向?qū)ο缶幊痰娜藖?lái)說(shuō),總?cè)菀酌曰蟆K?#xff0c;我試著討論下兩者的
理論并比較他們的使用。最后我將演示下怎么用C#使用它們。
背景:
一個(gè)抽象類在沒(méi)有執(zhí)行時(shí)就象一個(gè)接口一樣。但在它們間有很多不同點(diǎn),下面就解釋它們的概念,比較他們的相似點(diǎn)和不同點(diǎn)。
什么是抽象類?
一個(gè)抽象類是一種特殊的類,它無(wú)法實(shí)例化。所以,我們會(huì)問(wèn),那我們?yōu)槭裁催€需要一個(gè)無(wú)法實(shí)例化的類呢?一個(gè)抽象類僅用來(lái)被繼承的,即它只
允許其他類繼承它,但自己不能實(shí)例化。對(duì)繼承的子集,抽象類的優(yōu)勢(shì)是增強(qiáng)了某些層次結(jié)構(gòu)。簡(jiǎn)而言之,它是一種契約,使所有繼承它的子類都帶有
一樣的層次結(jié)構(gòu)或標(biāo)準(zhǔn)。
什么是接口?
接口不是一個(gè)類。一個(gè)接口沒(méi)有執(zhí)行機(jī)制。它只能有一個(gè)信號(hào),換句話說(shuō),它只能夠定義方法名字,方法怎么實(shí)現(xiàn)的一無(wú)所有。有一點(diǎn)是和抽象類
相似的,它也具有一種契約,接口被用來(lái)定義所有子類的結(jié)構(gòu),或者說(shuō)是定義一套子類方法。它們間的主要區(qū)別就是,一個(gè)類能實(shí)現(xiàn)或執(zhí)行多個(gè)接口,
而只能從一個(gè)抽象類繼承。在C#中,不支持向C++那樣一個(gè)類可以多重繼承,但接口的運(yùn)用,從另外的角度看,以用來(lái)實(shí)現(xiàn)了多繼承。
接口和抽象類:
?? 當(dāng)我們創(chuàng)建一個(gè)接口,相當(dāng)于我們基本上創(chuàng)建了一套沒(méi)有執(zhí)行的方法,需要我們?cè)谧宇愔羞^(guò)載(Overriden)實(shí)現(xiàn)。這樣做有一個(gè)非常顯著的優(yōu)勢(shì),
它提供了一種方法,使一個(gè)類可以成為兩個(gè)類的一部分.
?? 當(dāng)我們創(chuàng)建一個(gè)抽象類,相當(dāng)于我們創(chuàng)建了一個(gè)基類,它擁有一個(gè)或著多個(gè)完整的方法,??
?? 但至少有一個(gè)方法沒(méi)有實(shí)現(xiàn),即被聲明為抽象方法。若所有的方法都沒(méi)實(shí)現(xiàn),那它和接口的功能是一樣的,只是它還遵守?zé)o法被繼承的規(guī)則。
?? 抽象類的目的是提供了一個(gè)基本類,定義了子類將如何設(shè)計(jì),允許程序員在子類中實(shí)現(xiàn)這些方法。
特點(diǎn)
接 口
????????抽象類
多繼承
一個(gè)類能從幾個(gè)接口繼承
一個(gè)類只能繼承于一個(gè)抽象類????????????
實(shí)現(xiàn)方式
一個(gè)接口沒(méi)有任何實(shí)現(xiàn)的代碼只顯示方法名
抽象類可以提供完整的方法實(shí)現(xiàn)的代碼
核心與外圍
接口用來(lái)定義類的外圍功能.
比如人類和交通工具兩個(gè)類都能從IMOVABLE接口繼承。??
抽象類多用來(lái)定義一個(gè)類的核心層
使用場(chǎng)合
如果很多實(shí)現(xiàn)都共享一些方法,則??????
用接口比較
如果很多實(shí)現(xiàn)使用同一系列方法,使用一樣的屬性,則用抽象類較好
速度??
要求更多的時(shí)間,去找到實(shí)際的子類實(shí)現(xiàn)的方法
比較快
功能擴(kuò)展性
如果我們要給接口添加一個(gè)方法,我們要捕捉所有使用該接口的實(shí)現(xiàn)子類,并分別添加一個(gè)新的方法,并實(shí)現(xiàn)它.
如果我們要給抽象類添加一個(gè)新的方法我們可以在抽象實(shí)現(xiàn),也可以在子類實(shí)現(xiàn)
代碼實(shí)例:
用代碼來(lái)實(shí)現(xiàn)會(huì)更簡(jiǎn)單些。這里有一個(gè)Employee抽象類,和一個(gè)IEmployee接口。我會(huì)用實(shí)現(xiàn)的對(duì)象分別繼承于Employee和IEmployee,Emp_Fulltime繼承于Employee,Emp_Fulltime2繼承于IEmployee。
在測(cè)試中,我創(chuàng)建兩者的實(shí)例,并設(shè)置他們的屬性,調(diào)用一個(gè)calculateWage的方法。
Abstract Class Employee
1 using System;
2
3 namespace AbstractsANDInterfaces
4 {
5???? ///
6
7???? /// Summary description for Employee.
8
9???? ///
10
11????
12???? public abstract class Employee
13???? {
14???????? //we can have fields and properties
15
16???????? //in the Abstract class
17
18???????? protected String id;
19???????? protected String lname;
20???????? protected String fname;
21
22???????? //properties
23
24???????? public abstract String ID
25???????? {
26???????????? get;
27???????????? set;
28???????? }
29
30???????? public abstract String FirstName
31???????? {
32???????????? get;
33???????????? set;
34???????? }
35????????
36???????? public abstract String LastName
37???????? {
38???????????? get;
39???????????? set;
40???????? }
41???????? //completed methods
42
43???????? public String Update()
44???????? {
45???????????? return "Employee " + id + " " +
46?????????????????????? lname + " " + fname +
47?????????????????????? " updated";
48???????? }
49???????? //completed methods
50
51???????? public String Add()
52???????? {
53???????????? return "Employee " + id + " " +
54?????????????????????? lname + " " + fname +
55?????????????????????? " added";
56???????? }
57???????? //completed methods
58
59???????? public String Delete()
60???????? {
61???????????? return "Employee " + id + " " +
62?????????????????????? lname + " " + fname +
63?????????????????????? " deleted";
64???????? }
65???????? //completed methods
66
67???????? public String Search()
68???????? {
69???????????? return "Employee " + id + " " +
70?????????????????????? lname + " " + fname +
71?????????????????????? " found";
72???????? }
73
74???????? //abstract method that is different
75
76???????? //from Fulltime and Contractor
77
78???????? //therefore i keep it uncompleted and
79
80???????? //let each implementation
81
82???????? //complete it the way they calculate the wage.
83
84???????? public abstract String CalculateWage();
85????????
86???? }
87 }
88
Interface Employee??1 using System;
2
3
4 namespace AbstractsANDInterfaces
5 {
6???? /// <summary>
7
8???? /// Summary description for IEmployee.
9
10???? /// </summary>
11
12???? public interface IEmployee
13???? {
14???????? //cannot have fields. uncommenting
15
16???????? //will raise error!
17
18 //????????protected String id;
19
20 //????????protected String lname;
21
22 //????????protected String fname;
23
24
25???????? //just signature of the properties
26
27???????? //and methods.
28
29???????? //setting a rule or contract to be
30
31???????? //followed by implementations.
32
33???????? String ID
34???????? {
35???????????? get;
36???????????? set;
37???????? }
38
39???????? String FirstName
40???????? {
41???????????? get;
42???????????? set;
43???????? }
44????????
45???????? String LastName
46???????? {
47???????????? get;
48???????????? set;
49???????? }
50????????
51???????? // cannot have implementation
52
53???????? // cannot have modifiers public
54
55???????? // etc all are assumed public
56
57???????? // cannot have virtual
58
59
60???????? String Update();
61
62???????? String Add();
63
64???????? String Delete();
65
66???????? String Search();
67
68???????? String CalculateWage();
69???? }
70 }
71
Inherited Objects
Emp_Fulltime:
??1 using System;
??2
??3 namespace AbstractsANDInterfaces
??4 {
??5???? ///
??6
??7???? /// Summary description for Emp_Fulltime.
??8
??9???? ///
10
11??????
12???? //Inheriting from the Abstract class
13
14???? public class Emp_Fulltime : Employee
15???? {
16???????? //uses all the properties of the
17
18???????? //Abstract class therefore no
19
20???????? //properties or fields here!
21
22
23???????? public Emp_Fulltime()
24???????? {
25???????? }
26
27
28???????? public override String ID
29???????? {
30???????????? get
31???????????? {
32???????????????? return id;
33???????????? }
34???????????? set
35???????????? {
36???????????????? id = value;
37???????????? }
38???????? }
39????????
40???????? public override String FirstName
41???????? {
42???????????? get
43???????????? {
44???????????????? return fname;
45???????????? }
46???????????? set
47???????????? {
48???????????????? fname = value;
49???????????? }
50???????? }
51
52???????? public override String LastName
53???????? {
54???????????? get
55???????????? {
56???????????????? return lname;
57???????????? }
58???????????? set
59???????????? {
60???????????????? lname = value;
61???????????? }
62???????? }
63
64???????? //common methods that are
65
66???????? //implemented in the abstract class
67
68???????? public new String Add()
69???????? {
70???????????? return base.Add();
71???????? }
72???????? //common methods that are implemented
73
74???????? //in the abstract class
75
76???????? public new String Delete()
77???????? {
78???????????? return base.Delete();
79???????? }
80???????? //common methods that are implemented
81
82???????? //in the abstract class
83
84???????? public new String Search()
85???????? {
86???????????? return base.Search();
87???????? }
88???????? //common methods that are implemented
89
90???????? //in the abstract class
91
92???????? public new String Update()
93???????? {
94???????????? return base.Update();
95???????? }
96????????
97???????? //abstract method that is different
98
99???????? //from Fulltime and Contractor
100
101???????? //therefore I override it here.
102
103???????? public override String CalculateWage()
104???????? {
105???????????? return "Full time employee " +
106?????????????????? base.fname + " is calculated " +
107?????????????????? "using the Abstract class";
108???????? }
109???? }
110 }
111
Emp_Fulltime2
??1 using System;
??2
??3 namespace AbstractsANDInterfaces
??4 {
??5???? ///
??6
??7???? /// Summary description for Emp_fulltime2.
??8
??9???? ///
10
11????
12???? //Implementing the interface
13
14???? public class Emp_fulltime2 : IEmployee
15???? {
16???????? //All the properties and
17
18???????? //fields are defined here!
19
20???????? protected String id;
21???????? protected String lname;
22???????? protected String fname;
23
24???????? public Emp_fulltime2()
25???????? {
26???????????? //
27
28???????????? // TODO: Add constructor logic here
29
30???????????? //
31
32???????? }
33
34???????? public String ID
35???????? {
36???????????? get
37???????????? {
38???????????????? return id;
39???????????? }
40???????????? set
41???????????? {
42???????????????? id = value;
43???????????? }
44???????? }
45????????
46???????? public String FirstName
47???????? {
48???????????? get
49???????????? {
50???????????????? return fname;
51???????????? }
52???????????? set
53???????????? {
54???????????????? fname = value;
55???????????? }
56???????? }
57
58???????? public String LastName
59???????? {
60???????????? get
61???????????? {
62???????????????? return lname;
63???????????? }
64???????????? set
65???????????? {
66???????????????? lname = value;
67???????????? }
68???????? }
69
70???????? //all the manipulations including Add,Delete,
71
72???????? //Search, Update, Calculate are done
73
74???????? //within the object as there are not
75
76???????? //implementation in the Interface entity.
77
78???????? public String Add()
79???????? {
80???????????? return "Fulltime Employee " +
81?????????????????????????? fname + " added.";
82???????? }
83
84???????? public String Delete()
85???????? {
86???????????? return "Fulltime Employee " +
87???????????????????????? fname + " deleted.";
88???????? }
89
90???????? public String Search()
91???????? {
92???????????? return "Fulltime Employee " +
93????????????????????????fname + " searched.";
94???????? }
95
96???????? public String Update()
97???????? {
98???????????? return "Fulltime Employee " +
99???????????????????????? fname + " updated.";
100???????? }
101????????
102???????? //if you change to Calculatewage().
103
104???????? //Just small 'w' it will raise
105
106???????? //error as in interface
107
108???????? //it is CalculateWage() with capital 'W'.
109
110???????? public String CalculateWage()
111???????? {
112???????????? return "Full time employee " +
113?????????????????? fname + " caluculated using " +
114?????????????????? "Interface.";
115???????? }
116???? }
117 }
118
Code for testing
1 private void InterfaceExample_Click(object sender,
2???????????????????????????????? System.EventArgs e)
3 {
4???? try
5???? {
6
7???????? IEmployee emp;
8
9???????? Emp_fulltime2 emp1 = new Emp_fulltime2();
10???????? //has to be casted because of the interface!
11
12???????? emp = (IEmployee) emp1;
13???????? emp.ID = "2234";
14???????? emp.FirstName= "Rahman" ;
15???????? emp.LastName = "Mahmoodi" ;
16???????? //call add method od the object
17
18???????? MessageBox.Show(emp.Add().ToString());
19????????
20???????? //call the CalculateWage method
21
22???????? MessageBox.Show(emp.CalculateWage().ToString());
23
24
25???? }
26???? catch(Exception ex)
27???? {
28???????? MessageBox.Show(ex.Message);
29???? }
30
31 }
32
33 private void cmdAbstractExample_Click(object sender,
34????????????????????????????????????System.EventArgs e)
35 {
36
37???? Employee emp;
38???? //no casting is requird!
39
40???? emp = new Emp_Fulltime();
41????
42
43???? emp.ID = "2244";
44???? emp.FirstName= "Maria" ;
45???? emp.LastName = "Robinlius" ;
46???? MessageBox.Show(emp.Add().ToString());
47
48???? //call the CalculateWage method
49
50???? MessageBox.Show(emp.CalculateWage().ToString());
51
52 }
53
結(jié)論:
我已經(jīng)解釋了接口和抽象類的不同點(diǎn),并用一個(gè)Demo Project 講解了他們實(shí)現(xiàn)的不同點(diǎn).
轉(zhuǎn)載于:https://www.cnblogs.com/feima-lxl/archive/2008/04/25/1170971.html
總結(jié)
以上是生活随笔為你收集整理的【转】接口 与 抽象类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: bzoj 1024 [ SCOI 200
- 下一篇: PPT常用功能及其实现