List<Person>): Person { var maxAge = 0 var theOldest: Person? = null for (person in people) { if (person.age > maxAge) { maxAge = person.age theOldest = person } } return theOldest }
private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public final String getName() { return this.name; } public final int getAge() { return this.age; } public final Person copy(String name, int age) { return new Person(name, age); } public String toString() { return "Person(name=" + this.name + ", age=" + this.age + ")"; } public int hashCode() { return (this.name != null?this.name.hashCode():0) * 31 + this.age; } public boolean equals(Object var1) { //… } }
>>> Person person = new Person("Bob", true); >>> System.out.println(person.getName()); Bob >>> System.out.println(person.isMarried()); true person.setName("Alice") //Error! person.setMarried(false) //OK!