java 封装练习题! 求解答!
(封装)已知一个类Student代码如下:classStudent{Stringname;intage;Stringaddress;StringzipCode;Strin...
(封装)已知一个类 Student 代码如下:
class Student{
String name;
int age;
String address;
String zipCode;
String mobile;要求:1、把Student 的属性都作为私有,并提供get/set 方法以及适当的构造方法。
2、为Student 类添加一个getPostAddress 方法,要求返回Student 对象的地址和邮编 展开
class Student{
String name;
int age;
String address;
String zipCode;
String mobile;要求:1、把Student 的属性都作为私有,并提供get/set 方法以及适当的构造方法。
2、为Student 类添加一个getPostAddress 方法,要求返回Student 对象的地址和邮编 展开
1个回答
2013-08-25
展开全部
public class Student {
private String name;
private int age;
private String address;
private String zipCode;
private String mobile;
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;
}
public String[] getPostAddress() {
String[] array = new String[2];
array[0] = this.address;
array[1] = this.zipCode;
return array;
}
}
private String name;
private int age;
private String address;
private String zipCode;
private String mobile;
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;
}
public String[] getPostAddress() {
String[] array = new String[2];
array[0] = this.address;
array[1] = this.zipCode;
return array;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询