class People
{
private static String nationality;
String name;
int age;
public String print()
{
return "国籍 = " + nationality + ",姓名 = " + name + ",年龄 = " + age ;
}
static void setNationality(String c)
{
//System.out.println(c);
if(c.equals(null))
{
c = "未获得";
}
nationality = c ;
}
}
public class StaticTest
{
public static void main(String args[])
{
String n = "china";
People.setNationality(n);
People p1 = new People();
People p2 = new People();
p1.name = "山山";
p1.age = 22;
p2.name = "水水";
p2.age = 23;
System.out.println(p1.print());
System.out.println(p2.print());
}
}
====================code end================
代码是一个static知识点的例题
通过set方法设定全局变量已经ok,但是我希望在set方法中加入一个验证
验证“国籍”这个属性是否为空,如果为空,则显示“未获得”
不过我试了好多方法貌似实现不了,求大师指教!谢谢!
qq_笑_17
相关分类