现在的位置: 首页 > 综合 > 正文

实验课上第一次实现简单的接口

2013年10月08日 ⁄ 综合 ⁄ 共 1086字 ⁄ 字号 评论关闭

实验题目:

   

    1、定义矩形接口rect ,在接口中有计算面积area()、获取宽度getWidth()、获取高度getHeight()。继承接口rect实现矩形类Rectangle1.编写程序应用Rectangle1类。

 

 

 

 

 class Rectangle1 implements rect              // Rectangle1类 实现 接口rect .

{

       public  double height,width; //定义矩形的高 宽变量.

       public void  area()        //实现面积 计算方法.

       {    

              System.out.println ("This Rectangle's area is:" +this.width * this.height)  ;

       }
      

       public void  getWidth()        //取得矩形宽度.

       {

              System.out.println ("The Width is:" +this.width);       

       }

      
       public void  getHeight()       //取得矩形高度.

       {

              System.out.println ("The Height is:" +this.height);

       }

}
 

public class Re       //主类

{

       public static void main(String[] args)

       {

              Rectangle1 a=new Rectangle1();   //实例化.

              a.width=232;

              a.height=123;

              a.area();

              a.getWidth();

              a.getHeight();        

       }

}

 

interface rect  //接口类

{

       public void  area();

       public void  getWidth();

       public void  getHeight();     

}

 

 

  

    总结:这是第一次写接口,其实挺简单的(只是简单的实现题目要求而已),主要是看过没练过,真动起手来,还是有点慢。

 

   把第一次的贴出来,做自己的警示吧,刺激提高!

     

  

抱歉!评论已关闭.