其实java方法就是c++的函数。
两者没重大区别。
区别:
1.Java中的方法定义顺序不做要求,即存在“先调用后定义”的情况,但是C中的函数是不支持这一点的。
2.方法中不能嵌套方法
3.方法是面向对象思想中针对对象属性的行为,函数是面向过程的一段程序。
代码实现:
1 package com.one;
2
3 public class hello {
4 int d;
5 // System.out.println("00"); 报错,因为类体只能定义变量,不能编写java语句
6 // 方法只能出现在类体当中
7 public static void main(String [] args) {
8 //main是主方法,是程序的入口 String []是一种引用数据类型 args是局部变量的变量名
9 int a,b;
10 a=1;
11 b=3;
12
13 System.out.println(hello.cal(a,b));
14
15 }
16 /*
17 方法的定义:
18 [修饰符列表]+返回值类型+方法名+(参数)
19 比如:
20 public static int Cal (int a,int c)
21
22 public static就是 修饰符列表
23 */
24 public static int cal (int a,int c) { //成员方法
25 // return true; 出错因为Java中不会将true转换成int
26 return a+c;
27 /*
28 假设该函数的类型是boolean,返回值是0或者是1时也会报错(和c++不同之处),
29 我的理解是Java中的方法,返回值应当和方法类型严格相同,不存在隐式转换
30 */
31
32 }
33 }
34