【发布时间】:2023-03-24 17:18:01
【问题描述】:
我是 Java 新手,正在尝试编写一个简单的程序来帮助我进一步了解面向对象的编程。
我决定做一个电话程序。但是,在以下程序的第 5 行,我尝试创建电话类的实例,但出现以下错误:
“没有 OOPTutorial 类型的封闭实例可访问。必须使用 OOPTutorial 类型的封闭实例限定分配(例如x.new A(),其中x 是OOPTutorial 的实例)。”
这是程序:
public class OOPTutorial {
public static void main (String[] args){
phone myMobile = new phone(); // <-- here's the error
myMobile.powerOn();
myMobile.inputNumber(353851234);
myMobile.dial();
}
public class phone{
boolean poweredOn = false;
int currentDialingNumber;
void powerOn(){
poweredOn = true;
System.out.println("Hello");
}
void powerOff(){
poweredOn = false;
System.out.println("Goodbye");
}
void inputNumber(int num){
currentDialingNumber = num;
}
void dial(){
System.out.print("Dialing: " + currentDialingNumber);
}
}
}
【问题讨论】:
-
[off topic ]:你应该使用 UppercaseStartingCamelCase 命名你的类。也就是说,您的“电话”类应该是“电话”。
标签: java