⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipleinher.cs

📁 北大青鸟-Cs学生用书-教师用书实例源代码.rar
💻 CS
字号:
using System;

namespace Example_4
{

	//IPromotable 接口,晋升到新工作岗位
	interface IPromotable
	{
		void Promote();
	}

	//IGoodStudent 接口,出色的学生升一级
	interface IGoodStudent
	{
		void Promote();
	}

	/// <summary>
	/// Employee 类的摘要说明。
	/// </summary>
	//Employee 类表示某公司的一名职员。
	//请注意,该类没有抽象方法,但仍应声明为抽象类,
	//因为 Employee 是个抽象概念。
	abstract class Employee
	{
		//必要时,可以允许派生类访问受保护字段
		protected string _name;
		protected double _salary;
		//限制受保护构造函数对派生类的访问
		protected Employee(string name, double salary)
		{
			this._name   = name;
			this._salary = salary;
		}
	
		//将 Print 方法声明为 virtual,以进行动态绑定
		public virtual void Print()
		{
			Console.WriteLine("姓名:{0}", _name);
			Console.WriteLine("薪水:{0}", _salary);		
		}
	}

	//Programmer 类表示公司的一名计算机程序员,程序员可以在公司内获得晋升
	class Programmer : Employee, IPromotable
	{
		protected double _averageOT;

		public Programmer(string name, double salary, double averageOT):base(name, salary)
		{
			this._averageOT = averageOT;
		}

		//从 IPromotable 接口实现 Promote 方法
		public void Promote()
		{
			Console.WriteLine("薪水:{0}", _salary *= 1.6);
		}

		public override void Print()
		{
			Console.WriteLine("程序员");
			base.Print();
			Console.WriteLine("平均加班时间:{0}", _averageOT);
		}
	}

	//Manager 类表示一位公司经理,经理可以在公司内获得晋升。所有经理都需要继续深造,以实现 IGoodStudent
	class Manager : Employee, IPromotable, IGoodStudent
	{
		protected string _secretaryName;

		public Manager(string name, double salary, string secretaryName): base(name, salary)
		{
			this._secretaryName = secretaryName;
		}
	
		//从 IPromotable 接口实现 Promote 方法
		void IPromotable.Promote()
		{
			Console.WriteLine("薪水:{0}", _salary *= 1.9);
		}
	
		//从 IGoodStudent 接口实现 Promote 方法
		void IGoodStudent.Promote()
		{
			Console.WriteLine("经理是优等生,所以获得晋升");
		}

		public override void Print()
		{
			Console.WriteLine("经理");
			base.Print();
			Console.WriteLine("秘书:{0}", _secretaryName);		
		}
	}

	//Intern 类表示公司的一名实习生,实习生是临时雇员,因此不符合晋升的条件
	class Intern : Employee
	{
		protected int _periodOfInternship;
	
		public Intern(string name, double salary, int periodOfInternship):base(name, salary)
		{
			this._periodOfInternship = periodOfInternship;
		}
	
		public override void Print()
		{
			Console.WriteLine("实习生");
			base.Print();
			Console.WriteLine("实习期(月):{0}", _periodOfInternship);
		}
	}

	class InterfacesTest
	{
		static void Main()
		{
			
			Intern objIEmployee = new Intern("David",5500,2);
			objIEmployee.Print();
			Console.WriteLine();

			Programmer objPEmployee = new Programmer("Michael", 45000, 4);
			objPEmployee.Print();
			IPromotable Promotable1 = objPEmployee;
			Promotable1.Promote();
			Console.WriteLine();

			Manager objMEmployee = new Manager("John", 120000, "Smith");
			objMEmployee.Print();
			IPromotable Promotable2 = objMEmployee;
			IGoodStudent GoodStudent = objMEmployee;
			Promotable2.Promote();
			GoodStudent.Promote();		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -