java接口小结
By  凌云志 发表于 2007-10-28 13:21:00 

类A是一个接口,类B是一个接口,类AImpl是类A接口的一个实现,类BImp是类B的一个实现,请看

A.java:

package abc;
public interface A {
 public static final String TEST_STRING = "hello world";
 public void methed1();
 public int methed2();
}

B.java

package abc;
public interface B {
 public static final String TEST_INT = "123456";
 public void methed11();
 public int methed22();
}

AImpl.java:
package abc;
public class AImpl implements A {
 public void methed1() {
  // TODO Auto-generated method stub
 }
 public int methed2() {
  // TODO Auto-generated method stub
  return 0;
 }
 public static void main(String[] args) {
  AImpl ai = new AImpl();
  System.out.println(TEST_STRING);
  System.out.println(ai instanceof A);
 }
}

class BImpl implements A, B{
 public void methed1() {
  // TODO Auto-generated method stub
 }
 public int methed2() {
  // TODO Auto-generated method stub
  return 0;
 }
 public void methed11() {
  // TODO Auto-generated method stub
 }
 public int methed22() {
  // TODO Auto-generated method stub
  return 0;
 }
 public static void main(String[] args) {
  System.out.println(TEST_INT);
 } 
}

小结:

1.接口只可以定义static final成员变量,在实现类中可以直接访问。
2.instanceof 运算符可以用来决定某对象的类是否实现了接口。
3.类AImpl(接口的实现)必须实现类A(接口)的所有方法。
4.类BImpl(接口的实现)必须实现类A(接口)和类B(接口)类的所有方法。
 
阅读全文 | 回复(0) | 引用通告 | 编辑

发表评论:

    密码:
    主页:
    标题:
    页面数据正在载入...

 
站点公告
莫忘:锻炼身体;提高技能;修身养性。
欢迎光临:www.javaedu.com
与我联系:beanor@gmail.com
站点日历
<<  < 2007 - 10 >  >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
最新日志
最新评论
最新留言
友情链接
站点统计
日志搜索
用户登陆

 
bxna 京ICP备05002321号