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