方法不会覆盖 Eclipse 中的包可见方法

发布于 2024-10-18 05:36:33 字数 152 浏览 1 评论 0原文

从 Eclipse Java 编译器设置:方法不会覆盖包可见方法

“包默认方法在不同的包中不可见,因此不能被覆盖。启用此选项时,编译器将发出信号这种情况要么是错误,要么是警告。”

如何触发此警告/错误?我正在寻找代码示例。

From the Eclipse Java compiler setting: Method does not override package visible method

"A package default method is not visible in a different package, and thus cannot be overridden. When this option is enabled, the compiler will signal such scenario either as an error or a warning."

How can I trigger this Warning/Error? I'm looking for a code example.

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

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

发布评论

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

评论(4

纸伞微斜 2024-10-25 05:36:33

Foo.java:Bar.java

package foopackage;

public class Foo {
    String getString() {
        return "foo";
    }
}

package barpackage;

import foopackage.Foo;

public class Bar extends Foo {
    String getString() {
        return "bar";
    }
}

应该这样做。

来自 Eclipse 帮助文档

包默认方法在不同的包中不可见,因此不能被覆盖。启用此选项后,编译器会以错误或警告的形式发出此类情况的信号。

Foo.java:

package foopackage;

public class Foo {
    String getString() {
        return "foo";
    }
}

Bar.java:

package barpackage;

import foopackage.Foo;

public class Bar extends Foo {
    String getString() {
        return "bar";
    }
}

Should do it.

From the Eclipse Help docs:

A package default method is not visible in a different package, and thus cannot be overridden. When this option is enabled, the compiler will signal such scenario either as an error or a warning.

小情绪 2024-10-25 05:36:33
package b;

public class Foo {

    void test() {}

}

package a;

import b.Foo;

public class Bar extends Foo {

    void test() {}
}
package b;

public class Foo {

    void test() {}

}

And

package a;

import b.Foo;

public class Bar extends Foo {

    void test() {}
}
缘字诀 2024-10-25 05:36:33
package a;

public class A {
    // package protected method foo : no visibility modifier
    void foo() {
    }
}

package b;

public class B extends A {
    // although this method has the same signature as A.foo(), it doesn't 
    // override it because A.foo is not visible to B (other package)
    void foo() {
    }
}
package a;

public class A {
    // package protected method foo : no visibility modifier
    void foo() {
    }
}

package b;

public class B extends A {
    // although this method has the same signature as A.foo(), it doesn't 
    // override it because A.foo is not visible to B (other package)
    void foo() {
    }
}
忆沫 2024-10-25 05:36:33

为什么这个警告或错误很重要,请考虑以下示例:

package a;

public class Animal { 

    // Note: no visibility modifier therefore package-private
    String makeNoise() {
        return "no noise defined";
    }
}

package b;

import a.Animal;

public class Cat extends Animal {

    // Note: Eclipse will show the warning that it doesn't override
    public String makeNoise() {
        return "meow";
    }

}

package b;

import a.Animal;

public class Dog extends Animal {

    // Note: Eclipse will show the warning that it doesn't override
    public String makeNoise() {
        return "bark";
    }

}

package a;

import java.util.ArrayList;
import java.util.List;

import b.Cat;
import b.Dog;

public class Main {

    public static void main(String[] args) {
        // Make a list of Animals
        List<Animal> animals = new ArrayList<>();
        animals.add(new Cat());
        animals.add(new Dog());

        // Loop through the animals making their noises
        for (Animal animal : animals) {
            System.out.println(animal.makeNoise());
        }
    }

}

将整个内容复制粘贴到 Eclipse 中,它将整理类和包。然后运行 ​​Main 类。您可能期望它打印

meow
bark

,但它却打印

no noise defined
no noise defined

这是令人困惑的行为,意外地破坏了多态性。它这样做的原因是 Animal 子类 CatDog 实际上根本没有重写方法 makeNoise,因此,当在 Animal 上调用 makeNoise 时,它会使用 Animal 实现。

要修复此代码,请将 publicprotected 修饰符添加到 Animal makeNoise 方法,然后重新运行现在的代码按预期工作。在这个简单的示例中,很清楚发生了什么,但在更复杂的情况下,这种行为可能会非常令人困惑,并且可能是不正确的,因此应该认真对待该警告。

An example of why this warning or error is important consider the following example:

package a;

public class Animal { 

    // Note: no visibility modifier therefore package-private
    String makeNoise() {
        return "no noise defined";
    }
}

package b;

import a.Animal;

public class Cat extends Animal {

    // Note: Eclipse will show the warning that it doesn't override
    public String makeNoise() {
        return "meow";
    }

}

package b;

import a.Animal;

public class Dog extends Animal {

    // Note: Eclipse will show the warning that it doesn't override
    public String makeNoise() {
        return "bark";
    }

}

package a;

import java.util.ArrayList;
import java.util.List;

import b.Cat;
import b.Dog;

public class Main {

    public static void main(String[] args) {
        // Make a list of Animals
        List<Animal> animals = new ArrayList<>();
        animals.add(new Cat());
        animals.add(new Dog());

        // Loop through the animals making their noises
        for (Animal animal : animals) {
            System.out.println(animal.makeNoise());
        }
    }

}

Copy paste the whole thing into Eclipse and it will sort out the classes and packages. Then run the Main class. You might expect it to print

meow
bark

But instead it prints

no noise defined
no noise defined

This is confusing behaviour which unexpectedly breaks polymorphism. The reason it does this is the Animal subclasses Cat and Dog have not actually overridden the method makeNoise at all, so when makeNoise is called on an Animal it uses the Animal implementation.

To fix this code add public or protected modifiers to the Animal makeNoise method, then rerun the code it will now work as expected. In this simple example its quite clear what is going on, but in a more complicated situation this behaviour could be extremely confusing and probably incorrect so the warning should be taken seriously.

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