class Student
{
static String name;
static String country = "国";
public Student(String name,String country)
{
this.name = name;
this.country = country;
}
}
class hehei
{
public static void main(String[] args)
{
Student c2 = new Student("娃一","韩国");
Student c1 = new Student("娃二","美国");
// System.out.println("国籍:"+ Student.country);
System.out.println("名字:"+ c1.name+ c1.country);
System.out.println("名字:"+ c2.name+ c2.country);
}
}
问题一、请问怎样才能使,娃一韩国,娃二美国都出现哪。
问题二、为什么娃二会把娃一覆盖哪。
把name和country改为public不要用静态的,因为静态的变量是所有类的实例所共用的。内存中分配一份内存来存放静态数据。
你定义的都是static,所以所有的对象都是用相同的name,country,
把name和国家的static修饰去掉即可,因为同一个类的静态属性和方法会被这个类的所有实例对象共享。
谢谢以上各位 了 ,都采纳了!