黑马程序员技术交流社区
标题:
static静态有个问题,如果没有国籍那个是该输出美国还是...
[打印本页]
作者:
莫里亚蒂
时间:
2015-4-25 14:07
标题:
static静态有个问题,如果没有国籍那个是该输出美国还是...
class Person
{
/**姓名*/
private String name;
/**年龄*/
private int age;
/**国家*/
private static String country;
public Person()
{
}
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public Person(String name,int age,String country)
{
this.name=name;
this.age=age;
this.country=country;
}
/**
获取国籍
*/
public String getCountry() {
return country;
}
/**
设置国籍
*/
public void setCountry(String country)
{
this.country = country;
}
/**
获取姓名
*/
public String getName() {
return name;
}
/**
设置姓名
*/
public void setName(String name) {
this.name = name;
}
/**
获取年龄
*/
public int getAge() {
return age;
}
/**
设置年龄
*/
public void setAge(int age) {
this.age = age;
}
/**show方法主要用以输出名字,年龄,国籍*/
public void show()
{
System.out.println(name+"~~~"+age+"~~~"+country);
}
}
public class PersonTest
{
public static void main(String[] args)
{
Person p=new Person("小哲",23,"中国");
p.show();
Person p2=new Person("小情",24,"美国");
p2.show();
Person p3=new Person("小玥",23);
p3.show();
Person p4=new Person("小芳",23,"中国");
p4.show();
//Person p5=new Person("小含",23,"美国");
//p5.show();
}
}
作者:
谷歌
时间:
2015-4-25 15:29
static变量应该算这几个对象共有的。所以你后面不写,那么它就会使用前面设置的那个。但是楼主这里对于static变量的使用不太妥。
作者:
秋天-10
时间:
2015-4-25 20:59
这里的国籍定义为static有点不合适,因为static定义的是变量应该是公用的,先与对象产生,你这里又是中国又是美国的,不应该是定义为static。
作者:
mxdeheima
时间:
2015-4-25 21:17
楼主static的用法不大妥当
作者:
andre
时间:
2015-4-25 21:46
小白 来学习学习下
作者:
lclxjzz
时间:
2015-4-25 21:53
看看···············
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2