Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1708394
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: Java

2008-08-10 22:55:00

1.   接口

1.1. 代码

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

package javatutorials;

 

/**

 *

 * @author wanpor

 */

//创建Shape接口

interface Shape{

    public void draw();

}

//创建Action接口

interface Action{

    public void hit();

}

//实现两个接口

class Wce implements Shape,Action{

    public void draw(){

        System.out.println("Wce draw");

    }

    public void hit(){

        System.out.println("Wce hit");

    }

}

public class Interface {

    public static void main(String[] args){

        Wce ce = new Wce();

        ce.draw();

        ce.hit();

        Shape sp = (Shape)ce;

        Action ac = (Action)ce;

        sp.draw();

        ac.hit();  

    }

 

}

1.2. 说明

1.       接口的声明,将方法声明为public

2.       使用关键字interface

3.       一个类可以实现多个接口

4.       使用关键字implements

5.       使用接口访问Shape sp/Action ac

文件:Interface.zip
大小:0KB
下载:下载
阅读(969) | 评论(0) | 转发(0) |
0

上一篇:Java枚举类型

下一篇:Java继承

给主人留下些什么吧!~~