chapter08

package chapter08;

public class Account {
    private String name;
    private double balance;
    private String pwd;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(name.length() >= 2 && name.length() <= 4){
            this.name = name;
        }else {
            System.out.println("输入长度不在范围内");
        }

    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        if(balance > 20){
            this.balance = balance;
        }else {
            System.out.println("余额不足");
        }

    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        if(pwd.length() == 6){
            this.pwd = pwd;
        }else {
            System.out.println("输入密码错误");
        }

    }
//自己写的时候忘记写构造器了
    public Account() {
    }

    public Account(String name, double balance, String pwd) {
        this.setName(name);
        this.setBalance(balance);
        this.setPwd(pwd);
    }

    public String info(){
        return name + " " + balance + " " + pwd;
    }




}

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值