3、编写控制台应用程序,设计一个普通职工类Employee,其工资为基本工资(1000)加上工龄工资(每年增加30元)。从Employee类派生一个本科生类UEmployee,其工资为普通职工算法的1.5倍,另从Employee类派生一个研究生类SEmployee,其工资为普通职工算法的2倍,要求计算工资用虚方法实现;分别用相关数据进行测试。
using System;
namespace Test3_3
{
public class Employee
{
private double bsalary = 1000;
private double psalary;
private int n;
public int Pn
{
get { return n; }
set { n = value; }
}
public virtual double ComSalary()
{
Console.Write("工作年数:");
Pn = int.Parse(Console.ReadLine());
psalary = bsalary + 30 * Pn;
return psalary;
}
public class UEmployee : Employee
{
new public double ComSalary()
{
return 1.5 * base.ComSalary();
}
}
public class GEmployee : Employee
{
public override double ComSalary()
{
return 2 * base.ComSalary();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("普通职工:");
Employee emp1 = new Employee();
Console.WriteLine("该普通职工的工资为:{0}", emp1.ComSalary());
Console.WriteLine("本科生职工:");
UEmployee emp2 = new UEmployee();
Console.WriteLine("本科生职工的工资为:{0}", emp2.ComSalary());
//Console.WriteLine("研究生职工:");
GEmployee emp3 = new GEmployee();
Console.WriteLine("研究生职工的工资为:{0}", emp3.ComSalary());
}
}
}
}
运行结果:
4、设计控制台应用程序,假设图书馆的图书类Book包含书名、编号和作者属性,读者类Reader包含姓名和借书证属性,每位读者最多可借5本书,设计它们的公共基类BClass。要求列出所有读者的借书情况,类似下图所示:
using System;
namespace Test3_4
{
public class BClass // 基类
{
private string name; // 名称
private int no; // 编号
public BClass( string na, int n) // 构造函数
{
name = na;
no = n;
}
public void show()
{
Console.Write( "{0}({1})" , name, no);
}
}
public class Book : BClass // 图书类
{
string author; // 作者
public Book( string na, int n, string auth) : base(na, n)
{
author = auth;
}
public void showBook()
{
base.show();
Console .Write( " 作者 :{0}" , author);
}
}
public class Reader : BClass // 读者类
{
Book[] rent; // 所借图书
int top;
public Reader( string na, int n) : base(na, n) // 构造函数
{
rent = new Book[5];
top = 0;
}
public void rentBook( ref Book b)
{
rent[top] = b;
top++;
}
public void showReader()
{
Console.Write( "读者 :" );
base.show();
Console .WriteLine( " 所借图书 :" );
for ( int i = 0; i < top; i++)
{
Console .Write( " {0}:" , i + 1); //5 个空格
rent[i].show();
Console .WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
Book b1 = new Book( "C语言 ", 100, " 潭浩强 ");
Book b2 = new Book( "数据结构 ", 110, "严蔚敏 " );
Book b3 = new Book( "软件工程 ", 210, "陈华 ");
Book b4 = new Book( "操作系统 ", 208, "张明 ");
Reader r1 = new Reader( "王华 ", 1234);
Reader r2 = new Reader( "李兵 ", 2600);
r1.rentBook( ref b1);
r1.rentBook( ref b2);
r1.rentBook( ref b3);
r2.rentBook( ref b4);
r1.showReader();
r2.showReader();
}
}
}
运行结果: