public class SimpleGetterSetter {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
SimpleGetterSetter obj = new SimpleGetterSetter();
// возникнет ошибка компиляции, т.к. number является private
obj.number = 10;
// то же самое
int number = obj.number;
SimpleGetterSetter obj = new SimpleGetterSetter();
obj.setNumber(10); // ок
int number = obj.getNumber(); // ок
public class Employee {
public String name;
public int retirementAge;
public Employee(String name, int retirementAge) {
this.name = name;
this.retirementAge = retirementAge;
}
}
public class RetirementAgeModifier {
public static void main(String[] args) {
Employee employee = new Employee("John", 58);
employee.retirementAge = 18;
}
}
public void setNumber(int number) {
if (number < 10 || number > 100) {
throw new IllegalArgumentException();
}
this.number = number;
}
obj.number = 3;
public int getNumber() {
return number;
}
private String name;
public void setName(String name)
public String getName()
private boolean single;
public String isSingle()
public void setSingle(boolean single)
public String firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
private String firstName;
private int[] scores;
public void setScores(int[] scores) {
this.scores = scores;
}
int[] myScores = {5, 5, 4, 3, 2, 4}; // 1
setScores(myScores); // 2
displayScores(); // 3
myScores[1] = 1; // 4
displayScores(); // 5
public void displayScores() {
for (int number : scores) {
System.out.print(number + " ");
}
System.out.println();
}
public void setScores(int[] scores) {
this.scores = scores;
}
public void setScores(int[] scores) {
this.scores = new int[scores.length];
System.arraycopy(scores, 0, this.scores, 0, scores.length);
}
myScores[1] = 1;
public void setScores(int[] scores) {
this.scores = Arrays.copyOf(scores, scores.length);
}
private int[] scores;
public int[] getScores() {
return scores;
}
int[] myScores = {5, 5, 4, 3, 2, 4}; // 1
setScores(myScores) // 2
displayScores(); // 3
int[] copyScores = getScores(); // 4
copyScores[1] = 1; // 5
displayScores(); // 6
public int[] getScores() {
int[] copyScores = new int[scores.length];
System.arraycopy(scores, 0, copyScores, 0, copyScores.length);
return copyScores;
}
public int[] getScores() {
return Arrays.copyOf(scores, scores.length);
}
private float amount;
public void setAmount(float amount) {
this.amount = amount;
}
public float getAmount() {
return amount;
}
private String address;
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
private Date birthDate;
public void setBirthDate(Date birthDate) {
this.birthDate = new Date(birthDate.getTime());
}
public Date getBirthDate() {
return new Date(birthDate.getTime();
}
private List<String> listTitles;
public void setListTitles(List<String> listTitles) {
this.listTitles = listTitles;
}
public List<String> getListTitles() {
return listTitles;
}
import java.util.*;
public class CollectionGetterSetter {
private List<String> listTitles;
public void setListTitles(List<String> listTitles) {
this.listTitles = listTitles;
}
public List<String> getListTitles() {
return listTitles;
}
public static void main(String[] args) {
CollectionGetterSetter app = new CollectionGetterSetter();
List<String> titles1 = new ArrayList();
titles1.add("Name");
titles1.add("Address");
titles1.add("Email");
titles1.add("Job");
app.setListTitles(titles1);
System.out.println("Titles 1: " + titles1);
titles1.set(2, "Habilitation");
List<String> titles2 = app.getListTitles();
System.out.println("Titles 2: " + titles2);
titles2.set(0, "Full name");
List<String> titles3 = app.getListTitles();
System.out.println("Titles 3: " + titles3);
}
}
public void setListTitles(List<String> listTitles) {
this.listTitles = new ArrayList<>(listTitles);
}
public List<String> getListTitles() {
return new ArrayList<>(listTitles);
}
import java.util.*;
public class CollectionGetterSetterObject {
private List<Person> listPeople;
public void setListPeople(List<Person> listPeople) {
this.listPeople = new ArrayList<>(listPeople);
}
public List<Person> getListPeople() {
return new ArrayList<>(listPeople);
}
public static void main(String[] args) {
CollectionGetterSetterObject app = new CollectionGetterSetterObject();
List<Person> list1 = new ArrayList<>();
list1.add(new Person("Peter"));
list1.add(new Person("Alice"));
list1.add(new Person("Mary"));
app.setListPeople(list1);
System.out.println("List 1: " + list1);
list1.get(2).setName("Maryland");
List<Person> list2 = app.getListPeople();
System.out.println("List 2: " + list2);
list1.get(0).setName("Peter Crouch");
List<Person> list3 = app.getListPeople();
System.out.println("List 3: " + list3);
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied)
public Object clone()
public Object clone() {
return new Person(name);
}
public void setListPeople(List<Person> listPeople) {
for (Person person : listPeople) {
this.listPeople.add((Person) person.clone());
}
}
public List<Person> getListPeople() {
List<Person> listPeople = new ArrayList<>();
for (Person person : this.listPeople) {
listPeople.add((Person) person.clone());
}
return listPeople;
}
import java.util.*;
public class CollectionGetterSetterObject {
private List<Person> listPeople = new ArrayList<>();
public void setListPeople(List<Person> listPeople) {
for (Person person : listPeople) {
this.listPeople.add((Person) person.clone());
}
}
public List<Person> getListPeople() {
List<Person> listPeople = new ArrayList<>();
for (Person person : this.listPeople) {
listPeople.add((Person) person.clone());
}
return listPeople;
}
public static void main(String[] args) {
CollectionGetterSetterObject app = new CollectionGetterSetterObject();
List<Person> list1 = new ArrayList<>();
list1.add(new Person("Peter"));
list1.add(new Person("Alice"));
list1.add(new Person("Mary"));
app.setListPeople(list1);
System.out.println("List 1: " + list1);
list1.get(2).setName("Maryland");
List<Person> list2 = app.getListPeople();
System.out.println("List 2: " + list2);
list1.get(0).setName("Peter Crouch");
List<Person> list3 = app.getListPeople();
System.out.println("List 3: " + list3);
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
public Object clone() {
return new Person(name);
}
}
public void setFriend(Person friend) {
this.friend = (Person) friend.clone();
}
public Person getFriend() {
return (Person) friend.clone();
}