0
点赞
收藏
分享

微信扫一扫

TestCat4


package com.shrimpking.t1;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/9/16 18:50
 */
class MyCat4{
    private float weight;

    public void setWeight(float weight){
        if(weight > 0){
            this.weight = weight;
        }else{
            System.out.println("weight设置非法,应该大于0");
            this.weight = 10; //默认值
        }
    }

    public float getWeight(){
        return this.weight;
    }

    private void makeSound(){
        System.out.println("Meow,Meow,my weight is " + this.weight);
    }
}

public class TestCat4
{
    public static void main(String[] args)
    {
        MyCat4 cat = new MyCat4();
        cat.setWeight(-10f);
        float weight = cat.getWeight();
        System.out.println("the weight of cat is =" + weight);
        //私有方法,是无法调用的
        //cat.makeSound();
    }
}

举报

相关推荐

2022/4/4

2022/4/4偷懒

实验4:4-12

实验4:4-8

4月4日java作业

0 条评论