接口
来源:互联网 发布:桂圆淘宝 编辑:程序博客网 时间:2024/04/29 00:45
平时用C/C++和java相对较多,一般在c++中用到的接口就是指成员函数,用接口访问成员变量比直接访问成员变量安全一些。
例如一个类中有
class test{
public :
int x,y;
//一下四个函数就是接口
int getX();
int getY();
void setX();
void setY();
}
在java中的接口则不一样,它是先要定义一个接口,用关键字interface标注,并且文件名要和接口名保持一致
接口只能用来定义常量和方法。
例如:
(public)interface Circle//要么缺省public,要么写上public,不能用其他的来修饰interface
{
final float PI = 3.14f;
float GetArea(float r);
float GetLength(float r);
}
在实现接口的类中要将接口中的每一个方法都实现:
如:
public class MyCircle implements Circle{
float x,y;
float GetArea(float r){
return PI*r*r;
}
float GetLength(float r){
return 2.0f*PI*r;
}
void test(){
//其他函数方法的实现
}
public static void main(String args[])
{
MyCircle c = new MyCircle();
float area = c.GetArea(2.0f);
System.out.println(Float.toString(area));
}
}
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 接口
- 简易文本编辑器分析
- strchr
- C++图结构的图结构操作示例
- U-Boot启动过程完全分析
- poj 2840 Big Clock
- 接口
- 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
- Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)
- 无线高清显示技术
- 下列应用程序,产生两个组合框,一个可以选择月份,一个可以选择2000~2009年之间的年份。
- 自动刷新页面
- Spring3整合Hibernate4 Annotation详细图文教程
- 问题1
- [安卓破解]汉字转拼音,无广告,破解积分限制