eat后面少加了{导致出现语言级别 ‘8‘ 不支持 隐式声明的类,你敢信???

package com.example.pets;

public abstract class Pet {
    protected String name;
    protected int health;
    protected int closeness;

    public Pet(String name) {
        this.name = name;
        this.health = 100;
        this.closeness = 0;
    }

    protected void eatBase(int value, String food) {
        // 基础进食方法供子类调用
        health = Math.min(100, health + value);
        closeness += 5;
        System.out.println(name + "吃掉了" + value + "克" + food + ",健康值+" + value);
    }

    //少加了大括号导致错误
    public abstract void eat(int value); // 抽象进食方法
        
    }

    public void play(int minutes, String activity) {
        health = Math.max(0, health - minutes);
        closeness += 10;
        System.out.println(name + activity + "了" + minutes + "分钟,亲密度+" + 10);
    }

    public void print() {
        System.out.println("宠物名称:" + name + ",健康值:" + health + ",亲密度:" + closeness);
    }
}
        eat后面少加了{导致出现语言级别 '8' 不支持 隐式声明的类,你敢信???

eat  方法声明后 提前闭合了类的大括号,导致 play  和 print  方法被“挤出”了类的作用域,编译器误以为这些方法是“隐式声明的类”(其实是语法结构混乱导致的误报)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值