标题: 类名前的public修饰符何时用? [打印本页] 作者: 张成龙 时间: 2012-5-8 13:20 标题: 类名前的public修饰符何时用? public class Student//java:1: 错误: 类Student是公共的, 应在名为 Student.java 的文件中声明
public class Student
{
private String name;
public Student(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
public class TestDemo
{
public static void main(String[] args)
{
Student[] s = new Student[2];//定义Student数组对象
s[0] = new Student("张三");
s[1] = new Student("李四");
int i=0;
for(Student st : s)
{
System.out.println("姓名:"+st.getName()+ i++);
}
}
}