简体   繁体   English

为什么我在Java中的同一个类中没有抽象和静态方法

[英]Why can I not have an abstract and static method in the same class in Java

I have the following class: 我有以下课程:

public abstract class A
{
  public abstract String doSomething(String X, String Y);
  public static String doSomething(String X, String Y){return X + Y;}
  ...
}

The issue I have is that the static and abstract doSomething() methods seem to clash as duplicates. 我遇到的问题是静态和抽象的doSomething()方法似乎是重复的。 I thought this should be fine because the static method belongs to the class, not an instance of the class, so I was going to use the abstract method to enforce the method on all subclasses and the static method as a helper so that I have nicely factored code. 我认为这应该没问题,因为静态方法属于类,而不是类的实例,因此我将使用抽象方法在所有子类上强制执行该方法,并将静态方法作为帮助程序,以便我很好地使用因子代码。

I know I could probably add an interface into the mix, but I don't really understand what's wrong with my abstract and static methods existing on the same class. 我知道我可能会在混合中添加一个接口,但我真的不明白我在同一个类上存在的抽象和静态方法有什么问题。 What's wrong with this? 这有什么问题?

In Java it is valid (despite being misleading and confusing) to call a static method from an object instance rather than the class name (despite warnings generated by many compilers). 在Java中,从对象实例调用静态方法而不是类名称(尽管许多编译器生成警告 )是有效的(尽管有误导性和混淆)。

System.out.println(String.valueOf(true)); // Prints "true".
System.out.println("".valueOf(true)); // Prints "true", unfortunately.

So the following seemingly valid code wouldn't know which of those methods to call: 所以下面看似有效的代码不知道要调用哪些方法:

A a = getInstanceOfConcreteSubclassOfA();
a.doSomething(null, null); // Compiler can't decide which method to call...

Unfortunately, it's just one of the few ugly corners of the Java language. 不幸的是,它只是Java语言中为数不多的几个角落之一。

It's not specific to abstract methods; 它并不特定于抽象方法; in general, Java doesn't let you have two methods with the same parameter-types but one being static and one not. 通常,Java不允许您使用相同参数类型的两个方法,但一个是静态的而一个不是。 Something like this: 像这样的东西:

public String doSomething(String X, String Y){return X + Y;}
public static String doSomething(String X, String Y){return X + Y;}

would also be illegal. 也是非法的。

(This makes sense when you consider that you're allowed to call a static method "on" an actual instance, or for that matter, on any expression of the appropriate type. The compiler translates ((A)null).staticMethod() to A.staticMethod() .) (当你考虑允许在一个实际的实例上调用一个静态方法,或者就此而言,在任何相应类型的表达式上调用静态方法时,这是有道理的。编译器转换((A)null).staticMethod()A.staticMethod() 。)

Each method has a signature composed of: 每种方法都有一个签名组成:

method name
parameter type
Return type

If 2 methods have the same signature, this will cause an error. 如果2个方法具有相同的签名,则会导致错误。

the word static does not interfere in the signature of the method just like const . static 一样的方法的签名干扰const

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

相关问题 Java类扩展了抽象类,但必须具有静态方法 - Java Class extends Abstract class but must have static method java static 摘要方法 class - java static method for abstract class Java:为什么抽象类经常具有静态方法?这样做的目的是什么? - Java:Why the abstract class often has a static method?And the purpose of this? 为什么不能在Java中使用相同的方法签名的静态和非静态方法? - Why can't you have a static and non-static method with the same method signature in Java? Java抽象类实现一个接口,两者具有相同的方法 - Java abstract class implements an interface, both have the same method 我怎样才能调用Abstract类的非静态方法 - How can i call an non static method of Abstract class 为什么我们不能在(非静态)内部类(Java 16 之前)中使用静态方法? - Why can't we have static method in a (non-static) inner class (pre-Java 16)? 为什么我不能在我的实现中使抽象方法静态化? - Why can't I make an abstract method static in my implementation? 我可以在java中使用方法链接而不执行不安全操作的抽象构建器类吗? - Can I have an abstract builder class in java with method chaining without doing unsafe operations? 为什么在同一个列表中的同一方法中不能有2个循环? 爪哇 - Why can not I have 2 loops in the same method on an equal list? JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM