类代码:

        interface Employee
{
void ShowEmp();
} abstract class EmployeeInPostion: Employee
{
public abstract void ShowEmp();
} class EmployeeInPass: Employee
{
public virtual void ShowEmp()
{
Console.WriteLine("员工:gongyg");
ShowEmpAge();
}
public virtual void ShowEmpAge()
{
Console.WriteLine("年龄:27");
} public void ShowEmpSex()
{
Console.WriteLine("性别:男");
}
} class EmployeeInPassHandler : EmployeeInPass
{
public override void ShowEmp()
{
Console.WriteLine("员工:gumd");
ShowEmpAge();
ShowEmpSex();
}
public override void ShowEmpAge()
{
Console.WriteLine("年龄:29");
}
}

执行代码:

            Employee emp = new EmployeeInPass();
emp.ShowEmp();//员工:gongyg 年龄:27 Employee emp1 = new EmployeeInPassHandler();
emp1.ShowEmp();//员工:gumd 年龄:29 性别:男 EmployeeInPass empInPass = new EmployeeInPass();
empInPass.ShowEmp();//员工:gongyg 年龄:27 EmployeeInPass empInpass1 = new EmployeeInPassHandler();
empInpass1.ShowEmp();//员工:gumd 年龄:29 性别:男 EmployeeInPassHandler empHandler = new EmployeeInPassHandler();
empHandler.ShowEmp();//员工:gumd 年龄:29 性别:男
empHandler.ShowEmpAge();//年龄:29

注意:

1、接口和抽象类都不能被实例化

2、重写、覆盖(override)方法  父类方法中一定要有 virtual(虚方法)

05-11 17:53