简体   繁体   English

在 C# 中创建类的实例

[英]making instance of class in c#

In this code在这段代码中

using System;
namespace Hello
{
    class TestEmployee
    {
        static void Main()
        {
            Employee employee = new Employee();
            employee.salary = 1000;
            employee.bonus = 100;
            employee.CacalculateTotalPay();
            Console.ReadKey();
        }
    }
    class Employee
    {
        public double salary;
        public double bonus;
        public void CacalculateTotalPay()
        {
            Console.WriteLine(salary + bonus);
        }
    }
}

Why should we make a instance of a class with the new keyword when we already declared the employee variables type with Employee class?当我们已经用Employee类声明了 employee 变量类型时,为什么还要用new关键字创建一个类的实例?

I didn't tried anything.我没有尝试任何东西。

When you declare a variable for a class without a new instance, like this:当您为没有新实例的类声明变量时,如下所示:

Employee employee;

you don't actually have an instance of the class yet.您实际上还没有该类的实例。 There is no Employee object here.这里没有 Employee 对象。 What you have is a variable with the potential to refer to a class instance.您拥有的是一个可能引用类实例的变量。 Initially there isn't anything there;最初那里什么都没有; only the potential... it is a null reference.只有潜力……它是一个空引用。 Therefore we might also create a new instance of the type and assign that new instance to our variable:因此,我们还可以创建该类型的一个new实例并将该新实例分配给我们的变量:

Employee employee = new Employee();

You could avoid this by making the Employee type static .您可以通过将Employee类型static来避免这种情况。 Then, instead of declaring a variable at all you would refer to its members via the type name (ie Employee.salary ).然后,您根本不用声明一个变量,而是通过类型名称(即Employee.salary )引用它的成员。 However, that's limiting.然而,这是有限的。 If you refer to the members via type name you would have no way to distinguish one employee from another, and you likely expect to have many more than just the one employee.如果您通过类型名称引用成员,您将无法将一名员工与另一名员工区分开来,并且您可能希望拥有的不仅仅是一名员工。

Therefore we do not use static and instead create new instances so that each instance can represent a different employee.因此,我们不使用static ,而是创建新实例,以便每个实例可以代表不同的员工。

This can be confusing for new programmers, because the first exposure to code is often via a class with a static Main() method, where you did not need to create an instance, and with primitive types like int and string that have support in the language so you don't need to use the new operator.这可能会让新程序员感到困惑,因为第一次接触代码通常是通过一个带有静态Main()方法的类,您不需要在其中创建intstring具有在语言,因此您不需要使用new运算符。 It may be some time before you progress far enough to need to worry about object instances.可能需要一段时间才能取得足够的进展,需要担心对象实例。 But rest assured, over time instances are the norm, and static and struct will be less common.但请放心,随着时间的推移,实例将成为常态, staticstruct将不那么常见。

Finally, it is important to come to terms with the difference between object instances, references, variables, and types (classes/structs).最后,重要的是要了解对象实例、引用、变量和类型(类/结构)之间的区别。 Those are four different kinds of thing, and it is difficult to write effective code without understanding how they relate to each other.这是四种不同的东西,如果不了解它们之间的相互关系,就很难编写出有效的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM