package chapter08;
public class AccountTest {
public static void main(String[] args){
Account A = new Account();
A.setBalance(33);
A.setName("xjhhhh");
A.setPwd("43012g4");
System.out.println(A.info());
Account B = new Account("谢纪",34.2 ,"111222");
System.out.println(B.info());
}
}
package chapter08;
public class Encapsulation {
public static void main(String[] args){
Person p1 = new Person();
p1.setAge(180);
p1.setName("xjhhhhhh");
p1.setSalary(30000);
System.out.println(p1.info());
}
}
class Person{
public String name;
private int age;
private double salary;
public String getName(String name) {
return name;
}
public void setName(String name) {
if(name.length() >= 2 && name.length() <= 6){
this.name = name;
}else {
System.out.println("输入名字长度错误");
this.name = "无名小卒";
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age >= 1 && age <= 120){
this.age = age;
}else{
System.out.println("输入年龄不符合范围");
this.age = 18;
}
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String info(){
return name + " " + age + " " +salary;
}
}
package chapter08;
public class Encapsulation {
public static void main(String[] args){
Person p1 = new Person();
p1.setAge(180);
p1.setName("xjhhhhhh");
p1.setSalary(30000);
System.out.println(p1.info());
}
}
class Person{
public String name;
private int age;
private double salary;
public String getName(String name) {
return name;
}
public void setName(String name) {
if(name.length() >= 2 && name.length() <= 6){
this.name = name;
}else {
System.out.println("输入名字长度错误");
this.name = "无名小卒";
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age >= 1 && age <= 120){
this.age = age;
}else{
System.out.println("输入年龄不符合范围");
this.age = 18;
}
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String info(){
return name + " " + age + " " +salary;
}
}