java程序求解:帮忙解决下面的问题,谢谢!
classStudent{staticStringschoolName;StringstudentName;}publicclassStudengManager{publ...
class Student {
static String schoolName;
String studentName;
}
public class StudengManager {
public static void main(String[] args) {
Student s1 = new Student();
s1.schoolName = "guangximinzushifanxueyuan";
s1.studentName = "zhangsan";
System.out.println("s1的学校是:" + s1.schoolName);
System.out.println("s1的姓名是:" + s1.studentName);
System.out.println("---------");
Student s2 = new Student();
s2.schoolName = "rongxianzhongxue";
s2.studentName = "lisi";
System.out.println("s1的学校是:" + s1.schoolName);
System.out.println("s1的姓名是:" + s1.studentName);
System.out.println("s2的学校是:" + s2.schoolName);
System.out.println("s2的姓名是:" + s2.studentName);
}
}
为什么s1的校名一直变化??
这个是课本上解释static用法的一个例题,现在就是讨论s1的值为什么会变化。 展开
static String schoolName;
String studentName;
}
public class StudengManager {
public static void main(String[] args) {
Student s1 = new Student();
s1.schoolName = "guangximinzushifanxueyuan";
s1.studentName = "zhangsan";
System.out.println("s1的学校是:" + s1.schoolName);
System.out.println("s1的姓名是:" + s1.studentName);
System.out.println("---------");
Student s2 = new Student();
s2.schoolName = "rongxianzhongxue";
s2.studentName = "lisi";
System.out.println("s1的学校是:" + s1.schoolName);
System.out.println("s1的姓名是:" + s1.studentName);
System.out.println("s2的学校是:" + s2.schoolName);
System.out.println("s2的姓名是:" + s2.studentName);
}
}
为什么s1的校名一直变化??
这个是课本上解释static用法的一个例题,现在就是讨论s1的值为什么会变化。 展开
3个回答
展开全部
。你这写法实在是太不规范了,哪有这么写的。
参数赋值,你应该放在构造函数内。
比如
public class Student{
private String schoolName;
private String studentName;
public Student(schoolName,studentName)
this.schoolName=schoolName;
this. studentName= studentName;
}
}
同时static String schoolName,去掉static
因共享一个空间,即s1调用它和s2调用它是一样的。可以用Student.schoolName,直接调用。
s2在调用它时就会刷新s1调用时的值。。
参数赋值,你应该放在构造函数内。
比如
public class Student{
private String schoolName;
private String studentName;
public Student(schoolName,studentName)
this.schoolName=schoolName;
this. studentName= studentName;
}
}
同时static String schoolName,去掉static
因共享一个空间,即s1调用它和s2调用它是一样的。可以用Student.schoolName,直接调用。
s2在调用它时就会刷新s1调用时的值。。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
去掉第二行的static就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询