接口传值的收藏!
来源:互联网 发布:联想手机淘宝旗舰店 编辑:程序博客网 时间:2024/05/16 12:26
原文出处:http://www.bdqn.cn/news/201312/12350.shtml
原文出处:http://my.oschina.net/onlytwo/blog/39193
interface
Runner
//定义接口
{
int
i=
3
;
public
void
start();
void
run();
void
stop();
}
interface
Eater
extends
Runner
//接口间可以继承
{
public
final
static
int
j=
4
;
void
openMouth();
void
upAndDown();
void
goIn();
}
class
TT
implements
Eater
//引用接口
{
public
void
start()
{
System.out.println(
"---------start()-------"
);
}
public
void
run()
{
System.out.println(
"---------run()-------"
);
}
public
void
stop()
{
System.out.println(
"---------stop()-------"
);
}
public
void
openMouth()
{
System.out.println(
"---------openMouth()-------"
);
}
public
void
upAndDown()
{
System.out.println(
"---------upAndDown()-------"
);
}
public
void
goIn()
{
System.out.println(
"---------goIn()-------"
);
}
}
public
class
TestInterface
{
public
static
void
main(String[] args)
{
Runner tt=
new
TT();
//接口的引用指向实现的对象
System.out.println(tt.i);
System.out.println(Runner.i);
tt.start();
Eater ee=
new
TT();
System.out.println(ee.j);
System.out.println(Eater.j);
ee.start();
}
}
1、接口方法用于回调 (这里定义接口是为了使用其接口方法):
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
public
interface
ICallback {
public
void
func();
}
public
class
Caller {
ICallback callback;
public
void
doCallback() {
callback.func();
}
public
void
setCallback(ICallback callback) {
this
.callback = callback;
}
}
public
class
MainClass {
public
MainClass() {
}
public
static
void
main(String[] args) {
Caller caller =
new
Caller();
caller.setCallback(
new
ICallback () {
public
void
func() {
System.out.println(
"dosth"
);
}
});
caller.doCallback();
}
}
2、向上转型
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
interface
People{
void
peopleList();
}
class
Student
implements
People{
public
void
peopleList(){
System.out.println(
"I’m a student."
);
}
}
class
Teacher
implements
People{
public
void
peopleList(){
System.out.println(
"I’m a teacher."
);
}
}
public
class
Example{
public
static
void
main(String args[]){
People a;
//声明接口变量
a=
new
Student();
//实例化,接口变量中存放对象的引用
a.peopleList();
//接口回调
a=
new
Teacher();
//实例化,接口变量中存放对象的引用
a.peopleList();
//接口回调
}
}
运行结果:
I’m a student.
I’m a teacher.
0 0
- 接口传值的收藏!
- 接口收藏
- 各种常用的JSON接口,值得收藏
- C#中的接口(收藏)
- 【收藏】以太网MAC和PHY之间的接口总结
- Mobile 输入法接口简介收藏
- mysql c 接口大全 收藏
- 函数的返回值 收藏
- 关于防止参数ID恶意传值的问题收藏
- 收藏的
- 收藏的
- 简单的接口传值
- 今天上午看到一篇非常好的,介绍 抽象与接口区别的文章,特别收藏
- 今天上午看到一篇非常好的,介绍 抽象与接口区别的文章,特别收藏
- 你必须收藏的快速学习Autodesk最新编程接口的免费录像
- 调用现有程序的接口(分享),发送和保存消息; 卸载程序的代码收藏
- [收藏]收藏的一些东西
- 朋友发了一篇关于接口跟抽象类的文章,觉得不错收藏一下
- 【poj 3321】Apple Tree 题意&题解&代码(C++)
- Initialization of variable was never used; consider replacing with assignment to ‘_’ or removing it
- imageLoader.getInstance().displayImage显示照片失败
- 解析xml
- iOS基础概念总结(一)
- 接口传值的收藏!
- [FractionalProgramming]分数规划
- Spring AOP 问与答
- redis、memcached
- DRM初始化过程
- ORMLite完全解析(二)官方文档第二章、如何使用ORMLite
- wifidog 源码初分析(二)
- 第11章评估产品机会——确定待解决的问题
- Hexo博客之旅