10-02 the problem of Java form parameters and return values in-depth study, chained programming

Source: Internet
Author: User

Problems with formal parameters and return values:

1: Problem of formal parameters and return values (understanding)    (1) Formal parameters:        class Name: Object abstract class name required for this class        : Subclass object Interface name that requires this class        : An implementation class object that requires the interface    (2 return value type:        class Name: Returns the object        abstract class name of the class: Returns the Subclass object interface name of the class: an object (3) Chained programming object that returns the        implementation class of    the interface        . Method 1 ( ). Method 2 () .... Method N ();                This usage: In fact, after the invocation of Method 1 (), an object should be used, and                  after the call of Method 2 (), an object should be returned.                  after the method N () call is complete, the object may or might not be an object. 

Class masterpieces as formal parameters

/*formal Parameters: Basic types (too simple, not what I'm going to talk about today) reference type class name: (as we've already said about anonymous objects), we need an abstract class of objects of this class: Interface*/classStudent { Public voidStudy () {SYSTEM.OUT.PRINTLN ("Good good study,day day up"); }}classStudentdemo { Public voidMethodStudent S) {//SS; ss = new Student (); The object of the class is required S.study (); }}classStudenttest { Public Static voidMain (string[] args) {//requirements: I want to test the study () method of the student classStudent s =NewStudent ();        S.study (); System.out.println ("----------------"); //Requirement 2: I want to test method () methods in the Studentdemo classStudentdemo SD =NewStudentdemo (); Student SS = new Student ();        Sd.method (SS); System.out.println ("----------------"); //Anonymous Object Usage        NewStudentdemo (). Method (NewStudent ()); }}

Abstract class masterpieces as formal parameters

/*formal Parameters: Basic types (too simple, not what I'm going to talk about today) reference type class name: (as we've already said about anonymous objects), we need an abstract class of objects of this class : the abstract Sub-class object interface*/Abstract classPerson { Public Abstract voidstudy ();}classPersondemo { Public voidMethod (person p) {//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 = new Student ();    Pd.method (p); }}

Interface name as formal parameter

/*formal Parameters: Basic types (too simple, not what I'm going to talk about today) reference type class name: (as we've already said about anonymous objects) What you need is an abstract class of objects of that class: This abstract subclass object is required Interface: The implementation class object for the interface is required *///define a hobby's interfaceInterfaceLove { Public Abstract voidLove ();}classLovedemo { Public voidMethodLove L) {//l;  L = new Teacher (); Love L = new Teacher ();  polymorphicL.love (); }}//Define concrete classes to implement interfacesclassTeacherImplementsLove { Public voidLove () {System.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 classLovedemo ld =NewLovedemo (); Love l = new Teacher ();    Ld.method (l); }}

class masterpieces are return value class type

/*return value type base type: (Basic type too simple, I'm not going to explain) reference type: class: Returns the object abstract class for this class : interface: */classStudent { Public voidStudy () {SYSTEM.OUT.PRINTLN ("Good good study,day day up"); }}classStudentdemo { Public Student getstudent () {//Student s = new Student (); //Student ss = S; //Student s = new Student (); //return s;       return new Student (); }}classStudentTest2 { Public Static voidMain (string[] args) {//requirement: I want to use the study () method in the Student class//However, this time my request is not to directly create student objects//Let you use Studentdemo to help you create objectsStudentdemo SD =NewStudentdemo (); Student s= Sd.getstudent ();//new Student (); Student s = new Student ();S.study (); }}

Abstract class as return value type

/*return value type base type: (Basic type too simple, I'm not going to explain) reference type: Class: Returns the object abstract class for this class : The subclass object of the abstract class is returned Interface:*/Abstract classPerson { Public Abstract voidstudy ();}classPersondemo { PublicPerson   Getperson () {//Person p = new Student (); //return p;                return new Student (); }}classStudentextendsPerson { Public voidStudy () {SYSTEM.OUT.PRINTLN ("Good good study,day day up"); }}classPersonTest2 { Public Static voidMain (string[] args) {//requirement: I want to test the study () method in the person classPersondemo PD =NewPersondemo (); Person P= Pd.getperson ();//new Student (); Person p = new Student (); polymorphicP.study (); }}

Interface as the return value type

/*return value type base type: (Basic type too simple, I'm not going to explain) reference type: Class: Returns the object abstract class for this class: The subclass object of the abstract class is returned Interface: An object that returns an implementation class for the interface *///define a hobby's interfaceInterfaceLove { Public Abstract voidLove ();}classLovedemo { PublicLove Getlove () {//Love L = new Teacher (); //return l;                return NewTeacher (); }}//defining a concrete class implementation interfaceclassTeacherImplementsLove { Public voidLove () {System.out.println ("Teachers love students, Love Java, love Brigitte"); }}classTeacherTest2 { Public Static voidMain (string[] args) {//How to test it?Lovedemo ld =NewLovedemo (); Love L= Ld.getlove ();//new Teacher (); Love L = new Teacher (); polymorphicL.love (); }}

Chained programming

/*    chain-type programming. Each time the method is called, an object is returned. */classStudent { Public voidStudy () {SYSTEM.OUT.PRINTLN ("Good good study,day day up"); }}classStudentdemo { PublicStudent getstudent () {return NewStudent (); }}classStudentTest3 { Public Static voidMain (string[] args) {//How is it called ?Studentdemo SD =NewStudentdemo (); //Student s = sd.getstudent (); //S.study (); //attention, this is a chain-based programming sd.getstudent (). Study (); }}

10-02 the problem of Java form parameters and return values in-depth study, chained programming

Related Article

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.