2015-11-18 131 views
0

我有一個簡單的程序與僱員類,我有這個類lyndaalex 2個對象,我還創建了一個方法稱爲CalculateTotalPay()打印總薪酬控制檯後簡單添加salarybonus。現在的打印到控制檯我有Console.WriteLine("Total Pay = " + totalPay);,但我想使它成爲 Console.WriteLine("Total Pay for " + employeeName " = " + totalPay);但我不想反覆刁難新員工到新變量,什麼是最好的方式來做到這一點?分配對象到變量

這裏是代碼

class Employee 
{ 
    public double salary; 
    public double bonus; 
    public string employeeName; 

    public void CalculateTotalPay() 
    { 
     double totalPay = salary + bonus; 
     Console.WriteLine("Total Pay = " + totalPay); 
    } 
} 

static void Main() 
{ 
    Employee alex = new Employee(); 
    Employee lynda = new Employee(); 

    alex.salary = 900000; 
    alex.bonus = 20000; 
    alex.CalculateTotalPay(); 

    lynda.salary = 100000; 
    lynda.bonus = 20000; 
    lynda.CalculateTotalPay(); 
} 
+3

如果你不想分配一個變量,你認爲信息應該來自哪裏?你知道你可以再次使用相同的變量,對嗎? –

+0

如果你想要不同的員工,你必須以這樣或那樣的方式分配他們。那是不可避免的。例如,可以將它自動化一下,例如將對象數據讀寫到文件或數據庫。 – Adimeus

+0

從構造函數中獲取名稱。因爲您將任何名稱分配給對象。目前他們只是變量名稱。 –

回答

0

您可以使用List<Employee> workers = new List<Employee>();。 添加新員工很簡單 - workers.Add(new Employee {salary = 10, bonus = 100});。 終於可以interate在此collection:

foreach (var item in workers) 
{ 
    //TODO 
} 
2

首先,你的類應該是這樣的:

class Employee 
{ 
    public double Salary { get; set; } 
    public double Bonus { get; set; } 
    public string Name { get; set; } 

    public Employee(String name, double salary, double bonus) 
    { 
     Salary = salary; 
     Bonus = bonus; 
     Name = name; 
    } 

    public void CalculateTotalPay() 
    { 
     double totalPay = Salary + Bonus; 
     Console.WriteLine("Total Pay for {0} = {1}", Name, totalPay); 
    } 
} 

現在,你可以簡單地使用構造:

Employee alex = new Employee("alex", 900000, 20000); 
Employee lynda = new Employee("lynda", 100000, 20000); 

編輯:顯然你的問題有點不同。 如果您沒有外部信息來源,則必須在代碼中定義所有員工。我建議是這樣的:

List<Employee> Employees = new List<Employee> 
{ 
    new Employee("alex", 900000, 20000), 
    new Employee("lynda", 100000, 20000) 
}; 

foreach (var employee in Employees) { 
     employee.CalculateTotalPay(); 
} 

出於可讀性起見,做這種方式,並且不會開始進行類似的獎金,工資和3個獨立陣列。

+1

每當調用'Employee'構造函數時,您仍然需要一個新對象。 –

+0

我們可以將名稱,薪資,獎金屬性設爲私人套餐嗎?他們不應該從課外改變。肯定這個名字不應該從課外改變。 – mans

0

我認爲你正在尋找下面的解決方案。

class Program 
{ 
    static void Main(string[] args) 
    { 
     IEnumerable<Employee> employees = new List<Employee> 
     { 
      new Employee("Alex", 900000, 20000), 
      new Employee("Lynda", 100000, 20000) 
     }; 

     foreach (var employee in employees) 
     { 
      employee.CalculatePay(); 
     } 

     Console.ReadKey(); 
    } 
} 

public class Employee 
{ 
    private readonly double _salary; 
    private readonly double _bonus; 
    private readonly string _name; 

    public double Salary { get { return _salary; } } 
    public double Bonus { get { return _bonus; } } 
    public string Name { get { return _name; } } 

    public Employee(string name, double salary, double bonus) 
    { 
     _name = name; 
     _salary = salary; 
     _bonus = bonus; 
    } 

    public void CalculatePay() 
    { 
     double totalPay = Salary + Bonus; 
     Console.WriteLine("Total Pay for Employee {0} = {1}", Name, totalPay); 
    } 
}