1. Form parameters:
Basic type (too simple)
Reference type
Class Name: (We've already talked about an anonymous object) what you need is an object of that class
Abstract class: What is needed is the abstract class subclass object
Interface: The implementation class object for the interface is required
A, Class name: (In fact we have already spoken of the anonymous object) need is the object of this class (this example is this)
1 classstudent{2 Public voidStudy () {3System.out.println ("= = = Learn = = =");4 }5 }6 7 classteacher{8 Public voidMethod (Student s)//A class is referenced here, and what it needs is the object of this class9 {TenSYSTEM.OUT.PRINTLN ("---guidance---"); One } A } - - classStudentTest1 { the Public Static voidMain (string[] args) { - //Call the study method in the student class -Student T =Newstudent (); - t.study (); +System.out.println ("----------"); - //call method of the teacher class +Teacher TC =Newteacher (); A //The method of the teacher class refers to a class that requires objects of this class atStudent T1 =Newstudent (); -Tc.method (t1);//What it needs is the object of this class. -System.out.println ("----------"); - //The use of anonymous objects calls the method of the teacher class - NewTeacher (). Method (Newstudent ()); - } in -}
B, abstract class: What is needed is the abstract class subclass object
1 Abstract classPerson {2 Public Abstract voidstudy ();3 }4 5 classPersondemo {6 Public voidMethod (person p) {//p = new Student (); Person p = new Student (); // polymorphic7 p.study ();8 }9 }Ten One //define a specific class of students A classStudentextendsPerson { - Public voidStudy () { -System.out.println ("Good good study,day day up"); the } - } - - 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 A //So, we should first define a specific class at //requirement: I want to use method () in the Persondemo class -Persondemo PD =NewPersondemo (); -Person p =NewStudent (); - Pd.method (p); - } -}
C, interface: required is the implementation of the interface class object
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); - } -}
Java9-2 form Parameters