forked from shiveeg1/JavaExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
37 lines (29 loc) · 721 Bytes
/
Person.java
File metadata and controls
37 lines (29 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package ToStringExample;
// Here is an example of a simple class called 'Person'. It has two attributes and four methods.
// Note that the attributes are declared private so that they can only be accessed by the methods,
// not by anything outside the class.
class Person {
private String name;
private int age;
public Person(String n, int a) {
this.name = n;
this.age = a;
}
int getAge() {
return this.age;
}
String getName() {
return this.name;
}
void setAge(int a) {
this.age = a;
}
void setName(String n) {
this.name = n;
}
// This is the toString method that overrides the default.
@Override
public String toString() {
return ("Name: " + this.name + " Age: " + this.age);
}
}