【发布时间】:2022-01-18 11:49:24
【问题描述】:
我对我的 IF 语句有疑问,它不起作用 :D 我想检查 ID、年龄和薪水,如果它大于 0,但我的代码只是忽略了这一点,想将 0 更改为某些含义,例如 - if age
class Person
{
private int _age;
private string _firstName;
private string _lastName;
private int _id;
public Person(int age, string firstName, string lastName, int id)
{
_age = age;
_firstName = firstName;
_lastName = lastName;
_id = id;
}
public int GetAge()
{
return _age;
}
public void SetAge(int age)
{
if (_age == 0)
_age = 21;
else
_age = age;
}
public int GetId()
{
return _id;
}
public void SetId(int id)
{
if (id > 0)
_id = id;
else
_id = 1;
}
public void Print()
{
Console.WriteLine("Age: {0}\tFirst name: {1}\tLast name: {2}\tID: {3}", this._age, this._firstName, this._lastName, this._id);
}
}
class TestInheritence
{
public static void Main(string[] args)
{
Employe[] employees = new[]
{
new Employe(21, "John", "Watson", 0)
};
employees[0].Print();
}
}
【问题讨论】:
-
顺便说一下,“Get*”和“Set*”方法是Java语法。在 C# 中,您将使用 properties
-
你为 id 做对了,为年龄复制该模式
-
@CaiusJard 但它也不起作用:D
标签: c# visual-studio if-statement