设计一个Worker类和Adderss类,要求如下:
1.定义一个Worker类,类中定义三个属性:姓名,年龄,工资,要求:
(1)提供公开无构造方法和三个参数的构造方法
(2)定义无参的work方法和带有整数参数的work(int hours)方法,参数代表工人小时数
2.定义一个Address类,类中定义属性:地址(addr)、邮编(zipCode),为此类提供无参和有参数的构造方法
3.为Worker类添加一个Address类型的属性address
4.定义一个测试类,创建一个Worker对象,其姓名为张三,年龄为26,工资为100000.0;
家庭住址为:北京市海定区清华园1号院,邮编为:100010
public class WorkerAddressTest {
public static void main(String[] args) {
Worker worker = new Worker("张三",26,10000.0);
System.out.println( worker.toString());
worker.work();
worker.work(3);
Address address=new Address();
address.addr="北京市海定区清华园2号院";
System.out.println(address.toString());
}
}
class Worker{
String name;
int age;
double salary;
Address add=new Address();
String address=add.addr;
public Worker() {
}
public Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public void work(){
System.out.println("调用无参work方法");
}
public void work(int hours){
System.out.println("调用有参work方法,工作了"+hours+"小时");
}
@Override
public String toString() {
return "Worker{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", add=" + add +
", address='" + address + '\'' +
'}';
}
}
class Address{
String addr;
int zipCode;
public Address() {
addr="北京市海定区清华园1号院";
zipCode=100010;
}
public Address(String addr, int zipCode) {
this.addr = addr;
this.zipCode = zipCode;
}
@Override
public String toString() {
return "Address{" +
"addr='" + addr + '\'' +
", zipCode=" + zipCode +
'}';
}
}