在Java中,set
方法用于设置对象的属性值。它可以设置各种类型的属性,包括但不限于以下几种:
以下是一些示例:
public class Person {
private String name;
private int age;
private boolean active;
private String[] hobbies;
private CustomClass customObject;
// setters for the properties
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setActive(boolean active) {
this.active = active;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public void setCustomObject(CustomClass customObject) {
this.customObject = customObject;
}
}
在这个示例中,我们定义了一个Person
类,它具有各种类型的属性,并为每个属性提供了相应的set
方法。