写一个worker类,添加int,String类型属性,再写一个Address类,添加两个String类型属性,为第一个Worker类添加Address类型属性,创建一个work对象
时间: 2024-01-23 14:03:33 浏览: 91
一个ASGI Gunicorn worker 类-python
下面是实现这个要求的Java代码:
```
public class Worker {
private int id;
private String name;
private Address address;
public Worker(int id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
// Getters and setters for all properties
}
public class Address {
private String street;
private String city;
public Address(String street, String city) {
this.street = street;
this.city = city;
}
// Getters and setters for all properties
}
public class Main {
public static void main(String[] args) {
Address address = new Address("123 Main St", "New York");
Worker worker = new Worker(1, "John", address);
}
}
```
在上面的代码中,我们定义了一个Worker类和一个Address类。Worker类有三个属性:id,name和address,其中address是Address类型。Address类有两个属性:street和city。在main方法中,我们创建了一个Address对象和一个Worker对象,将Address对象作为参数传递给Worker构造函数。这样,我们就为Worker对象添加了一个Address类型属性。
阅读全文