+ 1
getter and setter example?
3 Antworten
+ 16
public class Person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
// down here an example for you to try the setters and getters
public class PersonTest {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
person1.setName("R2-D2");
person1.setAge(40);
person2.setName("Yoda");
person2.setAge(900);
System.out.println("person1 name is " + person1.getName());
System.out.println("person1 age is " + person1.getAge());
System.out.println("person2 name is " + person2.getName());
System.out.println("person2 age is " + person2.getAge());
}
}
0
thank you
0
0