我什么时候应该在类中使用静态方法?有什么好处?

发布于 2024-08-18 07:15:45 字数 277 浏览 10 评论 0原文

我有静态变量的概念,但是类中静态方法有什么好处。我参与过一些项目,但我没有将方法设为静态。每当我需要调用类的方法时,我都会创建该类的一个对象并调用所需的方法。

问:方法中的静态变量即使在执行方法时也保留其值,但只能在其包含的方法中访问,但是静态方法的最佳定义是什么?

问: 调用静态方法而不创建该类的对象是静态方法的唯一好处吗?

问:静态方法的可访问范围是多少?

谢谢

I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need to call a method of a class, I create an object of that class and call the desired method.

Q: Static variable in a method holds it's value even when method is executed but accessible only in its containing method but what is the best definition of static method?

Q: Is calling the static method without creating object of that class is the only benefit of static method?

Q: What is the accessible range for static method?

Thanks

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

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

发布评论

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

评论(10

弥繁 2024-08-25 07:15:45

您对静态变量的描述更适合 C 中的描述。面向对象术语中静态变量的概念在概念上是不同的。我在这里吸取了 Java 的经验。当静态方法和字段在概念上不属于某事物的实例时,它们非常有用。

考虑一个 Math 类,其中包含一些常见值(例如 Pi 或 e)以及一些有用的函数(例如 sin 和 cos)。创建单独的实例来使用这种功能确实没有意义,因此它们作为静态更好:

// This makes little sense
Math m = new Math();
float answer = m.sin(45);

// This would make more sense
float answer = Math.sin(45);

在 OO 语言中(再次从 Java 角度来看)函数,或者更广为人知的方法,不能有静态局部变量。只有类可以拥有静态成员,正如我所说,这与 C 中的静态概念相比几乎没有什么相似之处。

Your description of a static variable is more fitting to that found in C. The concept of a static variable in Object Oriented terms is conceptually different. I'm drawing from Java experience here. Static methods and fields are useful when they conceptually don't belong to an instance of something.

Consider a Math class that contains some common values like Pi or e, and some useful functions like sin and cos. It really does not make sense to create separate instances to use this kind of functionality, thus they are better as statics:

// This makes little sense
Math m = new Math();
float answer = m.sin(45);

// This would make more sense
float answer = Math.sin(45);

In OO languages (again, from a Java perspective) functions, or better known as methods, cannot have static local variables. Only classes can have static members, which as I've said, resemble little compared to the idea of static in C.

飘落散花 2024-08-25 07:15:45

静态方法不会将“this”指针传递给对象,因此它们无法引用非静态变量或方法,但因此在运行时可能会更高效(参数更少,并且创建和销毁对象没有开销)。

它们可用于将内聚方法分组到单个类中,或对其类的对象进行操作,例如在工厂模式中。

Static methods don't pass a "this" pointer to an object, so they can't reference non-static variables or methods, but may consequently be more efficient at runtime (fewer parameters and no overhead to create and destroy an object).

They can be used to group cohesive methods into a single class, or to act upon objects of their class, such as in the factory pattern.

爱已欠费 2024-08-25 07:15:45

静态方法的语法 (php):

<?php
class Number {
    public static function multiply($a, $b) {
        return $a * $b;
    }
}
?>

客户端代码:

echo Number::multiply(1, 2);

这比以下更有意义:

$number = new Number();
echo $number->multiply(1, 2);

由于 multiply() 方法不使用任何类变量,因此不需要 Number< 的实例/代码>。

Syntax (php) for static methods:

<?php
class Number {
    public static function multiply($a, $b) {
        return $a * $b;
    }
}
?>

Client code:

echo Number::multiply(1, 2);

Which makes more sense than:

$number = new Number();
echo $number->multiply(1, 2);

As the multiply() method does not use any class variables and as such does not require an instance of Number.

朦胧时间 2024-08-25 07:15:45

本质上,静态方法允许您用面向对象的语言编写过程代码。它允许您调用方法而无需先创建对象。

Essentially, static methods let you write procedural code in an object oriented language. It lets you call methods without having to create an object first.

仅此而已 2024-08-25 07:15:45

您想要在类中使用静态方法的唯一时间是当给定方法不需要创建类的实例时。这可能是在尝试返回共享数据源(例如 Singleton)或执行不修改对象内部状态的操作(例如 String.format)时。

这个维基百科条目很好地解释了静态方法: http://en.wikipedia.org/wiki /Method_(计算机科学)#Static_methods

The only time you want to use a static method in a class is when a given method does not require an instance of a class to be created. This could be when trying to return a shared data source (eg a Singleton) or performing an operation that doesn't modify the internal state of the object (String.format for example).

This wikipedia entry explains static methods pretty well: http://en.wikipedia.org/wiki/Method_(computer_science)#Static_methods

那请放手 2024-08-25 07:15:45

静态变量和静态方法绑定到类,而不是类的实例。

静态方法不应包含“状态”。任何与状态相关的东西都应该绑定到实例化的对象,而不是类。

Static variables and static methods are bound to the class, and not an instance of the class.

Static methods should not contain a "state". Anything related to a state, should be bound to an instantiated object, and not the class.

戏剧牡丹亭 2024-08-25 07:15:45

静态方法的一种常见用法是命名构造函数习惯用法。请参阅:http://www.parashift.com/c++- faq-lite/ctors.html#faq-10.8

One common usage of static methods is in the named constructor idiom. See: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8.

_蜘蛛 2024-08-25 07:15:45

PHP 中的静态方法

无需创建即可调用类对象。

只能调用静态方法和函数。

Static Methods in PHP:

Can be called without creating a class object.

Can only call on static methods and function.

偏爱自由 2024-08-25 07:15:45

当您想要在类的不同对象之间共享一些信息时,使用静态变量。由于变量是共享的,每个对象都可以更新它,并且更新后的值也可用于所有其他对象。
由于静态变量可以共享,因此通常称为类变量。

Static variable is used when you want to share some info between different objects of the class.As variable is shared each object can update it and the updated value be available for all other objects as well.
As static variable can be shared,these are often called as class variable.

掩于岁月 2024-08-25 07:15:45

静态元素可以从任何上下文(即脚本中的任何位置)访问,因此您可以访问这些方法,而无需在对象之间传递类的实例。

静态元素在类的每个实例中都可用,因此您可以设置想要对类型的所有成员可用的值。

进一步阅读链接

static elements are accessible from any context (i.e. anywhere in your script), so you can access these methods without needing to pass an instance of the class from object to object.

Static elements are available in every instance of a class, so you can set values that you want to be available to all members of a type.

for further reading a link!

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