【无标题】

#设计猫类,属性有种类|、颜色、年龄和体重,方法有输出猫的信息、猫发出叫声、猫玩东西

package demo;

class Cat {
    private String species; // 种类
    private String color; // 颜色
    private int age; // 年龄
    private double weight; // 体重

    // 构造方法
    public Cat(String species, String color, int age, double weight) {
        this.species = species;
        this.color = color;
        this.age = age;
        this.weight = weight;
    }

    // 输出猫的信息
    public void printCatInfo() {
        System.out.println("种类: " + species);
        System.out.println("颜色: " + color);
        System.out.println("年龄: " + age + "岁");
        System.out.println("体重: " + weight + "公斤");
    }

    // 猫发出叫声
    public void meow() {
        System.out.println("喵喵喵~");
    }

    // 猫玩东西
    public void playWithToy() {
        System.out.println("猫咪正在玩东西");
    }

    // getter 和 setter 方法
    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
}
public class Main{
    public static void main(String[] args) {
        // 创建一个Cat对象
        Cat cat = new Cat("家猫", "白色", 3, 3.5);

        // 输出猫的信息
        cat.printCatInfo();

        // 猫发出叫声
        cat.meow();

        // 猫玩东西
        cat.playWithToy();
    }
}
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值