我需要创建一个类Person
,有字段:
name
,surname
和salary
.如果salary
低于0,我得到例外:
ArgumentOutOfRangeException.使用getter,setter
我尝试过:
public class Employee
{
public string name { get; set; }
string surname { get; set; }
private int salary;
public int Salary
{
get
{
return salary;
}
set
{
if (salary < 0)
{
throw new ArgumentOutOfRangeException("salary", "wyplata ma byc wieksza niz 0");
}
else
{
salary = value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在主要:
Employee tmp = new Employee("michal", "jakowski", -1400);
Run Code Online (Sandbox Code Playgroud)