簡體   English   中英

構造函數屬性

[英]constructor properties

我想計算 HRA、DA 等。我必須定義一個構造函數,但彈出錯誤,指出變量 `basic_salary' 已分配,但它的值從未使用過。

代碼:

public void read()
{
    Console.WriteLine("Enter the employee name");
    empname = Console.ReadLine();
    Console.WriteLine("Enter the basic salary of an employee");
    int basic_salary = Convert.ToDouble(Console.ReadLine());
    calculate();
}

class program
{
public static void Main(string[] args)
{
    Employee employeobj = new Employee();
    employeobj.read();
    employeobj.display();
}
}

read() function 中,您再次定義basic_salary變量,而不是使用在basic_salary級別定義的 basic_salary 。

要解決您的問題,請嘗試

public void read()
{
    Console.WriteLine("Enter the employee name");
    empname = Console.ReadLine();
    Console.WriteLine("Enter the basic salary of an employee");
    //insted of defining new integer as basic_salary, use existing class level basic_salary variable
    basic_salary = Convert.ToInt32(Console.ReadLine()); 
    calculate();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM