通过实例理解继承与多态原理与优点
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
12 CHILDRENS
13 }
14
15 class Movie
16 {
17 private string _title; //movie name
18 TYPE _typeCode; //price code
19
20 public Movie()
21 {
22 _title = "unname";
23 _typeCode = 0;
thisAmount += 2;//2天之内2元
if (each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;
break;
case TYPE.NEW_RELEASE:
thisAmount += each.getDaysRented() * 3;
Movie m2 = new Movie("我是特种兵之利刃出鞘", TYPE.REGULAR);
Movie m3 = new Movie("熊出没之环球大冒险", TYPE.CHILDRENS);
Rental r1 = new Rental(m1, 4);
Rental r2 = new Rental(m1, 2);
影片有三种类型:普通影片、儿童影片及新上映影片。
另外,模仿时下潮流,程序还提供了积分制度,为常客计算积分,积分会根据影片是否为新上映影片而不同。
2.本实例为控制台程序,运行界面如下:
三
根据需求,我们定义三个类,分别是movie类,Rental类(代表一条租用记录)和Customer类(租碟顾客)
其中movie类的代码如下:
Customer c4 = new Customer("孙俪");
c4.addRental(r2);
c4.addRental(r3);
c4.addRental(r5);
Console.Write(c1.statement());
Console.Write(c2.statement());
Console.Write(c3.statement());
35 }
36
37 void setTypeCode(TYPE arg)
38 {
39 _typeCode = arg;
40 }
41
42 public string getTitle()
43 {
44 return _title;
45 }
46 }
47 }
Rental类的代码如下,租用记录中包含了一个movie对象,以及一个租期成员(积分和租金与此有关):
Customer c2 = new Customer("林志玲");
c2.addRental(r1);
c2.addRental(r3);
c2.addRental(r2);
Customer c3 = new Customer("刘德华");
c3.addRental(r3);
c3.addRental(r5);
新的Movie类的代码如下:Movie类提供积分计算和租金计算的默认实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
public enum TYPE
public Rental(Movie movie, int daysRented)
{
_movie = movie;
_daysRented = daysRented;
}
public int getDaysRented()
{
return _daysRented;
}
public Movie getMovie()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
class Rental
{
private Movie _movie;
int _daysRented;
}
public Movie(string title, TYPE priceCode)
{
_title = title;
_priceCode = priceCode;
}
public virtual double getCharge(int daysRented)
{
return 0;//收费
}
public virtual int getFrequentRenterPoints(int daysRented)//积分
if ((each.getMovie().getTypeCode() == TYPE.NEW_RELEASE) &&
each.getDaysRented() > 1)
frequentRenterPoints++;
result += "\n\t" + each.getMovie().getTitle() + "\t" + thisAmount;
break;
case TYPE.CHILDRENS:
thisAmount += 1.5;//3天之内1.5元
if (each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
frequentRenterPoints++;//对于每一种类型的影片,一次租用积分加1
24 }
25
26 public Movie(string title, TYPE typeCode)
27 {
28 _title = title;
29 _typeCode = typeCode;
30 }
31
32 public TYPE getTypeCode()
33 {
34 return (TYPE)_typeCode;
{
public class ChildrensMovie : Movie
{
public ChildrensMovie (string title)
{
_title = title;
}
public override double getCharge(int daysRented)
{
double result = 1.5;
if (daysRented > 3)
result += (daysRented - 3) * 1.5;
return result;
}
}
}
NewReleaseMovie子类的代码如下:重写租金计算方法(多态性)和积分计算方法(多态性)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
class Program
{
static voidMain(string[] args)
{
Console.Write("影碟店客户租碟明细");
Movie m1 = new Movie("致我们终将逝去的青春", TYPE.NEW_RELEASE);
totalAmount += thisAmount;
}
result += "\n共消费" + totalAmount + "元" + "\n您增加了" + frequentRenterPoints + "个积分\n";
return result;
}
}
}
主程序代码如下:
using System;
using System.Collections.Generic;
}
string getName()
{
return _name;
}
public string statement()
{
double totalAmount = 0; //总共的租金
int frequentRenterPoints = 0;//积分
string result = "\r-------------------------------------------\n\r " +
private string _name;
List<Rental> _rentals = new List<Rental>();
public Customer(string name)
{
_name = name;
}
public void addRental(Rental arg)
{
_rentals.Add(arg);
}
ChildrensMovie子类的代码如下:重写租金计算方法(多态性),积分计算方法从父类继承。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
return daysRented * 3;
Rental r3 = new Rental(m3, 7);
Rental r4 = new Rental(m2, 5);
Rental r5 = new Rental(m3, 3);
Customer c1 = new Customer("孙红雷");
c1.addRental(r1);
c1.addRental(r4);
{
REGULAR,
NEW_RELEASE,
CHILDRENS
}
public class Movie
{
protected string _title; //movie name
TYPE _priceCode; //price code
public Movie()
{
_title = "unname";
_priceCode = 0;
通过
一
都说面向对象的4大支柱是抽象,封装,继承与多态。但是一些初涉编程的开发人员,体会不到继承与多态的妙用,本文就试以一个经典实例来诠释继承与多态的用武之地。
二
1.任务说明
我们的需求是一个影片出租的小应用,该应用会记录每个顾客的消费金额并打印出来。
程序输入为:顾客租的影片及对应的租期;
程序的处理为:根据顾客租用影片时间及影片类型,计算费用;输出:打印消费单。
{
return 1;
}
public TYPE getPriceCode()
{
return (TYPE)_priceCode;
}
void setPriceCode(TYPE arg)
{
_priceCode = arg;
}
public string getTitle()
{
return _title;
}
}
"租碟记录--- " + getName() + "\n";
foreach (Rental iter in _rentals)
{
double thisAmount = 0;
Rental each = iter;
switch (each.getMovie().getTypeCode())
{
case TYPE.REGULAR:
using System.Linq;
usiHale Waihona Puke Baidug System.Text;
namespace _1.cntbed
{
public class NewReleaseMovie:Movie
{
public NewReleaseMovie (string title)
{
_title = title;
}
public override double getCharge(int daysRented)
{
return _movie;
}
}
}
Customer类的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
class Customer
{
Console.Write(c4.statement());
}
}
}
四
我们定义一个Movie父类,每种影片类型均定义一个Movie子类(ChildrensMovie,NewReleaseMovie,RegularMovie),同时定义一个Movie工厂类(MovieFactoryMethod)。代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace _1.cntbed
7 {
8 public enum TYPE
9 {
10 REGULAR,
11 NEW_RELEASE,
13 }
14
15 class Movie
16 {
17 private string _title; //movie name
18 TYPE _typeCode; //price code
19
20 public Movie()
21 {
22 _title = "unname";
23 _typeCode = 0;
thisAmount += 2;//2天之内2元
if (each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;
break;
case TYPE.NEW_RELEASE:
thisAmount += each.getDaysRented() * 3;
Movie m2 = new Movie("我是特种兵之利刃出鞘", TYPE.REGULAR);
Movie m3 = new Movie("熊出没之环球大冒险", TYPE.CHILDRENS);
Rental r1 = new Rental(m1, 4);
Rental r2 = new Rental(m1, 2);
影片有三种类型:普通影片、儿童影片及新上映影片。
另外,模仿时下潮流,程序还提供了积分制度,为常客计算积分,积分会根据影片是否为新上映影片而不同。
2.本实例为控制台程序,运行界面如下:
三
根据需求,我们定义三个类,分别是movie类,Rental类(代表一条租用记录)和Customer类(租碟顾客)
其中movie类的代码如下:
Customer c4 = new Customer("孙俪");
c4.addRental(r2);
c4.addRental(r3);
c4.addRental(r5);
Console.Write(c1.statement());
Console.Write(c2.statement());
Console.Write(c3.statement());
35 }
36
37 void setTypeCode(TYPE arg)
38 {
39 _typeCode = arg;
40 }
41
42 public string getTitle()
43 {
44 return _title;
45 }
46 }
47 }
Rental类的代码如下,租用记录中包含了一个movie对象,以及一个租期成员(积分和租金与此有关):
Customer c2 = new Customer("林志玲");
c2.addRental(r1);
c2.addRental(r3);
c2.addRental(r2);
Customer c3 = new Customer("刘德华");
c3.addRental(r3);
c3.addRental(r5);
新的Movie类的代码如下:Movie类提供积分计算和租金计算的默认实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
public enum TYPE
public Rental(Movie movie, int daysRented)
{
_movie = movie;
_daysRented = daysRented;
}
public int getDaysRented()
{
return _daysRented;
}
public Movie getMovie()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
class Rental
{
private Movie _movie;
int _daysRented;
}
public Movie(string title, TYPE priceCode)
{
_title = title;
_priceCode = priceCode;
}
public virtual double getCharge(int daysRented)
{
return 0;//收费
}
public virtual int getFrequentRenterPoints(int daysRented)//积分
if ((each.getMovie().getTypeCode() == TYPE.NEW_RELEASE) &&
each.getDaysRented() > 1)
frequentRenterPoints++;
result += "\n\t" + each.getMovie().getTitle() + "\t" + thisAmount;
break;
case TYPE.CHILDRENS:
thisAmount += 1.5;//3天之内1.5元
if (each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
frequentRenterPoints++;//对于每一种类型的影片,一次租用积分加1
24 }
25
26 public Movie(string title, TYPE typeCode)
27 {
28 _title = title;
29 _typeCode = typeCode;
30 }
31
32 public TYPE getTypeCode()
33 {
34 return (TYPE)_typeCode;
{
public class ChildrensMovie : Movie
{
public ChildrensMovie (string title)
{
_title = title;
}
public override double getCharge(int daysRented)
{
double result = 1.5;
if (daysRented > 3)
result += (daysRented - 3) * 1.5;
return result;
}
}
}
NewReleaseMovie子类的代码如下:重写租金计算方法(多态性)和积分计算方法(多态性)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
class Program
{
static voidMain(string[] args)
{
Console.Write("影碟店客户租碟明细");
Movie m1 = new Movie("致我们终将逝去的青春", TYPE.NEW_RELEASE);
totalAmount += thisAmount;
}
result += "\n共消费" + totalAmount + "元" + "\n您增加了" + frequentRenterPoints + "个积分\n";
return result;
}
}
}
主程序代码如下:
using System;
using System.Collections.Generic;
}
string getName()
{
return _name;
}
public string statement()
{
double totalAmount = 0; //总共的租金
int frequentRenterPoints = 0;//积分
string result = "\r-------------------------------------------\n\r " +
private string _name;
List<Rental> _rentals = new List<Rental>();
public Customer(string name)
{
_name = name;
}
public void addRental(Rental arg)
{
_rentals.Add(arg);
}
ChildrensMovie子类的代码如下:重写租金计算方法(多态性),积分计算方法从父类继承。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
return daysRented * 3;
Rental r3 = new Rental(m3, 7);
Rental r4 = new Rental(m2, 5);
Rental r5 = new Rental(m3, 3);
Customer c1 = new Customer("孙红雷");
c1.addRental(r1);
c1.addRental(r4);
{
REGULAR,
NEW_RELEASE,
CHILDRENS
}
public class Movie
{
protected string _title; //movie name
TYPE _priceCode; //price code
public Movie()
{
_title = "unname";
_priceCode = 0;
通过
一
都说面向对象的4大支柱是抽象,封装,继承与多态。但是一些初涉编程的开发人员,体会不到继承与多态的妙用,本文就试以一个经典实例来诠释继承与多态的用武之地。
二
1.任务说明
我们的需求是一个影片出租的小应用,该应用会记录每个顾客的消费金额并打印出来。
程序输入为:顾客租的影片及对应的租期;
程序的处理为:根据顾客租用影片时间及影片类型,计算费用;输出:打印消费单。
{
return 1;
}
public TYPE getPriceCode()
{
return (TYPE)_priceCode;
}
void setPriceCode(TYPE arg)
{
_priceCode = arg;
}
public string getTitle()
{
return _title;
}
}
"租碟记录--- " + getName() + "\n";
foreach (Rental iter in _rentals)
{
double thisAmount = 0;
Rental each = iter;
switch (each.getMovie().getTypeCode())
{
case TYPE.REGULAR:
using System.Linq;
usiHale Waihona Puke Baidug System.Text;
namespace _1.cntbed
{
public class NewReleaseMovie:Movie
{
public NewReleaseMovie (string title)
{
_title = title;
}
public override double getCharge(int daysRented)
{
return _movie;
}
}
}
Customer类的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1.cntbed
{
class Customer
{
Console.Write(c4.statement());
}
}
}
四
我们定义一个Movie父类,每种影片类型均定义一个Movie子类(ChildrensMovie,NewReleaseMovie,RegularMovie),同时定义一个Movie工厂类(MovieFactoryMethod)。代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace _1.cntbed
7 {
8 public enum TYPE
9 {
10 REGULAR,
11 NEW_RELEASE,