列出从一个 class 添加的项目无法从另一个 class 访问
List items added from one class not accessible from another class
我正在编写一个程序,它使用列表来存储员工数据,遵循 SOLID 原则,我尝试将尽可能多的功能从主要 class 移到他们自己的 classes 中。我有一个存储库 class,它列出了初始列表值,并包含读取和创建访问和编辑列表的方法。我的问题是我的新 class 无法访问任何随后添加到列表中的内容,并且只能在启动时看到内部数据。
这是我的回购 Class:
public class Repository
{
private List<EmployeeData> myEmployeeData = new List<EmployeeData>()
{
new EmployeeData()
{
EmployeeID = 1,
FName = "Joe",
LName = "Bloggs",
isPermanent = true,
Salaryint = 40000,
Bonusint = 5000,
DayRateint = null,
WeeksWorkedint = null
},
new EmployeeData()
{
EmployeeID = 2,
FName = "John",
LName = "Smith",
isPermanent = true,
Salaryint = 45000,
Bonusint = 2500,
DayRateint = null,
WeeksWorkedint = null
},
new EmployeeData()
{
EmployeeID = 3,
FName = "Clare",
LName = "Jones",
isPermanent = false,
Salaryint = null,
Bonusint = null,
DayRateint = 350,
WeeksWorkedint = 40
}
};
public EmployeeData Create(int IDcount, string fname, string lname, bool isPerm, int? Salary, int? Bonus, int? DayRate, int? WeeksWorked)
{
var createEmployee = new EmployeeData()
{
EmployeeID = IDcount,
FName = fname,
LName = lname,
isPermanent = isPerm,
Salaryint = Salary,
Bonusint = Bonus,
DayRateint = DayRate,
WeeksWorkedint = WeeksWorked
};
myEmployeeData.Add(createEmployee);
return createEmployee;
}
public IEnumerable<EmployeeData> ReadAll()
{
return (myEmployeeData);
}
public EmployeeData Read(int employeeID)
{
return myEmployeeData[employeeID];
}
public EmployeeData Update(int employeeID, string fname, string lname, bool isPerm, int? Salary, int? Bonus, int? DayRate, int? WeeksWorked)
{
var x = Read(employeeID);
x.FName = fname;
x.LName = lname;
x.isPermanent = isPerm;
x.Salaryint = Salary;
x.Bonusint = Bonus;
x.DayRateint = DayRate;
x.WeeksWorkedint = WeeksWorked;
return x;
}
public bool Delete(int employeeID)
{
myEmployeeData.RemoveAt(employeeID);
return true;
}
}
这是我的新 classes,它无法访问新创建的项目:
public class Calculator : Repository
{
public double AnnualPayAfterTax;
public double AnnualPay;
public double CalculateEmployeePay(int employeeID)
{
bool EmploymentStatus = Read(employeeID).isPermanent;
if (EmploymentStatus == true)
{
int Salary = (int)Read(employeeID).Salaryint;
int Bonus = (int)Read(employeeID).Bonusint;
AnnualPay = Salary + Bonus;
}
else {
int DayRate = (int)Read(employeeID).DayRateint;
int WeeksWorked = (int)Read(employeeID).WeeksWorkedint;
AnnualPay = (DayRate * 5) + WeeksWorked;
}
if (AnnualPay < 12570) { AnnualPayAfterTax = AnnualPay; }
if (AnnualPay > 12570) { AnnualPayAfterTax = (AnnualPay - 12570) * 0.2; }
return (AnnualPayAfterTax);
}
}
这是我的主要方法的一部分,它调用了两个 classes,注意 read() 在这个 class 中对于新添加的列表项工作正常。
bool CalLoop = false;
while (CalLoop == false)
{
Console.Clear();
Console.WriteLine("CALCULATE ANNUAL PAY\n");
Console.WriteLine(string.Concat(re.ReadAll()));
Console.Write("\nSelect ID of Employee: ");
string Input = Console.ReadLine();
bool valid = int.TryParse(Input, out Output);
if (valid)
{
int selectedID = Output;
selectedID = selectedID - 1;
bool CheckID = (selectedID <= IDcount);
if (CheckID)
{
Console.WriteLine("Employee Name: " + re.Read(selectedID).FName + " " + re.Read(selectedID).LName);
Console.WriteLine("Employment Type: " + re.Read(selectedID).isPermanent);
Console.WriteLine("Annual Pay after Tax: £" + cal.CalculateEmployeePay(selectedID));
}
else
{
Console.WriteLine("Invaild ID.");
}
}
else
{
Console.WriteLine("Invaild ID.");
}
CalLoop = true;
}
为什么我的新 class 看不到新添加到列表中的任何内容?我认为列表在整个运行时动态更新?我错过了什么吗?
感谢任何帮助或意见,提前致谢!
private List<EmployeeData> myEmployeeData = new List<EmployeeData>()
是 private
... 将其更改为 protected
以便继承 类 可以访问它。
你有两个 classes :
public class Repository
{
public List<EmployeeData> myEmployeeData = new List<EmployeeData>();
}
public class Calculator : Repository
{ }
当这两个 class 实例化时,您可以看到它不共享相同的列表:
var rep = new Repository();
var cal = new Calculator();
if(object.ReferenceEquals(rep.myEmployeeData, cal.myEmployeeData))
{
Console.WriteLine("Repository and Calucaltor share the same liste.")
}
else
{
Console.WriteLine("Repository and Calucaltor don't share the same liste.")
}
一个class就像一个盖楼的计划。
例如,计划“BaseBuilding”有一个接待处和一个洗手间。
还有一个计划“OfficeBuilding”,它从计划“BaseBuilding”扩展(继承)并添加一些办公室工作
接下来,您将按照“BaseBuilding”计划建造一栋建筑,并按照“OfficeBuilding”计划建造另一栋建筑。
在这种情况下,您建造了两个接待处和洗手间(一个在大楼旁边)。
实例化Repository
和Calculator
也是一样的。
这将创建两个不同的对象,每个对象都有自己的列表。
有关详细信息,请参阅有关 inheritance;
的官方文档
但这是真正的问题。当 class A 继承自 class B 时,您可以用 A 替换 B。
在你的例子中,Calculator
继承自 Repository
,那么你可以用 Calculator
.
替换 Repository
I use the repository to read employee's informations.
I use the calculator to read employee's informations.
第一句很清楚,但是第二句……听起来很奇怪。
这是因为这打破了第三个 SOLID 原则(Liskov 替换)。
还有为什么计算器可以读取员工信息?
这是存储库检索员工信息的责任。
计算器应该只做计算。
这违反了第一个 SOLID 原则(单一职责)。
如果您想遵循 SOLID 原则,那么我建议您 to avoid inheritance and prefer the composition。
你的情况:
class Calculator
{
private readonly Repository _repository;
public Calculator(Repository repository)
{
_repository = repository;
}
public double CalculateEmployeePay(int employeeID)
{
bool EmploymentStatus = _repository.Read(employeeID).isPermanent;
...
}
}
所以计算器只计算,存储库只管理员工。
我正在编写一个程序,它使用列表来存储员工数据,遵循 SOLID 原则,我尝试将尽可能多的功能从主要 class 移到他们自己的 classes 中。我有一个存储库 class,它列出了初始列表值,并包含读取和创建访问和编辑列表的方法。我的问题是我的新 class 无法访问任何随后添加到列表中的内容,并且只能在启动时看到内部数据。
这是我的回购 Class:
public class Repository
{
private List<EmployeeData> myEmployeeData = new List<EmployeeData>()
{
new EmployeeData()
{
EmployeeID = 1,
FName = "Joe",
LName = "Bloggs",
isPermanent = true,
Salaryint = 40000,
Bonusint = 5000,
DayRateint = null,
WeeksWorkedint = null
},
new EmployeeData()
{
EmployeeID = 2,
FName = "John",
LName = "Smith",
isPermanent = true,
Salaryint = 45000,
Bonusint = 2500,
DayRateint = null,
WeeksWorkedint = null
},
new EmployeeData()
{
EmployeeID = 3,
FName = "Clare",
LName = "Jones",
isPermanent = false,
Salaryint = null,
Bonusint = null,
DayRateint = 350,
WeeksWorkedint = 40
}
};
public EmployeeData Create(int IDcount, string fname, string lname, bool isPerm, int? Salary, int? Bonus, int? DayRate, int? WeeksWorked)
{
var createEmployee = new EmployeeData()
{
EmployeeID = IDcount,
FName = fname,
LName = lname,
isPermanent = isPerm,
Salaryint = Salary,
Bonusint = Bonus,
DayRateint = DayRate,
WeeksWorkedint = WeeksWorked
};
myEmployeeData.Add(createEmployee);
return createEmployee;
}
public IEnumerable<EmployeeData> ReadAll()
{
return (myEmployeeData);
}
public EmployeeData Read(int employeeID)
{
return myEmployeeData[employeeID];
}
public EmployeeData Update(int employeeID, string fname, string lname, bool isPerm, int? Salary, int? Bonus, int? DayRate, int? WeeksWorked)
{
var x = Read(employeeID);
x.FName = fname;
x.LName = lname;
x.isPermanent = isPerm;
x.Salaryint = Salary;
x.Bonusint = Bonus;
x.DayRateint = DayRate;
x.WeeksWorkedint = WeeksWorked;
return x;
}
public bool Delete(int employeeID)
{
myEmployeeData.RemoveAt(employeeID);
return true;
}
}
这是我的新 classes,它无法访问新创建的项目:
public class Calculator : Repository
{
public double AnnualPayAfterTax;
public double AnnualPay;
public double CalculateEmployeePay(int employeeID)
{
bool EmploymentStatus = Read(employeeID).isPermanent;
if (EmploymentStatus == true)
{
int Salary = (int)Read(employeeID).Salaryint;
int Bonus = (int)Read(employeeID).Bonusint;
AnnualPay = Salary + Bonus;
}
else {
int DayRate = (int)Read(employeeID).DayRateint;
int WeeksWorked = (int)Read(employeeID).WeeksWorkedint;
AnnualPay = (DayRate * 5) + WeeksWorked;
}
if (AnnualPay < 12570) { AnnualPayAfterTax = AnnualPay; }
if (AnnualPay > 12570) { AnnualPayAfterTax = (AnnualPay - 12570) * 0.2; }
return (AnnualPayAfterTax);
}
}
这是我的主要方法的一部分,它调用了两个 classes,注意 read() 在这个 class 中对于新添加的列表项工作正常。
bool CalLoop = false;
while (CalLoop == false)
{
Console.Clear();
Console.WriteLine("CALCULATE ANNUAL PAY\n");
Console.WriteLine(string.Concat(re.ReadAll()));
Console.Write("\nSelect ID of Employee: ");
string Input = Console.ReadLine();
bool valid = int.TryParse(Input, out Output);
if (valid)
{
int selectedID = Output;
selectedID = selectedID - 1;
bool CheckID = (selectedID <= IDcount);
if (CheckID)
{
Console.WriteLine("Employee Name: " + re.Read(selectedID).FName + " " + re.Read(selectedID).LName);
Console.WriteLine("Employment Type: " + re.Read(selectedID).isPermanent);
Console.WriteLine("Annual Pay after Tax: £" + cal.CalculateEmployeePay(selectedID));
}
else
{
Console.WriteLine("Invaild ID.");
}
}
else
{
Console.WriteLine("Invaild ID.");
}
CalLoop = true;
}
为什么我的新 class 看不到新添加到列表中的任何内容?我认为列表在整个运行时动态更新?我错过了什么吗?
感谢任何帮助或意见,提前致谢!
private List<EmployeeData> myEmployeeData = new List<EmployeeData>()
是 private
... 将其更改为 protected
以便继承 类 可以访问它。
你有两个 classes :
public class Repository
{
public List<EmployeeData> myEmployeeData = new List<EmployeeData>();
}
public class Calculator : Repository
{ }
当这两个 class 实例化时,您可以看到它不共享相同的列表:
var rep = new Repository();
var cal = new Calculator();
if(object.ReferenceEquals(rep.myEmployeeData, cal.myEmployeeData))
{
Console.WriteLine("Repository and Calucaltor share the same liste.")
}
else
{
Console.WriteLine("Repository and Calucaltor don't share the same liste.")
}
一个class就像一个盖楼的计划。 例如,计划“BaseBuilding”有一个接待处和一个洗手间。 还有一个计划“OfficeBuilding”,它从计划“BaseBuilding”扩展(继承)并添加一些办公室工作
接下来,您将按照“BaseBuilding”计划建造一栋建筑,并按照“OfficeBuilding”计划建造另一栋建筑。 在这种情况下,您建造了两个接待处和洗手间(一个在大楼旁边)。
实例化Repository
和Calculator
也是一样的。
这将创建两个不同的对象,每个对象都有自己的列表。
有关详细信息,请参阅有关 inheritance;
但这是真正的问题。当 class A 继承自 class B 时,您可以用 A 替换 B。
在你的例子中,Calculator
继承自 Repository
,那么你可以用 Calculator
.
Repository
I use the repository to read employee's informations.
I use the calculator to read employee's informations.
第一句很清楚,但是第二句……听起来很奇怪。 这是因为这打破了第三个 SOLID 原则(Liskov 替换)。
还有为什么计算器可以读取员工信息? 这是存储库检索员工信息的责任。 计算器应该只做计算。 这违反了第一个 SOLID 原则(单一职责)。
如果您想遵循 SOLID 原则,那么我建议您 to avoid inheritance and prefer the composition。
你的情况:
class Calculator
{
private readonly Repository _repository;
public Calculator(Repository repository)
{
_repository = repository;
}
public double CalculateEmployeePay(int employeeID)
{
bool EmploymentStatus = _repository.Read(employeeID).isPermanent;
...
}
}
所以计算器只计算,存储库只管理员工。