1. Return value type
Basic type: (Basic type Simple)
Reference type:
Class: Returns the object of the class
1 classStudent2 {2 Public voidStudy () {3System.out.println ("Good good study,day day up");4 }5 }6 7 classStudentDemo1 {8 PublicStudent2 getstudent () {9 return NewStudent2 ();//equivalent to getting new Student2 ();Ten } One } A - classStudentTest2 { - Public Static voidMain (string[] args) { the - //requirement: I want to use the study () method in the Student2 class - //However, this time my request is not to directly create student objects - //let you use Studentdemo to help you create objects + -StudentDemo1 SD =NewStudentDemo1 (); + //equivalent to new Student2 SD = new Student2 (); A atStudent2 s =sd.getstudent (); - //equivalent to Student2 s = new Student2 (); - s.study (); - } -}
2. Return value type
Basic type: (Basic type Simple)
Reference type:
Abstract class: Returns the subclass object of the abstract class
1 Abstract classPerson3 {2 Public Abstract voidstudy ();3 }4 5 classPersonDemo3 {6 PublicPerson3 Getperson () {7 //Person p = new Student ();8 //return p;9 return NewStudent3 ();Ten } One } A - classStudent3extendsPerson3 { - Public voidStudy () { theSystem.out.println ("Good good study,day day up"); - } - } - + classPersonTest3 { - Public Static voidMain (string[] args) { + //requirements: I want to test the study () method in the Person3 class APersonDemo3 PD =NewPersonDemo3 ();//Student3 pd = new Studeng3 (); atPerson3 s = Pd.getperson ();//Person3 s = Pd.getperson (); - s.study (); - } -}
3. Return value type
Basic type: (Basic type Simple)
Reference type:
Class: Returns the object of the class
Abstract class: Returns the subclass object of the abstract class
Interface: An object that returns an implementation class for the interface
1 //define a hobby's interface2 InterfaceLove {3 Public Abstract voidLove ();4 }5 6 classLovedemo {7 PublicLove Getlove () {8 //Love L = new Teacher ();9 //return l;Ten One return NewTeacher (); A } - } - the //defining a concrete class implementation interface - classTeacherImplementsLove { - Public voidLove () { -SYSTEM.OUT.PRINTLN ("Teachers love students, Love Java, love Brigitte")); + } - } + A classTeacherTest2 { at Public Static voidMain (string[] args) { - //How to test it? -Lovedemo ld =NewLovedemo (); -Love L = Ld.getlove ();//new Teacher (); Love L = new Teacher (); polymorphic - L.love (); - } in}
4, chain-type programming.
Each time the method is called, an object is returned.
1 classStudent {2 Public voidStudy () {3System.out.println ("Good good study,day day up");4 }5 }6 7 classStudentdemo {8 PublicStudent getstudent () {9 return NewStudent ();Ten } One } A - classStudentTest3 { - Public Static voidMain (string[] args) { the //How is it called ? -Studentdemo SD =NewStudentdemo (); - //Student s = sd.getstudent (); - //s.study (); + - //Note + sd.getstudent (). Study (); A } at}
java9-3 return type