为什么不能静态类有非静态方法和变量?静态、变量、方法、类有非

2023-09-03 00:14:17 作者:给儿子聘个妈

为什么不能静态类有非静态方法,当非静态类可以有静态方法和变量的变量?

Why can't static classes have non-static methods and variables when non-static classes can have static methods and variables?

什么是具有在非静态类的静态方法和变量的优势在哪里?虽然有一个静态构造函数在非静态类是可以理解的。

What is the advantage of having static methods and variables in a non static class? Though having a static constructor in a non-static class is understandable.

推荐答案

静态类不能被实例摆在首位,因此,即使你可以声明非静态(实例)的成员,他们永远不能被访问。既然是不是真的有一个点,允许它的原因,语言简单地禁止它。

Static classes can't be instantiated in the first place, so even if you could declare non-static (instance) members, they can never be accessed. Since there really isn't a point allowing it for that reason, the language simply prohibits it.

记住,静态类的只是班的,而有两件事情是有直接关系的非静态类:类的自己,实例/对象的类的。

Bear in mind that static classes are just the classes, while there are two things that are directly related to non-static classes: the classes themselves, and the instances/objects of the classes.

一个非静态类可以有静态和非静态成员,以使静态成员适用于类,而非静态成员适用于该类的实例。

A non-static class can have both static and non-static members so that the static members apply to the class, whereas the non-static members apply to the instances of that class.