为什么我想要一个需要实例的方法?

发布于 2024-11-30 04:59:06 字数 38 浏览 3 评论 0原文

为什么我需要一个需要实例的方法?为什么我不将所有方法设为静态?

Why would I want a method that needs an instance? Why wouldn't I make all my methods static?

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

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

发布评论

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

评论(4

冷月断魂刀 2024-12-07 04:59:06

为什么您希望程序中的任何位置有任何状态?

您能想象如果没有 String 实例,并且 String 上的所有内容都是静态的吗?您将如何表示两个不同的字符序列?现在将相同的逻辑应用于其他代码。

从根本上讲,面向对象语言是围绕具有状态的对象这一理念构建的:Book 的一个实例与 Book 的另一个实例不同 - 每个 Book 实例封装了它的名称、作者、发布日期等。您如何仅使用静态方法而不使用实例来对其进行建模?

当然,您可以将所有方法设为静态,并在需要使用状态的每个调用中传入一个Book作为第一个参数。在幕后,类似的事情已经发生了……只不过你失去了多态性,所以接口、抽象类等都是无用的。不好。

Why would you not want any state anywhere in your program?

Can you imagine if there were no String instances, and everything on String was static? How would you represent two distinct sequences of characters? Now apply the same logic to other code.

Fundamentally, OO languages are built around the idea of objects with state: one instance of Book isn't the same an another instance of Book - each Book instance encapsulates its name, author, publication date etc. How would you model that with only static methods, and no instances?

Of course you could make all your methods static and pass in a Book as the first parameter on each call that needed to use the state. Behind the scenes, something pretty much like that is already happening... except you've then lost polymorphism, so interfaces, abstract classes etc are useless. Not good.

白龙吟 2024-12-07 04:59:06

因为对象是状态和行为在一起,封装到单个组件中。

如果您有单独的实例,则意味着它们每个都可以拥有因实例而异的私有数据。

静态数据和方法在类级别共享。各个实例不能有不同的静态数据。

Because objects are state and behavior together, encapsulated into a single component.

If you have individual instances, it means they can each have private data that varies from instance to instance.

Static data and methods are shared at the class level. Individual instances cannot have different static data.

丑丑阿 2024-12-07 04:59:06

静态方法不能直接访问对象内的成员变量 - 它们只能访问静态变量。

如果您有一个汽车类和一个静态数据成员(例如其中的整数),那么您只能拥有一辆汽车,因为您无法创建汽车的多个实例并获取该变量的多个实例 - 您只能拥有一个静态的汽车。

每辆车不可能有相同的车牌号,因此每辆车都需要自己的车牌变量。

类中使用该变量的方法必须是非静态的,才能直接对其进行操作。

Static methods can't directly access the member variables within an object - they can only access static variables.

If you had a car class and a static data member like an integer in it, you could only ever have one car because you cant make multiple instances of cars and get multiple instances of that variable - you'd only ever have the single static one.

Every car can't have the same license plate number, thus ever car needs its own license plate variable.

The methods in the class that work with that variable need to be non-static to work on it directly then.

以酷 2024-12-07 04:59:06

使用“Car”类的示例,您可能有一个名为“startCar()”的方法。显然,您希望此方法仅与汽车的特定“实例”交互,而不是对所有汽车都是全局的。 Java 中的示例:

public class Car {
  public void startCar() {
    // code to start car
  }
}

public class MyProgram {
  public static void main(String[] Args) {
    Car myFord = new Car();
    Car myOpel = new Car();
    myCar.startCar; // starts the Car "myCar" and leaves "myOpel" alone
  }
}

还值得注意的是,静态方法可能不会使用定义它们的类的实例变量。

Using the example of a "Car" class, you might have a method called "startCar()". Obviously, you want this method to interact only with a particular "instance" of a car and not be global to ALL your cars. Example in Java:

public class Car {
  public void startCar() {
    // code to start car
  }
}

public class MyProgram {
  public static void main(String[] Args) {
    Car myFord = new Car();
    Car myOpel = new Car();
    myCar.startCar; // starts the Car "myCar" and leaves "myOpel" alone
  }
}

It's also worth noting that Static Methods may not make use of Instance Variables of the class in which they are defined.

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