Java Fundamentals-Interface 2

Source: Internet
Author: User

 1  package   Jiekou;  2   Public  interface   InterfaceA1 { Span style= "color: #008080;" >4  /*   (1) write an interface: Interfacea, containing only one method int Method (int n);  5   (4) write Test Class E, In the main method of test class E, use the form of an interface callback to test the implementation of  6   interface class.  */ 7   method (int   8 } 
1  PackageJiekou;2 3  Public classClassAImplementsInterfaceA1 {4     /*5 * (2) Write a class: ClassA to Implement Interface Interfacea,6 * When implementing an int method (int n) interface approach, a calculation of 1 to n is required;7      */8 @Override9      Public intMethodintN) {Ten         intSum=0; One          for(inti=0;i<=n;i++) A         { -sum+=i; -         } the         returnsum; -     } -  -}
1  PackageJiekou;2 3  Public classClassBImplementsInterfaceA1 {4     /*(3) write another class: ClassB to Implement Interface Interfacea,5 * When implementing the int method (int n) interface approach, it is required to calculate the factorial of n (n!);6      */7 @Override8      Public intMethodintN) {9         Ten         intS=1; One          for(inti=1;i<=n;i++) A         { -s=s*i; -              the         } -         returns; -     } -  +}
1  PackageJiekou;2 3  Public classE1 {4 5      Public Static voidMain (string[] args) {6         //TODO Auto-generated method stubs7 8ClassA a=NewClassA ();9System.out.println ("1-5 of the And for:" +a.method (5));TenClassB b=NewClassB (); OneSystem.out.println (The factorial of "5" is: "+b.method (5)); A     } -  -}

Java Fundamentals-Interface 2

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.