我是编程新手,本周已经开始使用java。我正在学习面向对象。这是我的问题:
public class Person
{
int age = 0;
String name = "John Doe";
void display{
System.out.println(name + " is " + age + " years old. ");
}
}
public class Driver
{
public static void main(String[] args)
{
Person mark = new Person();
mark.age = 25;
mark.name = "Mark";
display;
}
}
但它一直在说
语法错误,插入' VariableDeclarator'
答案 0 :(得分:1)
display
是类Person
的一部分,必须具有正确的语法(可见性 - 返回类型 - 名称 - 参数)。Person
对象mark
调用显示方法。否则,该方法将无法解析为编译器。最后但并非最不重要......你必须为Person创建自己的类,因为在一个源文件中不可能有两个公共类,或者你必须降低Driver类的可见性。
package de.db.ri.as.gleisansagen.evaluator;
public class Person {
int age = 0;
String name = "John Doe";
void display() {
System.out.println(name + " is " + age + " years old. ");
}
}
class Driver {
public static void main(String[] args) {
Person mark = new Person();
mark.age = 25;
mark.name = "Mark";
mark.display();
}
}
答案 1 :(得分:-2)
不要编写自己的语法程序,首先学习java语法,然后开始编码 https://docs.oracle.com/javase/specs/
public class Person {
int age = 0;
String name = "John Doe";
void display() {
System.out.println(name + " is " + age + " years old. ");
}
}
public class Driver {
public static void main(String[] args) {
Person mark = new Person();
mark.age = 25;
mark.name = "Mark";
mark.display();
}
}