java 是否通过对象“错误形式”调用静态方法?为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7884004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:49:58  来源:igfitidea点击:

Is calling static methods via an object "bad form"? Why?

javamethodsstaticinstance

提问by paxdiablo

In a recent question, someone asked about static methods and one of the answers stated that you generally call them with something like:

在最近的一个问题中,有人询问了静态方法,其中一个答案表示您通常使用以下内容调用它们:

MyClassName.myStaticMethod();

The comments on that also stated that you could also call it via an object with:

对此的评论还指出,您也可以通过对象调用它:

MyClassName myVar;
myVar.myStaticMethod();

but that it was considered bad form.

但它被认为是不好的形式。

Now it seems to me that doing this can actually make my life easier so I don't have to worryabout what's static or not (a).

现在在我看来,这样做实际上可以让我的生活更轻松,所以我不必担心什么是静态的(a)

Isthere some problem with calling static functions via an object? Obviously you wouldn't want to createa brand new object just to call it:

有一些问题,通过对象调用静态函数?显然,您不想创建一个全新的对象只是为了调用它:

Integer xyzzy;
int plugh = xyzzy.parseInt ("42", 10);

But, if you already have an object of the desired type, is there a problem in usingit?

但是,如果您已经拥有所需类型的对象,那么使用它有问题吗?



(a)Obviously, I can't call a non-static method with:

(a)显然,我不能调用非静态方法:

MyClassName.myNonStaticMethod();

but that's not the issue I'm asking about here.

但这不是我在这里问的问题。

回答by Mark Peters

In my opinion, the real use case that makes this so unreadable is something like this. What does the code below print?

在我看来,使这变得如此不可读的真正用例是这样的。下面的代码打印什么?

//in a main method somewhere
Super instance = new Sub();
instance.method();

//...
public class Super {
    public static void method() {
        System.out.println("Super");
    }
}

public class Sub extends Super {
    public static void method() {
        System.out.println("Sub");
    }
}

The answer is that it prints "Super", because static methods are not virtual. Even though instanceis-a Sub, the compiler can only go on the type of the variable which is Super. However, by calling the method via instancerather than Super, you are subtly implying that it will be virtual.

答案是它打印“Super”,因为静态方法不是虚拟的。即使instanceis-a Sub,编译器也只能处理变量的类型,即Super。但是,通过通过instance而不是调用该方法Super,您巧妙地暗示它将是虚拟的。

In fact, a developer reading the instance.method()would have to look at the declaration of the method (its signature) to know which method it actually being called. You mention

事实上,阅读 的开发人员instance.method()必须查看方法的声明(其签名)才能知道它实际调用的是哪个方法。你提到

it seems to me that doing this can actually make my life easier so I don't have to worry about what's static or not

在我看来,这样做实际上可以让我的生活更轻松,所以我不必担心什么是静态的

But in the case above context is actually very important!

但是在上面的情况下,上下文实际上非常重要!

I can fairly confidently say it was a mistake for the language designers to allow this in the first place. Stay away.

我可以相当自信地说,语言设计者一开始就允许这样做是错误的。远离。

回答by Ray Toal

The bad form comment comes from the Coding Conventions for Java

错误的形式注释来自 Java 的编码约定

See http://www.oracle.com/technetwork/java/codeconventions-137265.html#587

参见http://www.oracle.com/technetwork/java/codeconventions-137265.html#587

The reason for it, if you think about it, is that the method, being static, does not belongto any particular object. Because it belongs to the class, why would you want to elevate a particular object to such a special status that it appears to own a method?

如果你仔细想想,这样做的原因是该方法是静态的,不属于任何特定对象。因为它属于类,为什么要将特定对象提升到如此特殊的状态,以至于它似乎拥有一个方法?

In your particular example, you canuse an existing integer through which to call parseInt(that is, it is legal in Java) but that puts the reader's focus on that particular integer object. It can be confusing to readers, and therefore the guidance is to avoid this style.

在您的特定示例中,您可以使用现有的整数进行调用parseInt(即,它在 Java 中是合法的),但这会将读者的注意力集中在该特定的整数对象上。它可能会让读者感到困惑,因此指导方针是避免这种风格。

Regarding this making life easier for you the programmer, turn it around and ask what makes life easier on the reader? There are two kinds of methods: instance and static. When you see a the expression C.mand you know Cis a class, you know mmust be a static method. When you see x.m(where xis an instance) you can't tell, but it looks like an instance method and so most everyone reserves this syntax for instance methods only.

关于这让程序员的生活更轻松,请转过头来问问是什么让读者的生活更轻松?有两种方法:实例和静态。当您看到一个表达式C.m并且您知道C是一个类时,您就知道m必须是一个静态方法。当您看到x.mx实例在哪里)时,您无法分辨,但它看起来像一个实例方法,因此大多数人只为实例方法保留了这种语法。

回答by Dave Newton

It's a matter of communication. Calling a method on an instance impliesyou're acting on/with that particular instance, not on/with the instance's class.

这是沟通的问题。在实例上调用方法意味着您正在/与该特定实例一起操作,而不是在/与实例的类上操作。

回答by Dave Newton

It might get super confusing when you have an object that inherits from another object, overriding its static method. Especially if you're referring to the object as a type of its ancestor class. It wouldn't be obvious as to which method you're running.

当您有一个从另一个对象继承的对象并覆盖其静态方法时,它可能会变得非常混乱。尤其是当您将对象称为其祖先类的一种类型时。您正在运行哪种方法并不明显。