(1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 简单雇员类
{
class Employee
{
public string id;
public string name;
public Employee() { }
public Employee(string name,string id) {
this.name = name;
this.id = id;
}
//用于存储雇员数
//静态方法只能访问类中的静态成员,非静态方法可以访问类中的包括静态方法在内的所有成员。
public static int employeeCounter;
public static int AddEmployee() {
return ++employeeCounter;
}
//派生类
class MainClass : Employee {
public static void Main() {
Console.Write("输入雇员姓名:");
string name = Console.ReadLine();
Console.Write("输入雇员的id:");
string id = Console.ReadLine();
//建立雇员对象
Employee e = new Employee(name,id);
Console.Write("输入雇员数:");
string n = Console.ReadLine();
//Int32.Parse(n);转换成32位的有符号整数。employeeCounter是静态,所以可以直接用类调用。
Employee.employeeCounter = Int32.Parse(n);
//增加雇员数量
Employee.AddEmployee();
//显示新信息
Console.WriteLine("Name:{0}", e.name);
Console.WriteLine("ID:{0}",e.id);
Console.WriteLine("新的雇员数为:{0}",Employee.employeeCounter);
}
}
}
}
(2)