请问现在已写的代码有什么逻辑问题吗?希望能够给予一些参考和意见
3条回答 默认 最新
- ShowMeAI 2022-12-21 13:33关注
望采纳。有以下几个问题:
- 在 Animal 类的构造方法中, sex=false; 应该改为 this.sex = false; 。
- 在 Animal 类的 Introduce 方法中, if (sex=false){ 应该改为 if (sex == false){ 。
- 在 Dog 类的构造方法中, sex=true; 应该改为 super.setSex(true); 。
- 在 Dog 类的 Introduce 方法中, if (sex=true){ 应该改为 if (super.getSex() == true){ 。
修改后的代码如下:
class Animal { private boolean sex; private int age; public Animal(boolean sex, int age) { this.sex = false; this.age = age; } public boolean getSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void Introduce() { if (sex == false) { System.out.println("This is a male Animal!"); } else { System.out.println("This is a female Animal!"); } } } class Dog extends Animal { public Dog(boolean sex, int age) { super(sex, age); super.setSex(true); } public void Introduce() { if (super.getSex() == true) { System.out.println("This is a male Dog!"); } else { System.out.println("This is a female Dog!"); } } } public class AnimalTest { public static void main(String[] args) { Animal ani = new Animal(false, 10); ani.Introduce(); Dog dog = new Dog(true, 5); dog.Introduce(); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 对于这个问题的解释说明
- ¥200 询问:python实现大地主题正反算的程序设计,有偿
- ¥15 smptlib使用465端口发送邮件失败
- ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
- ¥15 对于squad数据集的基于bert模型的微调
- ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
- ¥20 steam下载游戏占用内存
- ¥15 CST保存项目时失败
- ¥15 树莓派5怎么用camera module 3啊
- ¥20 java在应用程序里获取不到扬声器设备