public class Demo2 { class Person{ //定义类型 private String name; private String age; private String sex; private String xueHao; private String ID;//私有数据成员 private String habit; public Person(){ } public Person(String name,String age,String sex,String xueHao,String ID,String habit){ this.name=name; this.age=age; this.sex=sex; this.xueHao=xueHao; this.ID=ID; this.habit=habit; } public String getName(){ return name; //返回姓名 } public void setName(String name) { this.name = name; } public String getAge() { return age; //返回年龄 } public void setAge(String age) { this.age = age; } public String getSex() { return sex; //返回性别 } public void setSex(String sex) { this.sex = sex; } public String getXueHao() { return xueHao; //返回学号 } public void setXueHao(String xueHao) { this.xueHao = xueHao; } public String getID() { return ID; //返回ID } public void setID(String ID) { this.ID = ID; } public String getHabit(){ return habit; //返回爱好 } public void setHabit(){ this.habit=habit; } } public static void main(String[] args) { Demo2 Man = new Demo2(); Person man = Man.new Person("小坤", "20", "男", "547547547547", "547547547547","唱,跳,rap,篮球"); man.setName("小鸡"); //修改姓名 System.out.println("姓名:"+man.getName()); System.out.println("年龄:"+man.getAge()); System.out.println("性别:"+man.getSex()); System.out.println("学号:"+man.getXueHao()); System.out.println("身份证号:"+man.getID()); System.out.println("爱好:"+man.getHabit()); } }
编译为:
姓名:小鸡
年龄:20
性别:男
学号:547547547547
身份证号:547547547547
爱好:唱,跳,rap,篮球