多态类/多态/内部类 - 获取错误消息

发布于 2024-12-21 09:56:52 字数 1480 浏览 1 评论 0原文

public class PolyMorphic {

public static void main(String[] args) {

    PolyMorphic.printNumber(new IntNumber(1));
    PolyMorphic.printNumber(new DoubleNumber(4.54));

}

public static void printNumber(MyNumber N) {
    N.print(N);
    System.out.println();
}

public abstract class MyNumber{
    abstract void print(MyNumber N);
}

public class IntNumber extends MyNumber{
    int x;

    IntNumber(){
        x = 3;
    }

    IntNumber(int x){
        this.x = x;
    }


    void print(MyNumber N) {
        double temp = (double)x;
        System.out.printf("%.2f",temp);

    }
}


public class DoubleNumber extends MyNumber{
    double x;

    DoubleNumber(){
        x = 3.23;
    }

    DoubleNumber(double x){
        this.x = x;
    }


    void print(MyNumber N) {
        double temp = x;
        System.out.printf("%.2f",temp);

    }
}

 }

因此,我尝试在名为 printNumberPolyMorphic 类中创建一个方法,它是 polymorphic 并且可以打印(到控制台)一个 intNumber 与右侧两位小数或右侧三位小数的 DoubleNumber。如 PolyMorphic.printNumber(new IntNumber(1));

我的问题是这样的:

在线:

PolyMorphic.printNumber(new IntNUmber(1));
PolyMorphic.printNumber(new DoubleNumber(4.54));

这是错误消息:

" 没有可访问的 PolyMorphic 类型的封闭实例。必须 使用 PolyMorphic 类型的封闭实例限定分配 (例如xnew A(),其中x是PolyMorphic的实例)。”

它为我提供了这两个实例,但我很困惑为什么它不起作用。如果有人能指出我正确的方向,我将非常感激。

谢谢。

public class PolyMorphic {

public static void main(String[] args) {

    PolyMorphic.printNumber(new IntNumber(1));
    PolyMorphic.printNumber(new DoubleNumber(4.54));

}

public static void printNumber(MyNumber N) {
    N.print(N);
    System.out.println();
}

public abstract class MyNumber{
    abstract void print(MyNumber N);
}

public class IntNumber extends MyNumber{
    int x;

    IntNumber(){
        x = 3;
    }

    IntNumber(int x){
        this.x = x;
    }


    void print(MyNumber N) {
        double temp = (double)x;
        System.out.printf("%.2f",temp);

    }
}


public class DoubleNumber extends MyNumber{
    double x;

    DoubleNumber(){
        x = 3.23;
    }

    DoubleNumber(double x){
        this.x = x;
    }


    void print(MyNumber N) {
        double temp = x;
        System.out.printf("%.2f",temp);

    }
}

 }

So I am trying to create a method in the PolyMorphic class named printNumber which is polymorphic and can print(to the console) either an intNumber with two decimal places to the right or a DoubleNumber with three decimal places to the right. Such as PolyMorphic.printNumber(new IntNumber(1));

My Problem is this:

On the Lines:

PolyMorphic.printNumber(new IntNUmber(1));
PolyMorphic.printNumber(new DoubleNumber(4.54));

This is the error message:

" No enclosing instance of type PolyMorphic is accessible. Must
qualify the allocation with an enclosing instance of type PolyMorphic
(e.g. x.new A() where x is an instance of PolyMorphic)."

It gives me it for both instances and I am confused to as why It is not working. IF someone could just point me in the right direction I would be really appreciative.

Thank you.

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

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

发布评论

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

评论(2

乖乖公主 2024-12-28 09:56:52

由于您声明内部类的方式,您的内部类需要一个 PolymorphicClass 的实例。然而,在你的情况下,你不需要这个,所以你可以将你的内部类标记为static

public static class IntNumber

public static class DoubleNumber

是一个Java设计功能。

另一种解决方案是对 PolymorphicClass 的实例进行操作:

Polymorphic p = new Polymorphic();
p.printNumber(new IntNumber(1));
p.printNumber(new DoubleNumber(4.54));

编辑:

您还需要:

public static abstract class MyNumber

Your inner classes require an instance of your PolymorphicClass because of the way you declared them. However, in your case, you don't need this, so you can mark your inner classes as static:

public static class IntNumber

and

public static class DoubleNumber

This is a Java design feature.

One other solution would be to operate on an instance of PolymorphicClass:

Polymorphic p = new Polymorphic();
p.printNumber(new IntNumber(1));
p.printNumber(new DoubleNumber(4.54));

EDIT:

You also need:

public static abstract class MyNumber
杀お生予夺 2024-12-28 09:56:52

不要将 MyNumber 类及其子类嵌套在多态中。

仅当嵌套类 (MyNumberIntNumberDoubleNumber) 是封闭类 (<代码>多态)。在您的例子中,两个类之间的唯一关系是 Polymorphic 正在调用 Number 类上的方法。

顺便说一句,编译器已经告诉了您问题的一种解决方案,如果您愿意花时间阅读并理解它所说的内容的话。要心存感激,因为很少有编译器能像我们这样乐于助人。

编辑 - 为什么有人对既正确又添加其他答案提供的附加信息的回复投反对票?

Don't nest your MyNumber class and its daughters inside of Polymorphic.

Nesting classes like that is only appropriate when the nested class (MyNumber, IntNumber, DoubleNumber) is part of the implementation of the enclosing class (Polymorphic). In your case, the only relationship between the two classes is that Polymorphic is calling methods on the Number classes.

By the way, the compiler has already told you one solution to your problem, if you would take the trouble to read and understand what it said. Be grateful, for few compilers are as obliging.

Edit - why is anyone downvoting a reply that is both correct and adds additional information to that provided by other answers?

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