A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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();
        }
}

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

5 个回复

正序浏览
看看···············
回复 使用道具 举报
小白 来学习学习下
回复 使用道具 举报
楼主static的用法不大妥当
回复 使用道具 举报
这里的国籍定义为static有点不合适,因为static定义的是变量应该是公用的,先与对象产生,你这里又是中国又是美国的,不应该是定义为static。
回复 使用道具 举报
static变量应该算这几个对象共有的。所以你后面不写,那么它就会使用前面设置的那个。但是楼主这里对于static变量的使用不太妥。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马