If you are going to pass a numeric value to a method parameter, you may want the method parameter to be of type double, so that you can pass byte, int, long, float, and double data to the parameter.
If the parameter of a method is an interface type , we can pass a reference to the instance of any class that implements the interface to the interface parameter, and then the interface parameter can call back the interface method implemented by the class.
For example:
interfaceSpeakHello{voidspeakHello();}classChineseimplementsSpeakHello{publicvoidspeakHello(){System.out.println(Chinese people are accustomed to greetings: Hello, have you eaten?);}}classEnglishimplementsSpeakHello{publicvoidspeakHello(){System.out.println(British Custom greeting: Hello, the weather is nice!);}} classKindHello{publicvoidlookHello(SpeakHellohello){//Interface type parameter hello.speakHello();//Interface callback}} publicclassMain{publicstaticvoidmain(Stringargs[]){KindHellokindHello=newKindHello ();kindHello.lookHello(newChinese());kindHello.lookHello(newEnglish());}}
The running results are as follows:
Chinese people are accustomed to greetings: Hello, have you eaten? British people are accustomed to greetings: Hello, the weather is nice!
Note: If several more classes like Chinese and English are added to the source file, the KindHello class does not need to be modified in any way.