using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class Employee
{
private string name;
private int empid;
BenefitPackage _BenefitPackage = new BenefitPackage();
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public int EmpId
{
get { return this.empid; }
set
{
if (value == 1)
return;
this.empid = value; }
}
public Employee(string Name, int EmpId)
{
this.Name = Name;
this.EmpId = EmpId;
}
public Employee()
{ }
public abstract void GiveBonus();
}
public class Manager : Employee
{
private int noofstockoptions;
public override void GiveBonus()
{
Console.WriteLine("Manger GiveBonus Override");
}
public int NoOfStockOptions
{
get { return this.noofstockoptions; }
set { this.noofstockoptions = value; }
}
public Manager(string Name,int EmpId, int NoOfStockOptions):base(Name,EmpId)
{
this.NoOfStockOptions=NoOfStockOptions;
}
}
public class SalesPerson:Employee
{
private int noofsales;
public int NoOfSales
{
get { return this.noofsales; }
set { this.noofsales = value; }
}
public SalesPerson(string Name, int EmpId, int NoOfSales):base(Name,EmpId)
{
this.NoOfSales = NoOfSales;
}
public override void GiveBonus()
{
Console.WriteLine("Hi from salesperson");
}
}
public sealed class PTSalesPerson : SalesPerson
{
private int noofhrworked;
public int NoOfHrWorked
{
get { return this.noofhrworked; }
set { this.noofhrworked = value; }
}
public PTSalesPerson(string Name, int EmpId, int NoOfSales,int NoOfHrWorked):base(Name,EmpId,NoOfSales)
{
this.NoOfHrWorked = NoOfHrWorked;
}
//public new void GiveBonus()
//{
// Console.WriteLine("hi from ptsalesperson");
//}
}
class BenefitPackage
{
public int Bonus;
public int GiveBonus()
{
int i = 200;
return i;
}
private class innerPublic
{
public int innerBonus;
}
}
class MainClass
{
public static void Main()
{
Manager _Manager=new Manager("Vaibhav",1,50);
PTSalesPerson _PTSalesPerson = new PTSalesPerson("Shantanu", 1, 4, 6);
_Manager.GiveBonus();
Employee _emp;
//_emp = new Employee("new emp",4);
//_emp.GiveBonus();
_PTSalesPerson.GiveBonus();
((SalesPerson)_PTSalesPerson).GiveBonus();
Console.ReadLine();
}
}
}
Пожалуйста, не пытайтесь понять весь код. Я резюмирую его.
Сотрудник — это абстрактный класс, который имеет абстрактный метод GiveBonus.
SalesPerson является производным от класса «Сотрудник». SalesPerson должен дать определение абстрактному методу GiveBonus.(SalesPerson не может быть абстрактным)
PTSalesPerson является производным от SalesPerson.
Теперь мой вопрос: как я могу заставить PTSalesPerson иметь собственную реализацию GiveBonus.
[b]Заголовок вопроса кажется немного запутанным, но я постараюсь прояснить здесь свой вопрос.[/b]
[code]using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication1 { public abstract class Employee { private string name; private int empid; BenefitPackage _BenefitPackage = new BenefitPackage(); public string Name { get { return this.name; } set { this.name = value; } } public int EmpId { get { return this.empid; } set { if (value == 1) return; this.empid = value; } } public Employee(string Name, int EmpId) { this.Name = Name; this.EmpId = EmpId; } public Employee() { }
public abstract void GiveBonus();
}
public class Manager : Employee { private int noofstockoptions; public override void GiveBonus() { Console.WriteLine("Manger GiveBonus Override"); } public int NoOfStockOptions { get { return this.noofstockoptions; } set { this.noofstockoptions = value; } }
public Manager(string Name,int EmpId, int NoOfStockOptions):base(Name,EmpId) { this.NoOfStockOptions=NoOfStockOptions; }
} public class SalesPerson:Employee { private int noofsales; public int NoOfSales { get { return this.noofsales; } set { this.noofsales = value; } }
public SalesPerson(string Name, int EmpId, int NoOfSales):base(Name,EmpId) { this.NoOfSales = NoOfSales; } public override void GiveBonus() { Console.WriteLine("Hi from salesperson"); } } public sealed class PTSalesPerson : SalesPerson { private int noofhrworked; public int NoOfHrWorked { get { return this.noofhrworked; } set { this.noofhrworked = value; }
} public PTSalesPerson(string Name, int EmpId, int NoOfSales,int NoOfHrWorked):base(Name,EmpId,NoOfSales) { this.NoOfHrWorked = NoOfHrWorked;
} //public new void GiveBonus() //{ // Console.WriteLine("hi from ptsalesperson"); //} }
class BenefitPackage { public int Bonus; public int GiveBonus() { int i = 200; return i; }
private class innerPublic { public int innerBonus;
}
}
class MainClass { public static void Main() { Manager _Manager=new Manager("Vaibhav",1,50); PTSalesPerson _PTSalesPerson = new PTSalesPerson("Shantanu", 1, 4, 6); _Manager.GiveBonus();
[b]Пожалуйста, не пытайтесь понять весь код.[/b] Я резюмирую его.
[list] [*]Сотрудник — это абстрактный класс, который имеет абстрактный метод GiveBonus.
[*]SalesPerson является производным от класса «Сотрудник». SalesPerson должен дать определение абстрактному методу GiveBonus.([b]SalesPerson не может быть абстрактным[/b])
[*]PTSalesPerson является производным от SalesPerson.[/list]
[b]Теперь мой вопрос: как я могу заставить PTSalesPerson иметь собственную реализацию GiveBonus.[/b]
Интересно, могу ли я инициализировать последнее поле в абстрактном базовом классе результатом абстрактного метода, который будет реализован производным классом. Я понимаю, что производный ctor сначала вызовет базовый ctor, поэтому я гарантирую, что...