龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

我对类,方法,接口的认识

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
作者:weirou520 //小汽车最多能载5吨货物 interface car{ int MAX_LOADWare= 5; } //出租车最多能载4个人 interface taxs{ int MAX_LOADMan =4; } //一般的汽车最高时速能达到150千米/小时 class fathercar{ int ints

  作者:weirou520  //小汽车最多能载5吨货物
  interface car{
    int MAX_LOADWare= 5;
  }
  //出租车最多能载4个人
  interface taxs{
    int MAX_LOADMan =4;
  }
  //一般的汽车最高时速能达到150千米/小时
  class fathercar{
   int intspeed =150;
   int drivespeed()
   {
   return intspeed;
   }
  }
  ///假如想要公共汽车有 小汽车的功能,也要有一般汽车,出租车的功能, 可以如下设置:
  //首先公共汽车(bus)继续一般汽车,然后实现car 和taxs 接口(因为一个类只答应有一个子类)
   class bus extends fathercar implements car,taxs{
    private int intware; //定义2个私有变量(可以认为是bus自身有的特性)
    private int intman;
    public int loadware()
  {
   intware=MAX_LOADWare;
   return intware;
  }
    public int loadman()
  {
   intman =MAX_LOADMan;
   return intman;
  } 
   }
    public class eat{
    public static void main(String[] args){
    bus nbbus= new bus();
    //打印公共汽车的功能
    System.out.println("the max drive speed of bus"+ nbbus.drivespeed());//最高速度
    System.out.println("the max ware weight of bus"+ nbbus.loadware());//最高载重量
    System.out.println("the max man number of bus"+ nbbus.loadman());//可以载4个人
   }
  
   }
  
精彩图集

赞助商链接