java实现接口类

要求:定义一个接口,接口中有四个抽象方法。

接口类函数:

public interface jiekou { public double area(); public double len(); public void area1(); public void len1(); }

Circle类,实现接口:

public class Circle implements jiekou { double r; double esp =3.1415926; Circle(double a) { r=a; } public double area() { // TODO Auto-generated method stub return esp*r*r; } public void area1() { // TODO Auto-generated method stub System.out.println("Circle area()=3.1415926*r*r"); } public double len() { // TODO Auto-generated method stub return 2*esp*r; } public void len1() { // TODO Auto-generated method stub System.out.println("Circle len()=2*3.1415926*r"); } }

Rectangle类,实现接口:

public class Rectangle implements jiekou { double h,w; Rectangle(double a,double b) { h=a;w=b; } public double area() { // TODO Auto-generated method stub return h*w; } public void area1() { // TODO Auto-generated method stub System.out.println("h*w"); } public double len() { // TODO Auto-generated method stub return 2*h+2*w; } public void len1() { // TODO Auto-generated method stub System.out.println("2*h+2*w"); } }

主函数:

public class MAIN { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub jiekou a=new Circle(10); System.out.println(a.area()); a.area1(); jiekou b= new Rectangle(10,20); System.out.println((int )b.area()); b.area1(); } }

 

你可能感兴趣的:(Java学习)