java9-3 return type

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.