/* (封装)已知一个类Student代码如下: class Student1 { String name; int age; String address; String zipCode; String mobile; } 要求: 1、把Student 的属性都私有化,并提供get/set方法以及适当的构造方法 2、为Student 类添加一个getPostAddress方法,要求返回Student对象的地址和邮编 */ class Student1{ private String name; private int age; private String address; private String zipCode; private String mobile; // 无参构造方法 public Student1() { } // 有参构造方法 public Student1(String name, int age, String address, String zipCode, String mobile) { this.name = name; this.age = age; this.address = address; this.zipCode = zipCode; this.mobile = mobile; } // set and get 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; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } // getPostAddress方法,要求返回Student对象的地址和邮编 public String getPostAddress(){ return "地址:" + this.getAddress() + ",邮编:" + this.getZipCode(); } } class Student1Test { public static void main(String[] args) { // 使用无参构造方法创建一个学生对象 Student1 s = new Student1(); // 输出上面学生对象的姓名家庭住址和邮编 System.out.println(s.getName() + s.getPostAddress()); // 为上面的学生对象赋值 s.setName("张三"); s.setAddress("河北省秦皇岛市"); s.setZipCode("066300"); System.out.println(s.getName() + s.getPostAddress()); } }