多态类/多态/内部类 - 获取错误消息
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);
}
}
}
因此,我尝试在名为 printNumber
的 PolyMorphic
类中创建一个方法,它是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您声明内部类的方式,您的内部类需要一个
PolymorphicClass
的实例。然而,在你的情况下,你不需要这个,所以你可以将你的内部类标记为static
:这
是一个Java设计功能。
另一种解决方案是对 PolymorphicClass 的实例进行操作:
编辑:
您还需要:
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 asstatic
:and
This is a Java design feature.
One other solution would be to operate on an instance of
PolymorphicClass
:EDIT:
You also need:
不要将 MyNumber 类及其子类嵌套在多态中。
仅当嵌套类 (
MyNumber
、IntNumber
、DoubleNumber
) 是封闭类 (<代码>多态)。在您的例子中,两个类之间的唯一关系是 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?