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 方法被“挤出”了类的作用域,编译器误以为这些方法是“隐式声明的类”(其实是语法结构混乱导致的误报)。