Java Interface--2

Source: Internet
Author: User

Write Java programs as required:

(1) Write an interface: Interfacea, which contains only one method, int (int n);

(2) Write a class: ClassA to implement the interface Interfacea, implement the int method (int n) interface square

The calculation of 1 to n is required;

(3) Write another class: ClassB to Implement Interface Interfacea, implementing the Int method (int n) interface

method, it is required to calculate the factorial of n (n!);

(4) Write Test Class E, test the implementation using the form of an interface callback in the main method of test class E

The class of the interface.

Package Lianxi1;public interface Interfacea {//write an interface: Interfacea, contains only one method, int (int n); int method (int n);}

Package Lianxi1;public class ClassA implements Interfacea {@Overridepublic int method (int n) {//TODO auto-generated methods stub int Sum=0;f or (int i=0;i<=n;i++) {sum+=i;} return sum;} public static void Main (string[] args) {//TODO auto-generated method stub//classa to implement Interface Interfacea, implement int method (int n) interface party//method, Requires calculation of 1 to N and Interfacea a=new ClassA (); A.method (10); System.out.println (A.method (10));}}

Package Lianxi1;public class ClassB implements Interfacea {@Overridepublic int method (int n) {//TODO auto-generated methods stub int A=1;for (int i=1;i<=n;i++) {a*=i;} return A;} public static void Main (string[] args) {//TODO auto-generated method stub Interfacea b=new ClassB (); B.method (10); System.out.println (B.method (10));}}

Package Lianxi1;public class E implements Interfacea {@Overridepublic int method (int n) {//TODO auto-generated methods stub return n;} public static void Main (string[] args) {//write Test Class E, use the form of an interface callback in the main method of test class E to test the class implementing//interface. Interfacea a=new E (); A.method (12); System.out.println (A.method (12));}}

Java 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.