一,源代码
1程序设计题 1:已知相册类为 Album,照片类为 Photo,请使用索引器 设计容量为 5 的相册存放照片,需要实现索引器的读/写功能;
using System
namespace test3
{
class Photo
{
string _title;
public Photo(string title)
{
this._title = title;
}
public string Title
{
get
{
return _title;
}
}
}
class Album
{
// 该数组用于存放照片
Photo[] photos;
public Album(int capacity)
{
photos = new Photo[capacity];
}
public Photo this[int index]
{
get
{
// 验证索引范围
if (index < 0 || index >= photos.Length)
{
Console.WriteLine(“索引无效”);
// 使用 null 指示失败
return null;
}
// 对于有效索引,返回请求的照片
return photos[index];
}
set
{
if (index < 0 || index >= photos.Length)
{
Console.WriteLine(“索引无效”);
return;
}
photos[index] = value;
}
}
class Program
{
static void Main(string[] args)
{
// 创建一个容量为 5 的相册
Album family = new Album(5);
// 创建 5 张照片
Photo first = new Photo(“Jeny”);
Photo second = new Photo(“Smith”);
Photo third = new Photo(“Lono”);
Photo four = new Photo(“John”);
Photo five = new Photo(“Mike”);
// 向相册加载照片
family[0] = first;
family[1] = second;
family[2] = third;
family[3] = four;
family[4] = five;
// 按索引检索
Photo objPhoto1 = family[2];
Console.WriteLine(objPhoto1.Title);
// 按名称检索
Photo objPhoto2 = family[4];
Console.WriteLine(objPhoto2.Title);
}
}
}
}
2程序设计题 2:已知 person 类下包含教师和学生两个子类,请使用接 口实现收作业功能;
using System;
namespace Pro
{
public class Person
{ }
public interface IHomeworkCollector
{
void CollectHomework();
}
public class Student : Person, IHomeworkCollector
{
public void CollectHomework()
{
Console.WriteLine("报告老师!作业收集完毕!");
}
}
public class Teacher :Person,IHomeworkCollector
{
public void CollectHomework()
{
Console.WriteLine("同学们,请交作业!");
}
}
}
class Program
{
private static void DoCollectHomework(IHomeworkCollector homeworkC)
{
homeworkC.CollectHomework();
}
static void Main(string[] args)
{
IHomeworkCollector s = new Teacher();
DoCollectHomework(s);
s =new Student();
DoCollectHomework(s);
}
}
3程序设计题 3:请使用事件和委托实现订阅推送功能。
using System;
using System.Collections.Generic;
namespace card_work
{
class depositCard
{
//储蓄名
private string name;
//储蓄金额
private int depositMoney;
//基础储蓄金额
public depositCard(string name, int money)
{
this.name = name;
this.depositMoney = money;
}
//获取储蓄金额
public int getDepositMoney()
{
return this.depositMoney;
}
public void setDepositMoney(int r)
{
this.depositMoney = r;
}
}
//信用卡类
class creditCard
{
//信用卡持有者名称
public string name;
//信用卡余额
public int creditMoney;
//扣款日期
public int dueDay;
//绑定储蓄卡对象
public depositCard DC;
//初始化
public creditCard(string name, int CM, int DD, depositCard DC)
{
this.name = name;
this.creditMoney = CM;
this.dueDay = DD;
this.DC = DC;
}
//还款
public void repayM()
{
Console.WriteLine(“用户{0}当前金额{1}”, this.name, this.DC.getDepositMoney());
DC.setDepositMoney(this.DC.getDepositMoney() + creditMoney);
Console.WriteLine(“用户已还款”);
Console.WriteLine(“还款金额:{0}”, Math.Abs(this.creditMoney));
Console.WriteLine(“余下金额为{0}”, DC.getDepositMoney());
Console.WriteLine();
}
//无需还款
public void norepayM()
{
Console.WriteLine(“用户{0}当前金额{1}”, this.nane,this.DC.getDepositMoney());
Console.WriteLine(“用户未到还款日期,无需还款”, this.name);
Console.WriteLine();
}
}
//扣款委托类
class repayDelegate
{
//扣款委托
public delegate void repayMoney();
//扣款事件
public event repayMoney DoRepay;
//事件执行
public void NotifyRepay()
{
if (DoRepay != null)
{
Console.WriteLine(“触发事件:”);
DoRepay();
}
}
}
class Program
{
static void Main(string[] args)
{
depositCard D1 = new depositCard(“张三”, 10000);
depositCard D2 = new depositCard(“李四”, 10000);
depositCard D3 = new depositCard(“王五”, 10000);
creditCard C1 = new creditCard(“张三”, -5000, 5, D1);
creditCard C2 = new creditCard(“李四”, -3000, 31, D2);
creditCard C3 = new creditCard(“王五”, -1000, 5, D3);
List cCards = new List();
cCards.Add(C1);
cCards.Add(C2);
cCards.Add(C3);
//创建委托对象
repayDelegate rD = new repayDelegate();
foreach (creditCard C in cCards)
{
//判断是否到了该还款的日期
if (C.dueDay == int.Parse(DateTime.Now.ToString("yyyy-MM-dd").Split('-')[2]))
{
//事件添加
rD.DoRepay += new repayDelegate.repayMoney(C.repayM);
}
else
{
rD.DoRepay += new repayDelegate.repayMoney(C.norepayM);
}
}
//事件执行
rD.NotifyRepay();
}
}
}
二 ,运行结果
三,学习重点
1、掌握C#面向对象设计语法;
2、综合运用C#面向对象的抽象、继承、多态等特性以及委托与事件。
学习心得
索引器是类的特殊成员,它可以根据索引在多个成员中进行选择,能够让对象以类似数组的方式来存取。通常情况下,属性只能访问单一的字段(一个属性对一个字段进行封装),如果想访问多个数据成员,就需要使用索引器。