抽象类>什么是抽象类
抽象类的声明中包含abstract关键字。只要有一个方法具有abstract修饰即声明为抽象方法,那么这个类就是抽象类。
抽象类,就是提供类的定义,不提供类的具体实现细节(可实现部分细节,但不全实现)。抽象类的实现交由派生类来完成。
抽象类>声明抽象类
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 购买书籍的抽象类
/// </summary>
public abstract class BuyBookOperate
{
public abstract void BuyBook();
//书店
public abstract string BookShop
{ get; set; }
//书名
public abstract string BookName
{ get; set; }
//支付行为
public void PayMoney()
{
Console.WriteLine("支付款项正在进行中......");
}
//总金额
private int _totalmoney;
public int TotalMoney
{
get
{
return _totalmoney;
}
set
{
_totalmoney = value;
if (_totalmoney > 100)
{
Console.WriteLine("您购买的书籍价格超过了100块钱");
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 购买书籍的抽象类
/// </summary>
public abstract class BuyBookOperate
{
public abstract void BuyBook();
//书店
public abstract string BookShop
{ get; set; }
//书名
public abstract string BookName
{ get; set; }
//支付行为
public void PayMoney()
{
Console.WriteLine("支付款项正在进行中......");
}
//总金额
private int _totalmoney;
public int TotalMoney
{
get
{
return _totalmoney;
}
set
{
_totalmoney = value;
if (_totalmoney > 100)
{
Console.WriteLine("您购买的书籍价格超过了100块钱");
}
}
}
}
}
抽象类>实现抽象类
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 抽象类的实现。
/// </summary>
public class ConcreteBuyBook : BuyBookOperate
{
//实现抽象方法
public override void BuyBook()
{
BookShop = "新华书店";
BookName = "C#的书籍";
TotalMoney = 101;
PayMoney();
}
private string _bookshop;
//实现抽象属性
public override string BookShop
{
get
{
return _bookshop;
}
set
{
_bookshop = value;
}
}
private string _bookname;
//实现抽象属性。
public override string BookName
{
get
{
return _bookname;
}
set
{
_bookname = value;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 抽象类的实现。
/// </summary>
public class ConcreteBuyBook : BuyBookOperate
{
//实现抽象方法
public override void BuyBook()
{
BookShop = "新华书店";
BookName = "C#的书籍";
TotalMoney = 101;
PayMoney();
}
private string _bookshop;
//实现抽象属性
public override string BookShop
{
get
{
return _bookshop;
}
set
{
_bookshop = value;
}
}
private string _bookname;
//实现抽象属性。
public override string BookName
{
get
{
return _bookname;
}
set
{
_bookname = value;
}
}
}
}
运行它
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ConcreteBuyBook cb1 = new ConcreteBuyBook();
Console.WriteLine(cb1.ToString() + "类的输出结果为:");
Buybook(cb1);
}
/// <summary>
/// 买书的动作,传递抽象类的实现.
/// </summary>
/// <param name="bop"></param>
static void Buybook(BuyBookOperate bop)
{
//调用在抽象类中定义的BuyBook方法
bop.BuyBook();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ConcreteBuyBook cb1 = new ConcreteBuyBook();
Console.WriteLine(cb1.ToString() + "类的输出结果为:");
Buybook(cb1);
}
/// <summary>
/// 买书的动作,传递抽象类的实现.
/// </summary>
/// <param name="bop"></param>
static void Buybook(BuyBookOperate bop)
{
//调用在抽象类中定义的BuyBook方法
bop.BuyBook();
}
}
}
多态>什么是多态
具有名称相同的方法或属性,的多个类,这些类具有不同的行为,但共享相同的基类或接口。
多态>多态的实现
Concrete2BuyBook.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Concrete2BuyBook : BuyBookOperate
{
//实现抽象方法
public override void BuyBook()
{
BookShop = "某专业书店";
BookName = "ASP.NET的书籍";
TotalMoney = 102;
PayMoney();
}
private string _bookshop;
//实现抽象属性
public override string BookShop
{
get
{
return _bookshop;
}
set
{
_bookshop = value;
}
}
private string _bookname;
//实现抽象属性。
public override string BookName
{
get
{
return _bookname;
}
set
{
_bookname = value;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Concrete2BuyBook : BuyBookOperate
{
//实现抽象方法
public override void BuyBook()
{
BookShop = "某专业书店";
BookName = "ASP.NET的书籍";
TotalMoney = 102;
PayMoney();
}
private string _bookshop;
//实现抽象属性
public override string BookShop
{
get
{
return _bookshop;
}
set
{
_bookshop = value;
}
}
private string _bookname;
//实现抽象属性。
public override string BookName
{
get
{
return _bookname;
}
set
{
_bookname = value;
}
}
}
}
运行它
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ConcreteBuyBook cb1 = new ConcreteBuyBook();
Console.WriteLine(cb1.ToString() + "类的输出结果为:");
Buybook(cb1);
Concrete2BuyBook cb2 = new Concrete2BuyBook();
Console.WriteLine(cb2.ToString() + "类的输出结果为:");
Buybook(cb2);
}
/// <summary>
/// 买书的动作,传递抽象类的实现.
/// </summary>
/// <param name="bop"></param>
static void Buybook(BuyBookOperate bop)
{
//调用在抽象类中定义的BuyBook方法
bop.BuyBook();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ConcreteBuyBook cb1 = new ConcreteBuyBook();
Console.WriteLine(cb1.ToString() + "类的输出结果为:");
Buybook(cb1);
Concrete2BuyBook cb2 = new Concrete2BuyBook();
Console.WriteLine(cb2.ToString() + "类的输出结果为:");
Buybook(cb2);
}
/// <summary>
/// 买书的动作,传递抽象类的实现.
/// </summary>
/// <param name="bop"></param>
static void Buybook(BuyBookOperate bop)
{
//调用在抽象类中定义的BuyBook方法
bop.BuyBook();
}
}
}
接口>什么是接口
接口和抽象类相同点是,不能被直接实例化。
接口和抽象类不同点是,接口并不提供任何实现,接口所有的方法与属性都是抽象的。可以利用接口实现多继承。
接口>实现接口
如果声明两个具有相同方法名称的接口,当在一个类中同时进行实现这两个接口,要对其中一个隐式实现接口,而另外一个要求实现显式接口。否则报错。
Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace interface2
{
public interface Ibook1
{
void GetPrice();
}
public interface Ibook2
{
void GetPrice();
}
public class Book : Ibook1, Ibook2
{
#region Ibook2 成员
public void GetPrice()
{
Console.WriteLine("Ibook2的隐式接口实现");
}
#endregion
#region Ibook1 成员
void Ibook1.GetPrice()
{
Console.WriteLine("Ibook1显示接口实现");
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
namespace interface2
{
public interface Ibook1
{
void GetPrice();
}
public interface Ibook2
{
void GetPrice();
}
public class Book : Ibook1, Ibook2
{
#region Ibook2 成员
public void GetPrice()
{
Console.WriteLine("Ibook2的隐式接口实现");
}
#endregion
#region Ibook1 成员
void Ibook1.GetPrice()
{
Console.WriteLine("Ibook1显示接口实现");
}
#endregion
}
}
Interface1.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace interface2
{
public interface Ibook1
{
void GetPrice();
}
public interface Ibook2
{
void GetPrice();
}
public class Book : Ibook1, Ibook2
{
#region Ibook2 成员
public void GetPrice()
{
Console.WriteLine("Ibook2的隐式接口实现");
}
#endregion
#region Ibook1 成员
void Ibook1.GetPrice()
{
Console.WriteLine("Ibook1显示接口实现");
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
namespace interface2
{
public interface Ibook1
{
void GetPrice();
}
public interface Ibook2
{
void GetPrice();
}
public class Book : Ibook1, Ibook2
{
#region Ibook2 成员
public void GetPrice()
{
Console.WriteLine("Ibook2的隐式接口实现");
}
#endregion
#region Ibook1 成员
void Ibook1.GetPrice()
{
Console.WriteLine("Ibook1显示接口实现");
}
#endregion
}
}