In Java, formal parameters mainly have basic types, class names, abstract class names, interface names. Let's tell each of them.
1. Basic type This is too simple to be brief.
2. Class name
Class Name: An object that requires this class
1 classStudent {2 Public voidStudy () {3System.out.println ("Good good study,day day up");4 }5 }6 7 classStudentdemo {8 Public voidMethod (Student s) {//SS; ss = new Student (); Student s = new Student ();9 s.study ();Ten } One } A - classStudenttest { - Public Static voidMain (string[] args) { the //requirements: I want to test the study () method of the student class -Student s =NewStudent (); - s.study (); -System.out.println ("----------------"); + - //Requirement 2: I want to test method () methods in the Studentdemo class +Studentdemo SD =NewStudentdemo (); AStudent SS =NewStudent (); at Sd.method (ss); -System.out.println ("----------------"); - - //Anonymous Object Usage - NewStudentdemo (). Method (NewStudent ()); - } in}
3. Abstract class Name: A subclass object that requires this class
Abstract classPerson { Public Abstract voidstudy ();}classPersondemo { Public voidMethod (person p) {//p = new Student (); Person p = new Student (); // polymorphicP.study (); }}//define a specific class of studentsclassStudentextendsPerson { Public voidStudy () {SYSTEM.OUT.PRINTLN ("Good good study,day day up"); }}classPersontest { Public Static voidMain (string[] args) {//There's no way to use it at the moment .//because the abstract class has no corresponding concrete class//So, we should first define a specific class//requirement: I want to use method () in the Persondemo classPersondemo PD =NewPersondemo (); Person P=NewStudent (); Pd.method (P); }}
4. Interface Name: An implementation class object that requires this interface
is similar to the abstract class name
1 //define a hobby's interface2 InterfaceLove {3 Public Abstract voidLove ();4 }5 6 classLovedemo {7 Public voidMethod (Love L) {//l; l = new Teacher (); Love L = new Teacher (); polymorphic8 L.love ();9 }Ten } One A //defining a concrete class implementation interface - classTeacherImplementsLove { - Public voidLove () { theSYSTEM.OUT.PRINTLN ("Teachers love students, Love Java, love Brigitte")); - } - } - + classTeachertest { - Public Static voidMain (string[] args) { + //requirement: I want to test the Love () method in the Lovedemo class ALovedemo ld =NewLovedemo (); atLove L =NewTeacher (); - Ld.method (l); - } -}
A detailed description of the formal parameters in Java