任何人都可以告訴我,如何在主體中實作我的功能?所有的作業,但我想做一個雇主目錄,那么如何寫雇員到串列或海量?謝謝 !
class Catalog : Employe
{
Employe[] employes = new Employe[10];
Employe p1 = new Employe(14, "Mark", "James", 124151, "Coder", 4000);
public Catalog(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
{
employes[1] = p1;
}
public void CatalogLog()
{
for(int i = 0; i < employes.Length; i )
Console.WriteLine(employes[i]);
}
}
class TestInheritence
{
public static void Main(string[] args)
{
Employe[] employes = new Employe[10];
}
}
uj5u.com熱心網友回復:
我認為您沒有使用正確的邏輯設定繼承層次結構。基類Employee是可擴展的并包含基方法:
public class Employee
{
private int _id;
private string _firstName;
public Employee(int id, string firstName)
{
_id = id;
_firstName = firstName;
}
public int GetID()
{
return _id;
}
public void SetID(int id)
{
if(id > 0)
_id = id;
}
public void Print()
{
Console.WriteLine("ID: {0}\tFirst Name: {1}", this._id, this._firstName);
}
}
派生類允許物件通過向基類的屬性添加新方法和屬性來擴展:
public class Manager : Employee
{
private string _city;
public Manager(int id, string firstName, string city) : base(id, firstName)
{
_city = city;
}
public string GetCity()
{
return _city;
}
}
要測驗這兩個類的作業方式,您可以查看以下應用程式代碼:
public class Program
{
public static void Main()
{
Employee[] employees = new[]
{
new Employee(1, "Thomas"),
new Employee(2, "John"),
new Employee(3, "Erick"),
new Employee(4, "Ahmet"),
new Employee(5, "Sun")
};
employees[0].Print();
Manager manager = new Manager(6, "Johnson", "London");
manager.Print();
Console.WriteLine("City: {0}", manager.GetCity());
}
}
uj5u.com熱心網友回復:
如果您想將員工添加到目錄中,您可以使用“添加”功能將員工添加到目錄中:
class Catalog : Employe
{
List<Employe> employes = new List<Employe>();
public void AddEmployee(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
{
Employe p1 = new Employe(_age, _firstName, _lastName, _id, _job, _salary);
employes.Add(p1);
}
public void CatalogLog()
{
for(int i = 0; i < employes.Count(); i )
Console.WriteLine(employes[i]);
}
}
class TestInheritence
{
public static void Main(string[] args)
{
Catalog catalog = new Catalog();
catalog.AddEmployee(14, "Mark", "James", 124151, "Coder", 4000);
// Add more employees.
}
}
但我認為這是繼承的錯誤用例。通常,當型別之間存在“Is-a”關系時,您希望繼承。但是“目錄”不是“員工”的型別
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/383735.html
上一篇:無法將vsix安裝到VisualStudio2019-Android設備管理器的版本低于VisualStudio的要求
下一篇:Angular和.NetCore專案VS2022:發生未處理的例外:專案“Leon\AppData\Roaming\ASP.NET\https\<projectName>.pem
