Page 158 - AP Computer Science A, 7th edition
P. 158

parameters, whereas obj is an implicit parameter. Example 2
Here’s an example where this is used as a parameter.
public class Person {
private String name; private int age;
public Person(String aName, int anAge) {
name = aName;
age = anAge; }
/∗ ∗ @return the String form of this public String toString()
{ return name + " " + age; }
public void printPerson()
{ System.out.println(this); }
person
∗ /
//Other variables and methods are not shown. }
Suppose a client class has these lines of code:
Person p = new Person("Dan", 10); p.printPerson();
The statement
System.out.println(this);
in t he printPerson m et hod m eans “print
t he
10. Note that
Person System.out.println invokes the toString method of the Person
object.” The output should be: Dan
class.
Example 3
c urrent









































































   156   157   158   159   160