Java Basics-Interface

Source: Internet
Author: User

Write 2 interfaces:interfacea and interfaceb; in interface interfacea There is a method void in the

Printcapitalletter (); there is a method in interface interfaceb void printlowercaseletter () ; then

write a class after Print Implementation interface Interfacea and interfaceb, requires printcapitalletter () Method

implements the output capital English alphabet function,the Printlowercaseletter () method implements the output lowercase English

the function of the alphabet. Write a main class E, create the Print object in the main method of the primary class e , and assign

value to Interfacea variable a, Object a calls the printCapitalletter method, and finally in the main class E

the main method of creating the Print object and assigning a value to the interfaceb variable b , the object b called

Printlowercaseletter method.

Package Ersan;  Public Interface interfacea {    void  printcapitalletter ();    }
Package Ersan;  Public Interface interfaceb {    void  printlowercaseletter ();}

Print

Package Ersan; Public classPrint implements Interfacea, interfaceb{@Override Public voidPrintcapitalletter () {//TODO Auto-generated method stubsSystem. out. println ("Abdef"); } @Override Public voidPrintlowercaseletter () {//TODO Auto-generated method stubsSystem. out. println ("ABCDEF"); }}
 package Ersan;  public  class   E { public  static  void   Main (string[] args) { //  TODO Auto-generated method stub   Interfacea a  =new                  Print ();                A.printcapitalletter ();                Interfaceb b  =new   Print ();                    B.printlowercaseletter (); }

(1) write an interface:interfacea, which contains only one method, int (int n);

Package Ersan;  Public Interface interfacea {    int method (int  n);    }

(2) write a class:ClassA to implement the interface interfacea, implementing the int method (int n) Interface Side

the calculation of 1 to n is required ;

 package Ersan;  public  class   ClassA implements Interfacea {@Override  public< /span> int  method (int   N) { //  TODO auto-generated method stub  int  b=0  ;  for  (int  i= 1 ; I<=n;i++) {b  =i+b;     return   b; }}

(3) write another class:ClassB to Implement Interface Interfacea, Implementing int method (int n) Interface

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

 package Ersan;  public  class   ClassB implements Interfacea {@Override  public< /span> int  method (int   N) { //  TOD auto-generated method stub  
    
     int 
     b=1  ;  for  (int  i= 1 ; I<=n;i++) {b  =b*i;     return   b; }}

(4) write test class e, test the implementation using the form of an interface callback in the main method oftest class e

The class of the interface.

Package Ersan; Public classE { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsInterfacea a=NewClassA (); System. out. println (A.method (Ten)); Interfacea b=NewClassB (); System. out. println (B.method (5)); }}

Java Basics-Interface

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.