没有方法main?

viqkga123 2010-04-27 04:39:24
public class Person
{
static int count =0;
protected String name;
protected int age;
public Person(String n1,int a1)
{
name=n1;
age=a1;
this.count++;
}
public String toString()
{
return this.name+","+this.age;
}
public void display()
{
System.out.print("本类名="+this.getClass().getName()+";");
System.out.println("父类名="+this.getClass().getSuperclass().getName());
System.out.print("Person.count="+this.count+" ");
System.out.print("Student.count="+Student.count+" ");
Object obj=this;
if(obj instanceof Student)
System.out.println(obj.toString()+"是Student类对象");
else if(obj instanceof Person)
System.out.println(obj.toString()+"是Person类对象");
}
}
class Student extends Person
{
static int count=0;
protected String dept;
protected Student (String n1,int a1,String d1)
{
super(n1,a1);
dept=d1;
this.count++;
}
public String toString()
{
return super.toString()+","+dept;
}
public void display()
{
super.display();
System.out.print("super.count="+super.count);
System.out.println(" ;this.count="+this.count);
}
public static void main(String args[])
{
Person per=new Person("王永涛",23);
per.display();
Student stu = new Student("张小三",22,"计算机系");
stu.display();
}
}
为什么会出现错误 exception in thread "main" java.lang.NoSuchMethodError:main
...全文
204 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
libitum_1 2010-05-02
  • 打赏
  • 举报
回复
学习 学习 学习
wufengye12 2010-05-01
  • 打赏
  • 举报
回复
xuexi
zhengmj777 2010-05-01
  • 打赏
  • 举报
回复
顶一楼!!!
zawy55 2010-05-01
  • 打赏
  • 举报
回复
同意2楼的说法
Mars_Ma_OK 2010-04-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 closewbq 的回复:]
写到一个类里面了?
那把main函数提到Person类中。
要不就分开写。
[/Quote]
up
24K純帥 2010-04-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 closewbq 的回复:]
写到一个类里面了?
那把main函数提到Person类中。
要不就分开写。
[/Quote]
对头
viqkga123 2010-04-27
  • 打赏
  • 举报
回复
谢了,各位。。放到Person类中确实可以了
不过主方法一定要放在public类中吗?
Student类不是继承了Person类了吗?放到里面却不行了?
qiaopeizhan 2010-04-27
  • 打赏
  • 举报
回复
本类名=javaapplication3.Person;父类名=java.lang.Object
Person.count=1 Student.count=0 王永涛,23是Person类对象
本类名=javaapplication3.Student;父类名=javaapplication3.Person
Person.count=2 Student.count=1 张小三,22,计算机系是Student类对象
super.count=2 ;this.count=1
jackmtlee 2010-04-27
  • 打赏
  • 举报
回复
main必须位于public类里面。
当然可以不用main()方法,楼主可以将main中代码写到一个
static{...}中,同样可以执行
tanxd39 2010-04-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 closewbq 的回复:]

写到一个类里面了?
那把main函数提到Person类中。
要不就分开写。
[/Quote]

顶一个
closewbq 2010-04-27
  • 打赏
  • 举报
回复
写到一个类里面了?
那把main函数提到Person类中。
要不就分开写。
  • 打赏
  • 举报
回复
你的public class Person 和class Student extends Person

public写反了。。。。。。

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧