Basic ideas:
An interface is used as the standard of pet store. As long as the method in the interface is satisfied, the interface can enter the pet store. This example uses the yellVoice() method as the standard.
Imported class
import java.util.Scanner;//Import Sacnner class
Interface
interface PetShop{//Pet store interface abstract String yellVoice();//There is only one calling method in the interface. As long as the class meets this method, it can be connected to the pet store }
Cat class implementation interface
class Cat implements PetShop{//Class Cat private String name; private String color; private int age; public Cat(String name,String color,int age){ this.name=name; this.color=color; this.age=age; } public String yellVoice(){//Override interface method return "cat"; } public String toString(){ return "(Cat) Name:"+this.name+",Color:"+this.color+",Age:"+this.age+",Call:"+this.yellVoice(); } }
Dog class implementation interface
class Dog implements PetShop{//Class Dog private String name; private String color; private int age; public Dog(String name,String color,int age){ this.name=name; this.color=color; this.age=age; } public String yellVoice(){//Override call method return "Wang Wang Wang"; } public String toString(){ return "(Dog) Name:"+this.name+",Color:"+this.color+",Age:"+this.age+",Call:"+this.yellVoice(); } }
Other class implementation interfaces
class Other implements PetShop{//Other classes private String name; private String color; private int age; public Other(String name,String color,int age){ this.name=name; this.color=color; this.age=age; } public String yellVoice(){//Override call method, unknown return "Unknown"; } public String toString(){ return "(Other) Name:"+this.name+",Color:"+this.color+",Age:"+this.age+",Call:"+this.yellVoice(); } }
The main loading class, which has a setPet() method, uses this method to set the corresponding pet type to reduce the code amount in the main method**
public class Train{ static Object setPet(String type,String name,String color,int age){//setPet method, set the object of the instantiated interface if(type.equals("cat")){ return new Cat(name,color,age);//If it's a Cat, instantiate the Cat object }else if(type.equals("Dog")){ return new Dog(name,color,age);//If it's a Dog, instantiate the Dog object }else{ return new Other(name,color,age); } } public static void main(String args[]){//Main method System.out.println("Please enter the total number of pets:"); Scanner s=new Scanner(System.in); int n=s.nextInt(); PetShop []p=new PetShop[n];//Create an array of objects (equivalent to opening up space in the pet store) for(int i=0;i<n;++i){ System.out.println("Please input number 1."+(i+1)+"Pet category:"); String type=s.next(); System.out.println("Please enter a pet"+type+"Name:"); String name=s.next(); System.out.println("Please input"+name+"Color:"); String color=s.next(); System.out.println("Please input"+name+"Age"); int age=s.nextInt(); p[i]=(PetShop)setPet(type,name,color,age);//Forced transformation with PetShop } for(int j=0;j<n;++j){//Loop through the content of the pet store (each class overrides the toString method) System.out.println("The first"+(j+1)+"Pets are:"+p[j]); } } }