Android源码底层的代码实现文件有.h、.c、.cpp后缀名文件,它们分别是c、c++程序代码文件和头文件。下面就以一个极其简单的小例子来对比理解它们之间关系。
C++是面向对象,JAVA是一切皆对象!
1. 在java中,假如我要实现一个函数getHeight(int height)返回的是传入的height
1). 在A.java文件中声明和定义函数的实现,代码如下:
- public class A
- {
- public int getHeight(int height)
- {
- return height;
- }
- }
- public class B
- {
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- A a = new A();
- a.getHeight(180);
- }
- }
2.对比在C++中实现同样的函数:
1). 在a.h文件中声明类和函数,实现如下:
- class A
- {
- pubic:
- int get_height(int height);
- };
2).在 a.cpp文件中的进行函数的定义,代码如下:
- #include "a.h"
- int A::get_height(int height)
- {
- return height;
- }
3).在 main.cpp文件中实现调用函数get_height(int height),代码如下:
- #include "a.h"
- void main()
- {
- A a;
- a.get_height(180);
- }