JAVA Implementation Interface Class

Source: Internet
Author: User

Requirement: Define an interface with four abstract methods.

Interface functions:

<Br/> Public interface jiekou {<br/> Public double area (); <br/> Public double Len (); <br/> Public void area1 (); <br/> Public void len1 (); <br/>}< br/>

Circle class, implementation interface:

Public class circle implements jiekou {<br/> double r; <br/> double ESP = 3.1415926; <br/> circle (double) <br/>{< br/> r = A; <br/>}< br/> Public double area () {<br/> // todo auto-generated method stub <br/> return ESP * r; <br/>}< br/> Public void area1 () {<br/> // todo auto-generated method stub <br/> system. out. println ("Circle area () = 3.1415926 * r"); <br/>}< br/> Public double Len () {<br/> // todo auto-generated method stub <br/> return 2 * ESP * r; <br/>}< br/> Public void len1 () {<br/> // todo auto-generated method stub <br/> system. out. println ("circle Len () = 2*3.1415926 * r"); <br/>}< br/>

Rectangle class, implementation interface:

Public class rectangle implements jiekou {<br/> double H, W; <br/> rectangle (double A, double B) <br/>{< br/> H =; W = B; <br/>}< br/> Public double area () {<br/> // todo auto-generated method stub <br/> return H * W; <br/>}< br/> Public void area1 () {<br/> // todo auto-generated method stub <br/> system. out. println ("H * w"); <br/>}< br/> Public double Len () {<br/> // todo auto-generated method stub <br/> return 2 * H + 2 * w; <br/>}< br/> Public void len1 () {<br/> // todo auto-generated method stub <br/> system. out. println ("2 * H + 2 * w"); <br/>}</P> <p >}< br/>

Main function:

Public class main {<br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> jiekou A = new circle (10); </P> <p> system. out. println (. area (); <br/>. area1 (); <br/> jiekou B = new rectangle (10, 20); <br/> system. out. println (INT) B. area (); <br/> B. area1 (); <br/>}< br/>

 

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.