用java编程:设计一个Worker类和Address类,类的要求如下: (1)定义一个Worker类,类中定义三个属性:姓名、年龄、工资(salary),要求如下: ①提供公开无参构造方法和三个参数的构造方法 2 定义无参的work方法和带整数参数work(inthours)方法,参数代表工人工作的小时数
时间: 2024-03-08 17:45:52 浏览: 70
好的,这里是Java代码实现:
```
// Address类
public class Address {
private String street;
private String city;
private String state;
private int zipCode;
public Address(String street, String city, String state, int zipCode) {
this.street = street;
this.city = city;
this.state = state;
this.zipCode = zipCode;
}
// getter和setter方法
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getZipCode() {
return zipCode;
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
}
// Worker类
public class Worker {
private String name;
private int age;
private double salary;
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(name + " is working.");
}
public void work(int hours) {
System.out.println(name + " is working for " + hours + " hours.");
}
// getter和setter方法
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
```
以上代码中,Address类表示地址信息,Worker类表示一个工人,包含姓名、年龄、工资三个属性,还有work和work(int hours)两个方法。其中work方法表示工人工作,不带参数;work(int hours)方法表示工人工作,带一个整数参数,表示工人工作的小时数。这里我们使用了构造方法和getter、setter方法来初始化和获取属性值。
阅读全文