是什么使静态方法可访问? [英] What makes static methods accessible?

查看:129
本文介绍了是什么使静态方法可访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么可以在不创建实例的情况下在外部访问静态成员?

Why static members are accessible outside without making instance?

推荐答案

这是设计使然.在功能独立于实例变量的情况下,静态方法有其优点.这对于应用程序中诸如main之类的程序入口点也很重要.

干杯!

—MRB
It''s by design. Static methods have their merit in cases when functionality is independant to instance variables. Also it''s important for program entry points like main in applications.

Cheers!

—MRB


这是一个灯泡"时刻,如果没有图片(和看到你的脸),很难解释,但是...

让我们谈谈汽车.

汽车有几个轮子?
汽车是什么颜色的?
汽车使用什么燃料?

第一个问题很简单:四个.
另外两个更难:汽车"没有颜色,或使用燃料类型. 这辆车"可以. 那辆车"做.但是,汽车"并没有比您学会汽车"时要驾驶汽车"更多.相反,您需要一个汽车实例来执行任何这些操作.您驾驶您的汽车",您的汽车"为绿色,并使用汽油.
我的车"是另一种情况:它可能是红色,并使用柴油.

但是,它们全部-这辆车",那辆车",您的车"和我的车"-具有共同的特征:它们都有四个轮子.这是一个静态属性-它不是特定于汽车的任何实例,而是通用的,并由所有实例共享.

在软件中是相同的:static变量或方法不是特定于任何实例的,对于所有实例而言都是相同的,因此您无需指定实例即可使用它.

It''s a "lightbulb" moment, and difficult to explain without pictures, (and seeing your face), but...

Lets talk about cars.

How many wheels has a car?
What color is a car?
What fuel does a car use?

The first question is easy: four.
The other two are harder: "a car" does not have a color, or use a fuel type. "this car" does. "that car" does. But "a car" doesn''t, any more than you would drive "a car" despite learning to drive "a car". Instead you need an instance of a car to do any of those things. You drive "your car", "your car" is green, and uses petrol.
"My car" is a different instance : it could be red, and use diesel.

But, all of them - "this car", "that car", "your car" and "my car" - share common features: they all have four wheels. This is a static property - it is not specific to any single instance of a car, but is general and shared by all instances.

In software it is the same: a static variable or method is not specific to any instance, it is the same for them all, so you do not need to specify an instance in order to use it.

Does that make sense?


正确的问题应该是:是什么使非静态方法能够访问实例?

这是因为静态方法没有什么特别的.它们完全等同于OOP之前存在的旧的良好功能.它们仅接收在其签名中明确列出的那些参数,并且只能与这些参数一起使用.可以调用其他静态方法和属性(实际上所有属性都被调用),并使用静态变量,前提是对它们有足够的访问权限.那将无法访问超出其参数范围的任何非静态内容.当然,某些参数可以引用类或结构的实例.通过这种方式,并且只有通过这种方式,静态方法才能访问那些实例的非静态成员.

相反,非静态方法可以访问某些实例.这样,它需要实例被调用.它来自哪里?实际上,所有非静态方法(也称为实例方法)都有附加的隐藏参数"this"用户来引用实例.

实际上,类似
Right question should be: what makes a non-static method able to access the instance?

This is because there is nothing special about static methods. They are completely equivalent to old good functions existing before OOP. They receive only those parameters which are explicitly listed in their signature and can work only with these parameters. The can call other static methods and properties (all properties are actually called) and use static variables provided there are sufficient access to them. That cannot access anything non-static beyond their parameters. Of course, some parameters can be referenced to instanced of classes or structures; in this and only in this way a static method can access non-static members of those instances.

In contrast, a non-static method gets access to some instance. In this way, it needs instance to be called. Where it comes from? Actually, all non-static methods (also called instance methods) have additional hidden parameter "this" user to reference the instance.

In effect, something like
class A {
    internal void InstanceMethod(int value, string name) {/*...*/}
    //...
}
A a = new A(/*...*/);
a.InstanceMethod(3, "name");



意思是:



Means this:

// Pseudo-code, not code!

A a = new A(/*...*/);
A.InstanceMethod(a, 3, "name);
// when implementation uses "this",
// it is equal to the reference of the parameter 'a' passed in this call



有这个主意吗?

—SA



Got the idea?

—SA


这篇关于是什么使静态方法可访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆