当前位置:文档之家› 通过实例理解继承与多态原理与优点

通过实例理解继承与多态原理与优点

}
string getName()
{
return _name;
}
public string statement()
{
double totalAmountБайду номын сангаас= 0; //总共的租金
int frequentRenterPoints = 0;//积分
string result = "\r-------------------------------------------\n\r " +
{
return 1;
}
public TYPE getPriceCode()
{
return (TYPE)_priceCode;
}
void setPriceCode(TYPE arg)
{
_priceCode = arg;
}
public string getTitle()
{
return _title;
}
}
if (daysRented > 3)
result += (daysRented - 3) * 1.5;
return result;
}
}
}
NewReleaseMovie子类的代码如下:重写租金计算方法(多态性)和积分计算方法(多态性)。
using System;
using System.Collections.Generic;
totalAmount += thisAmount;
}
result += "\n共消费" + totalAmount + "元" + "\n您增加了" + frequentRenterPoints + "个积分\n";
return result;
}
}
}
主程序代码如下:
using System;
using System.Collections.Generic;
}
public Movie(string title, TYPE priceCode)
{
_title = title;
_priceCode = priceCode;
}
public virtual double getCharge(int daysRented)
{
return 0;//收费
}
public virtual int getFrequentRenterPoints(int daysRented)//积分
"租碟记录--- " + getName() + "\n";
foreach (Rental iter in _rentals)
{
double thisAmount = 0;
Rental each = iter;
switch (each.getMovie().getTypeCode())
{
case TYPE.REGULAR:
using System.Linq;
using System.Text;
namespace _tbed
{
public class NewReleaseMovie:Movie
{
public NewReleaseMovie (string title)
{
_title = title;
}
public override double getCharge(int daysRented)
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);
Movie m2 = new Movie("我是特种兵之利刃出鞘", TYPE.REGULAR);
Movie m3 = new Movie("熊出没之环球大冒险", TYPE.CHILDRENS);
Rental r1 = new Rental(m1, 4);
Rental r2 = new Rental(m1, 2);
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace _tbed
7 {
8 public enum TYPE
9 {
10 REGULAR,
11 NEW_RELEASE,
{
public class ChildrensMovie : Movie
{
public ChildrensMovie (string title)
{
_title = title;
}
public override double getCharge(int daysRented)
{
double result = 1.5;
break;
case TYPE.CHILDRENS:
thisAmount += 1.5;//3天之内1.5元
if (each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
frequentRenterPoints++;//对于每一种类型的影片,一次租用积分加1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _tbed
{
class Rental
{
private Movie _movie;
int _daysRented;
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());
using System.Linq;
using System.Text;
namespace _tbed
{
class Program
{
static voidMain(string[] args)
{
Console.Write("影碟店客户租碟明细");
Movie m1 = new Movie("致我们终将逝去的青春", TYPE.NEW_RELEASE);
public Rental(Movie movie, int daysRented)
{
_movie = movie;
_daysRented = daysRented;
}
public int getDaysRented()
{
return _daysRented;
}
public Movie getMovie()
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对象,以及一个租期成员(积分和租金与此有关):
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;
if ((each.getMovie().getTypeCode() == TYPE.NEW_RELEASE) &&
each.getDaysRented() > 1)
frequentRenterPoints++;
result += "\n\t" + each.getMovie().getTitle() + "\t" + thisAmount;
新的Movie类的代码如下:Movie类提供积分计算和租金计算的默认实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _tbed
{
public enum TYPE
相关主题