6-2设计抽象动物Animal类及其子类

更新时间:2023-05-22 12:31:25 阅读: 评论:0

6-2设计抽象动物Animal类及其⼦类
编写抽象Animal类与其⼦类Dog类与Cat类,具体要求如下:
Animal抽象类:私有name属性(String型),私有age属性(int型),抽象⽅法info(),抽象⽅法speak(),构造⽅法,tter和getter Dog类:增加灵性agility属性(int型),覆盖info()和speak()⽅法,构造⽅法,tter和getter
Cat类:增加捕⿏能⼒mousingAbility属性(int型),覆盖info()和speak()⽅法,构造⽅法,tter和getter ⼦类重写后的info()⽅法输出⼦类特殊属性和名字、年龄属性(具体格式见输出样例);
⼦类重写后的speak()⽅法输出各⼦类叫声(具体格式见输出样例)
在⼦类构造⽅法中初始化⼦类特殊属性,并调⽤⽗类构造⽅法初始化⽗类属性
已有的主类Main定义:
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Animal a1=new (),Int(),Int());
Animal a2=new (),Int(),Int());
a1.info();
a1.speak();
System.out.Name());
System.out.Age());
a2.info();
a2.speak();
System.out.Name());
System.out.Age());
input.clo();
}
}
/* 请在这⾥填写你的Animal类、Dog类、Cat类 */
输⼊样例:
第⼀⾏输⼊狗的名字、年龄和灵性值
第⼆⾏输⼊猫的名字、年龄和捕⿏能⼒
DogJohn 3 123
CatMike 4 200
结尾⽆空⾏
输出样例:
前四⾏为程序结构测试数据,请严格按题⽬要求设计类即可。
true
true
true
true
狗的名字是DogJohn,年龄是3,灵性值是123旺旺
DogJohn
3
猫的名字是CatMike,年龄是4,捕⿏能⼒是200喵喵
CatMike
4
代码如下:追梦的翅膀
abstract class Animal{
private String name;
private int age;
abstract void info();
abstract void speak();
public String getName() {
return name;
}
public void tName(String name) {
负热量食物this.name = name;
}
public int getAge() {
return age;
}
public void tAge(int age) {
this.age = age;
}
}
class Dog extends Animal{
private int agility;
public Dog(String n,int a,int b){
super.tName(n);
新员工2super.tAge(a);
this.tAgility(b);
诚信和}
public int getAgility() {
return agility;
}
public void tAgility(int agility) {
this.agility = agility;
}
public void info() {
System.out.println("狗的名字是"+getName()+",年龄是"+getAge()+",灵性值是"+getAgility());
无法连接网络
}
public void speak() {
System.out.println("旺旺");
}
}
class Cat extends Animal{
private int mousingAbility;
public Cat(String n,int a,int b){
super.tName(n);
super.tAge(a);
this.tMousingAbility(b);
}
现代化的英文public int getMousingAbility() {
return mousingAbility;
二年级课本}
public void tMousingAbility(int mousingAbility) {
}
public void info() {
苹果手机验证真假
System.out.println("猫的名字是"+getName()+",年龄是"+getAge()+",捕⿏能⼒是"+getMousingAbility()); }
public void speak() {
System.out.println("喵喵");
}
}

本文发布于:2023-05-22 12:31:25,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/923459.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:属性   年龄   设计   抽象   输出
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图