Core Java实例-接口抽象
2008-09-22 21:53
387 查看
接口的使用:
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 |
相关文章推荐
- java中抽象类、抽象方法、接口与实现接口实例详解
- 【Java类集】_Iterator接口笔记(实例亲测)
- 接口测试——Java + TestNG 国家气象局接口(json解析)实例
- [实例]JAVA调用微信接口发送图文消息,不用跳到详情页
- Java Core 接口和内部类
- java后台调用HttpURLConnection类模拟浏览器请求实例(可用于接口调用)
- Java的接口及 实例
- [置顶] Java基础你要知道的事情------抽象与接口
- java 面向对象 接口 抽象
- 【Java类集】_Iterator接口笔记(实例亲测)
- java的抽象以及接口知识点
- Java中的“抽象接口”
- Core Java笔记—第6章 接口与内部类
- Core Java (二十四) SortedMap,NavigableMap,SortedSet,NavigableSet接口
- 黑马程序员——Java基础:继承、final关键字、抽象、接口、多态
- Java——继承,抽象,接口
- java Lock接口详解及实例代码
- java后台调用HttpURLConnection类模拟浏览器请求实例(可用于接口调用)
- 为什么这些java接口没有抽象方法?浅谈Java标记接口
- Java接口的实例