Chinaunix首页 | 论坛 | 博客
  • 博客访问: 542660
  • 博文数量: 855
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 19:08
文章分类

全部博文(855)

文章存档

2011年(1)

2008年(854)

我的朋友

分类:

2008-10-16 19:19:23

接口的使用:

1 多态的情况下使用接口:分为编译时和运行时的状态。

2 注意对象的相同性。

3 强制转换的情况。

package com;

public interface Animal {

}

package com;

/***
*
* 鸟类
*
* @author Administrator
*
*/
public class Bird implements Animal {

public Bird() {

}

public String color;
private int age;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}


}


package com;

public class SamllBird extends Bird {

}





package com;

import java.lang.reflect.InvocationTargetException;


public class Test {


public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

Animal b = new Bird();
b.toString();

Bird bird = (Bird)b;
bird.setColor("red");

System.out.println(bird.getColor());

System.out.println(b==bird);

Animal sb = new SamllBird();
Bird bb = (Bird)sb;
System.out.println(sb==bb);

System.out.println(b instanceof Animal);
System.out.println(bird instanceof Animal);
System.out.println(sb instanceof Animal);




}

}

运行结果:

red
true
true
true
true
true
【责编:Chuan】

--------------------next---------------------

阅读(202) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~