Java school last semester's question, can anyone give a complete code standard answer for reference

CSDN Q & A 2022-02-13 07:48:21 阅读数:961

java school semester question complete

img




Take the answer :
public interface Animal{ void eat(); void sleep();}public class Cat implements Animal{ public void eat(){ System.out.println("Cat can eat"); } public void sleep(){ System.out.println("Cat can sleep"); }}public class Dog implements Animal{ public void eat(){ System.out.println("Dog can eat"); } public void sleep(){ System.out.println("dog can sleep"); }}public interfaceDemo{ public static void main(String args[]){ Cat a=new Cat(); a.eat(); a.sleep(); Dog p= new Dog(); p.eat(); p.sleep(); }}


Other answers 2:

This is mainly java Problems with classes and objects , Just create classes and objects as needed


Other answers 3:

img

Let me analyze your problem for you .
Your problem is that you don't understand the relationship between interfaces and implementation classes .
First, only methods are defined in the interface , There is no concrete realization .
Class implementation interface , The so-called implementation is to implement the methods in the interface , That is to give the specific implementation of the method defined in the interface .

 Interface : Definition eat() Method 、 sleep() Method . namely public interface Animal { public void eat(); public void sleep();} Two implementation classes implement interfaces :** Note that the implementation method should add @Override annotation **Cat class :public class Cat implements Animal {@Overridepublic void eat(){ System.out.println(" Cats can eat "); }@Override public void sleep(){ System.out.println(" Cats can sleep "); }}Dog Class isomorphism : But the specific implementation content is different .public class Dog implements Animal {@Overridepublic void eat(){ System.out.println(" Dogs can eat "); }@Override public void sleep(){ System.out.println(" Dogs can sleep "); }} Test class : establish cat and dog Class object , And call the method in it .public interfaceDemo{ public static void main(String args[]){ Cat cat=new Cat(); cat.eat(); cat.sleep(); Dog dog= new Dog(); dog.eat(); dog.sleep(); }}

Other answers 4:

I have a program with the same function .


Other answers 5:
public interface Animal { public void eat(String s); public void sleep(String s);}public class Cat implements Animal{ @Override public void eat(String s) { System.out.println("Cat eat"); } @Override public void sleep(String s) { System.out.println("Cat sleep"); }}public class Dog implements Animal{ @Override public void eat(String s) { System.out.println("Dog eat"); } @Override public void sleep(String s) { System.out.println("Dog sleep"); }}public class InterfaceDemo { public static void main(String[] args){ Cat cat = new Cat(); cat.eat( "eat" ); cat.sleep( "sleep" ); Dog dog = new Dog(); dog.eat( "eat" ); dog.sleep( "sleep" ); }}

Other answers 6:

Have you solved your doubts


Other answers 7:

Animal.java file :

public interface Animal { public void eat(); public void sleep();}

Cat.java file :

public class Cat implements Animal{ @Override public void eat() { System.out.println("Cat eat!"); } @Override public void sleep() { System.out.println("Cat sleep!"); } }

Dog.java file :

public class Dog implements Animal{ @Override public void eat() { System.out.println("Dog eat!"); } @Override public void sleep() { System.out.println("Dog sleep!"); } }

TestAnimal.java file :

public class TestAnimal { public static void main(String args[]) { Cat ct = new Cat(); ct.eat(); ct.sleep(); Dog dog = new Dog(); dog.eat(); dog.sleep(); }}

Other answers 8:


Other answers 9:

public interface Animal {

/** * How to eat food * @param food Food name */public void eat(String food);/** * How to sleep * @param hours Sleep duration */public void sleep(int hours);

}

public class Dog implements Animal {

@Overridepublic void eat(String food) { // Output : Class name + Method name + Food name  System.out.println("Dog eat " + food);}@Overridepublic void sleep(int hours) { // Output : Class name + Method name + Sleep time  System.out.println("Dog sleep " + hours + " hours at night");}

}

public class Cat implements Animal {

@Overridepublic void eat(String food) { // Output : Class name + Method name + Food name  System.out.println("Cat eat " + food);}@Overridepublic void sleep(int hours) { // Output : Class name + Method name + Sleep time  System.out.println("Cat sleep " + hours + " hours during the day");}

}

public class IntrefaceDemo {

public static void main(String[] args) { Dog dog = new Dog(); dog.eat("bone"); dog.sleep(6); System.out.println(); Cat cat = new Cat(); cat.eat("fish"); cat.sleep(6);}

}

Running results :
Dog eat bone
Dog sleep 6 hours at night

Cat eat fish
Cat sleep 6 hours during the day

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130748192685.html