animal巧记方法(animal快速记忆法)
大家好!今天让创意岭的小编来大家介绍下关于animal巧记方法的问题,以下是小编对此问题的归纳整理,让我们一起来看看吧。
ChatGPT国内免费在线使用,一键生成原创文章、方案、文案、工作计划、工作报告、论文、代码、作文、做题和对话答疑等等
只需要输入关键词,就能返回你想要的内容,越精准,写出的就越详细,有微信小程序端、在线网页版、PC客户端
本文目录:
一、Animal类有一个eat方法,写三个子类继承并重写eat方法,写boy类有一个feed方法喂养动物Animal作参数
你好,标准答案是这样的:
abstract class Animal {
public abstract void eat();
}
class Rabbit extends Animal {
public void eat() {
System.out.println("我吃草");
}
}
class Monkey extends Animal {
public void eat(){
System.out.println("我吃香蕉");
}
}
class Tiger extends Animal {
public void eat(){
System.out.println("我吃肉");
}
}
class Boy{
public void feed(Animal animal){
animal.eat() ;
}
}
public class TestAnimal {
public static void main(String[] args) {
Animal animal1 = new Rabbit() ;
Animal animal2 = new Monkey() ;
Animal animal3 = new Tiger() ;
Boy boy = new Boy() ;
boy.feed(animal1) ;
boy.feed(animal2) ;
boy.feed(animal3) ;
}
}
二、JAVA定义一个父类Animal(动物),其中包括方法eat( ),move( ),cry( )……等
package com.java;//声明一个抽象类动物
abstract class Animal {
//声明一个方法eat(),此处可以将方法抽象声明:public abstract void eat();move方法和cry方法类似
public void eat() {
System.out.println("Animal eat...");
}
public void move() {
System.out.println("Animal move...");
}
public void cry() {
System.out.println("Animal cry...");
}
}
//声明一个Cat类继承Animal类
class Cat extends Animal{
//重写父类方法
public void eat() {
System.out.println("Cat eat...");
}
public void move() {
System.out.println("Cat move...");
}
public void cry() {
System.out.println("Cat cry...");
}
}
//声明一个Dog类,继承Animal类
class Dog extends Animal{
//重写父类方法
public void eat() {
System.out.println("Dog eat...");
}
public void move() {
System.out.println("Dog move...");
}
public void cry() {
System.out.println("Dog cry...");
}
}
//以下是测试结果,可以省略
public class NLL {
public static void main(String[] args) {
Animal a1 = new Cat();
Animal a2 = new Dog();
Cat c = new Cat();
Dog d = new Dog();
a1.eat();
a1.cry();
a1.move();
a2.cry();
a2.eat();
a2.move();
c.cry();
c.eat();
c.move();
d.cry();
d.eat();
d.move();
}
}
代码已给,好好学习,外力毕竟不能帮你一生,最终还是要靠自己!
三、设计动物类Animal及其子类,将源代码保存至T3.java,具体要求如下:
abstract class Animal{
String type;
public Animal(String type){
this.type=type;
}
public abstract void Talk();
public void showInfo(){Talk();}
}
class Dog extends Animal{
String name;
public Dog(String type,String name){
super(type);
this.name=name;
}
public void Talk(){
System.out.println("此动物是:"+name+",此动物属于:"+type);
}
}
class Cat extends Animal{
String name;
public Cat(String type,String name){
super(type);
this.name=name;
}
public void Talk(){
System.out.println("此动物是:"+name+",此动物属于:"+type);
}
}
public class Diy_5_2_1{
public static void main(String[] args){
Dog doggie=new Dog("犬科动物","德国黑贝");
Cat kitty=new Cat("猫科动物","波斯猫");
doggie.showInfo();
kitty.showInfo();
}
}
四、java程序题
输出结果为3
原因是animal是Animal类的一个对象,但由于它是由Dog类经过实质上的强制类型转换而来的,是Dog的一个实例.Dog类覆盖了父类的方法,在调用printA()时,就会调用到Dog类的方法.
但如果调用没有被子类覆盖的方法,就会执行父类的方法.
另一种情况,如果子类扩展了父类原来没有的方法,animal想要调用这个方法时就会出现编译错误,因为它是Animal类的对象,而Animal类中没有这个方法.
class Animal{
public void printA(){
int a=10;
int result=10%3;
System.out.println(result);
}
}
class Dog extends Animal{
public void printA(){
int a=10;
System.out.println(a/3);
}
public static void main(String[] args){
Animal animal=new Dog();
animal.printA();//结果为3
}
}
以上就是关于animal巧记方法相关问题的回答。希望能帮到你,如有更多相关问题,您也可以联系我们的客服进行咨询,客服也会为您讲解更多精彩的知识和内容。
推荐阅读: