0
点赞
收藏
分享

微信扫一扫

TestCat2


package com.shrimpking.t1;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/9/16 18:44
 */
class MyCat2{
    //属性私有
    private float weight;

    public MyCat2()
    {
        this.weight = 0.0f;
    }

    public float getWeight()
    {
        return weight;
    }

    //设置属性的值
    public void setWeight(float weight)
    {
        if(weight > 0){
            this.weight = weight;
        }else {
            System.out.println("Weight 设置非法,应该 > 0");
            this.weight = 10.0f; //默认值
        }
    }
}

public class TestCat2
{
    public static void main(String[] args)
    {
        MyCat2 cat = new MyCat2();
        cat.setWeight(-10f);
        float weight = cat.getWeight();
        System.out.println("the weight of a cat is=" + weight);
    }
}

举报

相关推荐

单链表2-2

技术2---swagger2

2020/2/22-2

javase 2-2 类

2

0 条评论