package com.hspedu.encap;
public class Encapsulation01 {
public static void main(String[] args) {
Person person = new Person();
person.setName("Jackkkkkkkkkkkkkkku");
person.setAge(30);
person.setSalary(250000);
System.out.println(person.info());
System.out.println(person.getSalary());
}
}
class Person {
public String name;
private int age;
private double salary;
public String getName() {
return name;
}
public void setName(String name) {
if(name.length() >= 2 && name.length() <= 6){
this.name = name;
} else {
System.out.println("名字长度得在2-6个字符,默认名字");
this.name = "无名人";
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age >= 1 && age <= 120){
this.age = age;
} else {
System.out.println("你设置的年龄不对!需要在(1-120),给默认年龄18");
this.age = 18;
}
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String info() {
return "信息为 name=" + name + " age =" +age + " 薪水=" + salary;
}
}