2

我是初学者,我目前正在学习 c#,我想知道是否可以在属性的 set 部分中使用 Console.ReadLine(),然后像读取用户输入的方法一样使用它,如下所示:

class Employee
{
    protected int empID;
    public int EmployeeID
    {
        set
        {
            Console.WriteLine("Please enter Employee ID:");
            this.empID = int.Parse(Console.ReadLine());
        }
    }
    //more code here
}
class Program
{
    static void Main(string[] args)
    {
        Employee employee1 = new Employee();
        employee1.EmployeeID;
        //more code here
    }
}

或者唯一的选择是直接在“Main”中使用Console.ReadLine(),如下:

class Employee
{
    protected int empID;
    public int EmployeeID { set; }
    //more code here
}
class Program
{
    static void Main(string[] args)
    {
        Employee employee1 = new Employee();
        employee1.EmployeeID = int.Parse(Console.ReadLine());
        //more code here
    }
}

预先感谢您的所有答案!


谢谢大家的答案!我现在可以看到这是编写代码的错误方式,我明白为什么。我认为通过使用'Console.ReadLine();' 在 'set' 属性中,从用户那里获取值会更容易,我不必重新编写这部分:'

Console.WriteLine("Please enter Employee ID:");
this.empID = int.Parse(Console.ReadLine());

每次我都会要求用户输入。但我现在明白为什么不应该使用它。
再次感谢您的所有回答,祝您有美好的一天!

4

3 回答 3

5

是的,你可以把一个Console.ReadLine()放在一组里面。但这是非常错误的。

C# 属性的编译类似于方法,因此您可以将任何可用的 C# 代码放入属性中,编译器将允许您这样做。(代码中的问题是您没有为集合编写正确的调用)。

但是在良好的实践和 SOLID 中思考,这是非常错误的。您的第二个代码段看起来好多了。

已编辑:关于您的代码,

如果您完全按照您编写的方式运行代码,我会注意到您的消息"Please enter Employee ID:"永远不会显示。get发生这种情况是因为对财产的和set方面存在误解。

看看这个特定的行:

employee1.EmployeeID;

这行代码是get对 property 的调用EmployeeID。也许这并不明显,因为您没有使用得到的值。但是这条线类似于:

var notUsedVar = employee1.EmployeeID;

要使用set属性操作,您需要一个归属操作,例如:

employee1.EmployeeID = 0; // or
employee1.EmployeeID++; // or
employee1.EmployeeID--; // or
employee1.EmployeeID += 1; // and so on...

Snippet ps:第一行你有一个set操作调用,但下面的行你有一个get调用和一个set调用之后。

这里有一些截断的代码给你确认并理解我在说什么:

class Employee
{
    private int _employeeID;

    public int EmployeeId
    {
        get
        {
            Console.WriteLine("The Employee.EmployeeId get operation was called.");
            return _employeeID;
        }
        set
        {
            Console.WriteLine("The Employee.EmployeeId set operation was called.");
            _employeeID = value;
        }
    }
}

class Program
{
    public static void Main()
    {
        var e = new Employee();

        e.EmployeeId++; // or any other exaple.
    }
}

如果您运行此代码,您将获得以下输出:

调用了 Employee.EmployeeId 获取操作。
调用了 Employee.EmployeeId 设置操作。

于 2015-08-28T19:07:57.077 回答
3
public class Employee
{
    public int EmployeeID { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter Employee ID:");

        var empID = int.Parse(Console.ReadLine());

        var employee1 = new Employee
        {
            EmployeeID = empID
        };
    }
}

Getters并且Setters应该只用于设置/返回属性持有的任何值。您还可以创建私有字段并使用不同的方法设置它们。但是,您不会Console.ReadLine()从课堂上打电话。该类是您的实体的表示。

于 2015-08-28T19:24:59.100 回答
1

你正朝着错误的方向前进。正确的方法是通过Main方法。

或者,如果您出于某种原因想将功能放入您的课程中,则应该是这种方式

class Employee
{
    protected int empID;
    public int EmployeeID 
    {
        get { return empId; }
    }
    //more code here
    public void AskEmployeeID()
    {
        Console.WriteLine("Please enter Employee ID:");
        this.empID = int.Parse(Console.ReadLine());
    }
}

现在您可以在Employee对象上调用此函数employee1.AskEmployeeID();

于 2015-08-28T19:16:38.120 回答