为什么我们不需要通过对象调用静态方法?

发布于 2024-11-09 18:47:52 字数 262 浏览 0 评论 0原文

public static void callit(ref int var)  
{  
   var++;  
}  
public static void main(Object sender, EventArgs e)  
{  
   int num=6;  
   callit(ref num);  
   Console.WriteLine(num);  
}

但如果这里的方法 callit() 不是静态的,那么我必须创建类的对象然后调用它。

public static void callit(ref int var)  
{  
   var++;  
}  
public static void main(Object sender, EventArgs e)  
{  
   int num=6;  
   callit(ref num);  
   Console.WriteLine(num);  
}

But if here method callit() would not be a static then I had to make object of class then call it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

少女七分熟 2024-11-16 18:47:52

这是正确的。需要在对象的实例上调用非静态方法。即使该方法实际上不使用对象的任何其他成员,编译器仍然强制执行实例方法需要实例的规则。另一方面,静态方法不需要在实例上调用。 这就是它们静态的原因。

That's correct. Non-static methods need to be called on an instance of an object. Even if the method doesn't actually use any other members of the object, the compiler still enforces the rule that instance methods require instances. Static methods, on the other hand, do not need to be called on an instance. That's what makes them static.

梦途 2024-11-16 18:47:52

正是因为这就是静态方法的全部意义。

实例方法需要知道您在类的哪个实例上调用该方法。

instance.Method();

然后他们可以引用类中的实例变量。

另一方面,静态方法不需要实例,但无法访问实例变量。

class.StaticMethod();

例如:

public class ExampleClass
{
    public int InstanceNumber { get; set; }

    public static void HelpMe()
    {
        Console.WriteLine("I'm helping you.");
        // can't access InstanceNumber, which is an instance member, from a static method.
    }

    public int Work()
    {
        return InstanceNumber * 10;
    }
}

您可以创建此类的实例来调用该特定实例上的Work()方法

var example = new ExampleClass();
example.InstanceNumber = 100;
example.Work();

static 关键字不过,这意味着您不需要实例引用来调用 HelpMe() 方法,因为它绑定到类,而不是该类的特定实例

ExampleClass.HelpMe();

Exactly because that's the whole point of static methods.

Instance methods require to know which instance of the class you call the method on.

instance.Method();

and they can then reference instance variables in the class.

Static methods, on the other hand, don't require an instance, but can't access instance variables.

class.StaticMethod();

An example of this would be:

public class ExampleClass
{
    public int InstanceNumber { get; set; }

    public static void HelpMe()
    {
        Console.WriteLine("I'm helping you.");
        // can't access InstanceNumber, which is an instance member, from a static method.
    }

    public int Work()
    {
        return InstanceNumber * 10;
    }
}

You could create an instance of this class to call the Work() method on that particular instance

var example = new ExampleClass();
example.InstanceNumber = 100;
example.Work();

The static keyword though, means that you don't need an instance reference to call the HelpMe() method, since it's bound to the class, and not to a particular instance of the class

ExampleClass.HelpMe();
往昔成烟 2024-11-16 18:47:52

我认为MSDN解释得很好

静态类和类成员用于创建无需创建类实例即可访问的数据和函数。静态类成员可用于分离独立于任何对象标识的数据和行为:无论对象发生什么,数据和函数都不会改变。当类中没有依赖于对象标识的数据或行为时,可以使用静态类。

您可以找到有关此主题的更多详细信息此处

I think MSDN explains it very well

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

You can find more details about this topic here

意中人 2024-11-16 18:47:52

如果我正确理解你的问题,这只是 C# 语法的问题。在您的示例中使用 callit(ref num); 没有任何歧义。确切地知道要调用什么方法,因为它是静态方法并且没有附加对象。另一方面,如果 callit 不是静态的,编译器将不知道要在哪个对象上调用该方法,因为您是从静态方法调用它(该方法没有对象)。因此,您需要创建一个新对象并调用该对象的方法。

当然,如果两个方法都不是静态的,则方法调用将对其自身进行操作,并且对象将是已知的,因此没有问题。

This is simply a matter of C# syntax, if I understand your question correctly. There is no ambiguity in using callit(ref num); in your example. It is known exactly what method to call, since it is a static method and there is no object attached. On the other hand, if callit was not static, the compiler would not know the object on which to call the method, since you are calling it from a static method (which has no object). So, you would need to create a new object and call the method on that object.

Of course, if neither method was static, the method call would operate on itself, and again, the object would be known so there is no problem.

梦回旧景 2024-11-16 18:47:52

因为静态方法与类本身相关联,而不是与特定的对象实例相关联。

Because static methods are associated with the class itself, not with a specific object instance.

煞人兵器 2024-11-16 18:47:52

静态函数作用于类。
成员函数在实例上工作。

是的,您只能调用对象(实例)的成员函数。没有实例,没有成员。

Static functions work on classes.
Member functions work on instances.

And yes, you can only call a member function of an object (instance). No instance, no member.

永不分离 2024-11-16 18:47:52

静态方法在类型(或类)上调用,而非静态方法在类型的实例(即类的对象)上调用。

您不能在静态方法中调用非静态方法或非静态变量,因为可能存在多个对象(即同一类型的实例)。

Static methods are called on the type (or class), whereas non-static methods are called on the instance of a type i.e. object of a class.

You cannot call non-static methods or non-static variables in a static method, as there can be multiple objects (i.e. instances of the same type).

悲凉≈ 2024-11-16 18:47:52

静态函数访问的类变量与任何其他静态函数和实例函数一样可以访问。意思是如果你有一个名为static int count的类变量,在静态方法staticincreaseCount() {count++; } 将变量 count 加 1,并且在静态方法 staticdecreaseCount() { count--; } 将变量 count 减 1。

因此,静态函数和实例函数都可以访问静态变量,但静态函数不能访问实例变量。

静态函数也称为类函数。
非静态函数也称为实例函数。

Static functions access class variables that are just as accessible by any other static functions AND instance functions. Meaning if you have a class variable called static int count, in static method static increaseCount() { count++; } increases the variable count by 1, and in static method static decreaseCount() { count--; } decreases the variable count by 1.

Therefore, both a static function and an instance function may access a static variable, but a static function MAY NOT access an instance variable.

Static functions are also called class functions.
Non static functions are also called instance functions.

迷雾森÷林ヴ 2024-11-16 18:47:52

静态方法独立于实例,假设有一个 man 类,其中有非静态的 eat 方法和静态的 sleep 方法,那么当您创建 man 的不同实例时,可以说 man m1、m2。 m1 和 m2 的 sleep 方法相同,而 eat 方法不同。在java中,所有静态变量都存储在堆内存中,假设运行时所有对象实例都共享该内存,如果静态变量发生变化,那么它将在我们的案例中的对象的所有实例上共享。m1和m2。但是,如果您更改非静态变量,它只会针对该对象实例

m1.anStaticvariable = 1; // 也改变 m2.anStaticvariable 的值

m1.nonStaticvariable = 1; //不会改变 m2.nonStaticvariable

希望这对你有帮助

Static method are independent of instance, suppose there is man class in that there is eat method which is non static and there is sleep method which is static then when you create different instance of man lets say man m1, m2. m1 and m2 share same sleep methods but different eat method. In java all static variables are stored in heap memory which is shared all object instances suppose at runtime if static variable changes then it will share on all instances of the object in our case.m1 and m2. But if you change non-static varible it will for only that object instance

m1.anStaticvariable = 1; // changes value of m2.anStaticvariable also
But
m1.nonStaticvariable = 1; //wont change m2.nonStaticvariable

Hope this helps you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文