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