封装(Getter and Setter)

文章展示了用Java编程语言创建了一个名为Cats的类,该类封装了猫咪的名字、年龄、种类和食物等属性,并定义了吃饭、睡觉和发狂的行为。在ApplicationRun类中,创建了两个Cats对象并设置了属性,然后调用了它们的行为方法。
摘要由CSDN通过智能技术生成

 

Cats.java

public class Cats {
    /*
    @该类有4个变量,分别对变量进行封装,包括:
    @名字;
    @年龄;
    @种类;
    @食物:
     */
    private String name;
    private int age;
    private String variety;
    private String food;
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age < 0 || age >30) {
            System.out.println("0");
        }else {
            this.age = age;
        }
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVariety() {
        return variety;
    }

    public void setVariety(String variety) {
        this.variety = variety;
    }

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    /*
    @该类中有3种行为,包括:
    @吃饭时间;
    @睡觉时间;
    @发狂时间;
     */
    public void eat(){
        System.out.println(this.name+"正在吃饭");
    }
    public void sleep(){
        System.out.println(this.name+"正在睡觉");
    }
    public void crazy(){
        System.out.println(this.name+"正在发狂");
    }

}
ApplicationRun.java
public class ApplicationRun {
    public static void main(String[] args) {
        Cats xiaoMing_family_pets = new Cats();
        Cats xiaoZhang_family_pets = new Cats();
        xiaoMing_family_pets.setName("Tom");
        xiaoMing_family_pets.setAge(2);
        xiaoMing_family_pets.setVariety("布偶");
        xiaoMing_family_pets.setFood("猫粮和小鱼干");
        xiaoZhang_family_pets.setName("Jerry");
        xiaoZhang_family_pets.setAge(2);
        xiaoZhang_family_pets.setVariety("狸花猫");
        xiaoZhang_family_pets.setFood("老鼠和小鸟");
        System.out.println("xiaoMing_family_pets.name = " + xiaoMing_family_pets.getName());
        System.out.println("xiaoMing_family_pets.age = " + xiaoMing_family_pets.getAge());
        System.out.println("xiaoMing_family_pets.variety = " + xiaoMing_family_pets.getVariety());
        System.out.println("xiaoMing_family_pets.food = " + xiaoMing_family_pets.getFood());
        System.out.println("xiaoZhang_family_pets.name = " + xiaoZhang_family_pets.getName());
        System.out.println("xiaoZhang_family_pets.age = " + xiaoZhang_family_pets.getAge());
        System.out.println("xiaoZhang_family_pets.variety = " + xiaoZhang_family_pets.getVariety());
        System.out.println("xiaoZhang_family_pets.food = " + xiaoZhang_family_pets.getFood());
        //记录吃饭时间
        xiaoMing_family_pets.eat();
        xiaoZhang_family_pets.eat();
        //记录睡觉时间
        xiaoMing_family_pets.sleep();
        xiaoZhang_family_pets.sleep();
        //记录发狂时间
        xiaoMing_family_pets.crazy();
        xiaoZhang_family_pets.crazy();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值