简体   繁体   English

实现接口方法,方法调用和类型转换

[英]Implements method of interface, method call and type cast

Consider the followoing code 考虑以下代码

interface MyInterface{
    void method(String s);// if we write static modifier we have compile error
}
class MyClass implements MyInterface{
    public static void main(String[] args){
        myMethod(new Object());//Compile error
    }
    static void method(String s){...}// compile error
    static void myMethod(Number n){...}

}
  1. Why we cant define static method in interface? 为什么我们不能在接口中定义静态方法?
  2. Why we cant implements method() with static modifier? 为什么我们不能用静态修饰符实现method()
  3. When we are calling myMethod with reference to Object we have compile error. 当我们参考对象调用myMethod时,发生编译错误。 As i understood, compiler doesnt cast automatically, isnt it? 据我了解,编译器不会自动强制转换,不是吗?
  4. Consider the following code 考虑以下代码

    Object someObj; ... Number n= (Number) someObj;

What compiler is doing when we cast to Number in this case? 在这种情况下,当我们转换为Number时,编译器在做什么?

Why we cant define static method in interface? 为什么我们不能在接口中定义静态方法?

All the methods of an interface are by default public abstract . 接口的所有方法默认情况下都是public abstract Using static modifier doesn't make sense. 使用static修饰符没有意义。 Because the invocation of static methods aren't polymorphic. 因为静态方法的调用不是多态的。 In the sense, you can't override them. 从某种意义上说,您不能覆盖它们。 You invoke the static methods on the class name only. 您只能在类名称上调用静态方法。 Well, you can invoke them on some reference too, but that will be ultimately resolved on the basis of the declared type of the reference. 好了,您也可以在某些引用上调用它们,但是最终将根据声明的引用类型来解决。 Now, since the method is by default abstract, it doesn't make sense to invoke it. 现在,由于默认情况下该方法是抽象的,因此调用它没有任何意义。 It doesn't have any body to do any task. 它没有任何机构可以执行任何任务。

Why we cant implements method() with static modifier? 为什么我们不能用静态修饰符实现method()?

When you try to add static modifier to the overridden method, it is not considered as being overriding. 当您尝试将static修饰符添加到重写的方法时,不认为它是重写的。 So, your class essentially have two different methods, with same name, same parameters, and same return type. 因此,您的类实际上具有两种不同的方法,它们具有相同的名称,相同的参数和相同的返回类型。 That is of course not allowed inside a class. 当然,这是不允许在班级内部进行的。

Note that, you have to explicitly add public modifier to the overriding method in the class, else your code won't compile. 请注意,您必须在类的重写方法中显式添加public修饰符,否则您的代码将无法编译。 The reason being, you can't decrease the visibility of the overridden method in the subclass. 原因是,您不能降低子类中重写方法的可见性。

When we are calling myMethod with reference to Object we have compile error. 当我们参考对象调用myMethod时,发生编译错误。 As i understood, compiler doesnt cast automatically, isnt it? 据我了解,编译器不会自动强制转换,不是吗?

Jave doesn't do automatic narrowing conversion. Jave不会自动缩小转换范围。 You need to add cast explicitly. 您需要显式添加演员表。 But even if it allowed, how you expect your code to behave, because you are trying to refer a super class object with a subclass reference? 但是,即使允许,您仍希望代码表现如何,因为您尝试使用子类引用来引用超类对象? You can of course make your code to compile by adding a cast while invoking the method: 您当然可以通过在调用方法时添加强制类型转换来使代码进行编译:

myMethod((Number)new Object());  // This will compile, but fail at runtime

The above invocation will result in a ClassCastException at runtime. 上面的调用将在运行时导致ClassCastException

However, if you have an Object reference refering to an object of any subclass of Number , you can add an explicit cast, that will be type safe: 但是,如果您有一个Object引用引用Number的任何子类的对象,则可以添加一个显式的强制类型转换,该类型将是安全的:

Object obj = new Integer(5);
Number num = (Number)obj;  // This is fine. Because `obj` is referring to an `Integer`.

And finally, the signature of your main method is not correct. 最后,您的main方法的签名不正确。 You are missing the public modifier. 您缺少public修饰符。

Why we cant define static method in interface? 为什么我们不能在接口中定义静态方法?

Interfaces are designed to work with polymorphism, basically. 基本上,接口旨在与多态一起使用。 Polymorphism How would you know which implementation to call when calling a static method on an interface? 多态性当在接口上调用静态方法时,如何知道要调用哪个实现?

// Should that invoke MyClass.method or MyOtherImplementation.method?
MyInterface.method("foo");

Next: 下一个:

Why we cant implements method() with static modifier? 为什么我们不能用静态修饰符实现method()?

The idea is that the method is called on some object which implements the interface - which makes it an instance method. 这个想法是在实现接口的某些对象上调用该方法-这使其成为实例方法。

When we are calling myMethod with reference to Object we have compile error. 当我们参考对象调用myMethod时,发生编译错误。 As i understood, compiler doesnt cast automatically, isnt it? 据我了解,编译器不会自动强制转换,不是吗?

No, the compiler doesn't cast automatically. 不,编译器不会自动强制转换。 There's no implicit conversion from Object to Number , so you can't call a method with a parameter of type Number with an argument of type Object . 没有从ObjectNumber隐式转换,因此您不能使用Number类型的参数和Object类型的参数来调用方法。

What compiler is doing when we cast to Number in this case? 在这种情况下,当我们转换为Number时,编译器在做什么?

It's validating that the value of someObj is either null or a reference to an instance of Number or a subclass. 验证someObj值为null或对Number实例或子类的引用。

until JDK7: 直到JDK7:

  1. because static methods are bound to the class. 因为静态方法绑定到该类。 you normaly invoke them like this: 您通常按如下方式调用它们:

     MyClass.method(""); 

    you can not override them. 您无法覆盖它们。

  2. see 1 and all interface method are public abstract you can not change that! 参见1,所有接口方法都是public abstract您不能更改!

  3. no the compiler does not cast automatically 否编译器不会自动强制转换

  4. he tries to cast and fails 他试图抛投失败

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

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