简体   繁体   English

静态与公共C#

[英]Static vs. public C#

I know that it's been answered multiple times here on SO, but I still don't get the nuts of bolts of what exactly it means to instantiate a class. 我知道在SO上已经多次回答了这个问题,但我仍然没有弄清楚实例化类的确切含义。 I read this and it did help my understanding. 我读了这篇文章 ,这确实有助于我理解。

I know that static classes like Console cannot be used with the new expression like Console c = new Console() because there aren't any instance variables in that class. 我知道像Console这样的静态类不能像Console c = new Console()这样的new表达式一起使用,因为该类中没有任何实例变量。 I also know that static classes provide on 'generic' methods and are generally used for Math functions. 我也知道静态类提供'泛型'方法,通常用于Math函数。 I know that once you instantiate a class like Double d = new Double(); 我知道一旦你实例化了一个像Double d = new Double(); you are now given access to whatever methods are inside of the Double class. 您现在可以访问Double类中的任何方法。

I know these facts but I feel like I don't really understand what they actually MEAN. 我知道这些事实,但我觉得我真的不明白他们实际上是什么意思。 Could someone give an example of where a static class is absolutely necessary and one where creating an instance of a class is absolutely necessary? 有人可以给出一个静态类绝对必要的例子,以及创建类的实例是绝对必要的吗?

Think of a class like a set of blueprints. 想象一个类似一组蓝图的类。 Instantiating a class is like taking the blueprints and building the item. 实例化类就像获取蓝图并构建项目一样。 When an engineer designs a car, he comes up with the design. 当工程师设计汽车时,他提出了设计。 That would be the class. 那将是班级。 After the car is designed, the plans are handed off to the assembly line to be built. 汽车设计完成后,计划将被移交到要建造的装配线上。 Each car that rolls off the line would be an instance of that design. 每辆下线的汽车都是该设计的一个实例。

When the car is still just a design, you can't really do anything with it. 当汽车还只是一个设计时,你无法用它做任何事情。 You can't open its door if there's no car. 如果没有车,你就无法开门。 Once you have the instance of a car, you can manipulate it. 一旦拥有了汽车的实例,就可以操纵它。 You can open the door, start the engine, etc. The same goes for a class like Double . 你可以打开车门,启动发动机等。像Double Once you have the instance, you can manipulate it. 拥有实例后,您可以对其进行操作。

A static class, like Console , are classes that don't have instances. 静态类(如Console )是没有实例的类。 They're more like a way to group useful related functionality. 它们更像是一种分组有用的相关功能的方法。 In the case of Console , the functionality is used to interact with the command line. Console的情况下,该功能用于与命令行交互。 Math is used to group mathematics related code. Math用于分组与数学相关的代码。 Configuration is used to read/manipulate configuration files. Configuration用于读取/操作配置文件。 None of these things require you to create anything unique for them to work. 这些都不需要您创建任何独特的工作。

A public class must be called in application by another class, for exampel this may be a class of data access (called by businnes layer). 必须在另一个类的应用程序中调用公共类,例如,这可能是一类数据访问(由businnes层调用)。

A static class need not necessarily the creation of an instance for example tracing or logging class. 静态类不一定需要创建实例,例如跟踪或记录类。

One (perhaps over) simplified example for thinking about static is the following: 考虑静态的一个(可能结束)简化示例如下:

If you have the class Dog; 如果你有班级狗; you could instantiate the class to create Dog Poodle = new Dog(); 你可以实例化这个类来创建Dog Poodle = new Dog(); and Dog Labrador = new Dog(); 和狗拉布拉多=新狗(); If the Dog class has a variable hairColor, then for Poodle and Labrador, hairColor could be different. 如果Dog类有一个变量hairColor,那么对于Poodle和Labrador,hairColor可能会有所不同。 The two different instances are seperate. 两个不同的实例是分开的。

If however, you added a static variable to Dog called numberOfDogs, and incremented the variable every time a new Dog was instantiated (you could do this is the constructor for example), then the variable would count the total number of Dogs, and would be the same number regardless of which instance of Dog you checked. 但是,如果您向Dog添加了一个名为numberOfDogs的静态变量,并且每次实例化一个新Dog时都会增加该变量(例如,您可以这样做构造函数),那么该变量将计算Dogs的总数,并且将是无论您检查哪个Dog实例,都是相同的数字。 This is useful (and dangerous) depending on how you use it. 根据您的使用方式,这很有用(也很危险)。

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

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