下面这个程序是练习static关键字的,主要是体现了static关键字的“可以被所有对象共享”这一特性。
class StaticDeno
{
public static void main(String[] args)
{
Static s1 = new Static("张三",45,"中国");
Static s2 = new Static("李四",40);
Static s3 = new Static("王五",50);
s1.show();
s2.show();
s3.show();
}
}
class Static
{
private String name;
private int age;
private static String country;
public Static(){}
public Static(String name,int age)
{
this.name = name;
this.age = age;
}
public Static(String name,int age,String country)
{
this.name = name;
this.age = age;
this.country = country;
}
public void show()
{
System.out.println(name+"***"+age+"***"+country);
}
}
输出的结果为:
|
|