Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active December 19, 2021 10:17
Show Gist options
  • Save sunmeat/26b2fa11fbca5803ebdc to your computer and use it in GitHub Desktop.
Save sunmeat/26b2fa11fbca5803ebdc to your computer and use it in GitHub Desktop.
person encapsulation
package com.alex.encapsulation;
import java.util.Date;
class Person {
private String name;
private Date birthday;
private boolean knowJava;
public String getName() {
return name;
}
public void setName(String name) /*throws Exception*/ {
//if(name.equalsIgnoreCase("Вася")) throw new Exception("недопустимое имя... :)");
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(int day, int month, int year) {
this.birthday = new Date(year, month, day);
}
public boolean isKnowJava() {
return knowJava;
}
public void setKnowJava(boolean knowJava) {
this.knowJava = knowJava;
}
public Person() {
setName("Ivan");
setBirthday(1, 1, 1970);
setKnowJava(false);
}
}
class Program {
public static void main(String[] args) {
Person p = new Person();
p.setName("Alex");
p.setBirthday(10, 3, 1989);
p.setKnowJava(true);
System.out.println(p.getName());
System.out.println(p.getBirthday());
System.out.println(p.isKnowJava());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment