package com.test.day01;
//
public class TestParam {
public void f1(int n){
n =0;
}
public static void main(String[] args) {
TestParam testParam = new TestParam();
int n = 9;
testParam.f1(n);
System.out.println(n);
} }

思考:上面的代码运行结果输出的是0还是9  答案是9    因为调用一个方法就会在栈里面开辟空间,这时候0在一个空间 9在一个空间。那么结果不会随着方法的调用而改变

那么什么时候会改变呢?

package com.test.day01;
//
class Param{
int value;
}
public class TestParam {

  //讲Param类作为类型
public void f2(Param p){
p.value=99;
}
public static void main(String[] args) {
TestParam testParam = new TestParam();
Param pc = new Param();
pc.value=22;
testParam.f2(pc);
System.out.println(pc.value);
} }

思路:引用传递是地址的传递,这里面使用了同一个地址,也就是都指向99 所以结果是99  如果在 f2里面再次创建一个对象的话,就会在堆里面创建对象,就会有新的地址。结果就会是22

使用引用参数求5个数的和:

  

package com.test.day01;
// public class TestParam {
//创建一个求和的方法
public int num(int n1,int n2,int n3,int n4,int n5){
return n1+n2+n3+n4+n5;
}
public static void main(String[] args) {
//求5个数的和
TestParam tp = new TestParam();
int sum = tp.num(88, 99, 54, 78, 90);
System.out.println(sum);
}
}

上述的代码虽然完成5个数的求和,但是如果遇到很多个数求和那么显然这样的方式是不可取的。那么怎么解决呢?传参的时候使用引用数据类型数组然后for循环的方式;

package com.test.day01;
// public class TestParam {
//创建一个求和的方法 //定义一个求和的方法
public int sum(int [] arr){
int sum =0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
TestParam testParam = new TestParam();
int [] a={6542,3,5,7,4,2,4,67,8,3,56,8,534,3,2,6,6,8,9,4,2};
int num = testParam.sum(a);
System.out.println(num);
} }

定义一个数组,存储3名学员的基本信息,编写方法计算学员的总成绩。

package com.test.day01;
class Student{
//姓名 性别 年龄 分数
double scoer;
char sex;
String name;
int age;
}
//管理类
class Manager{
public double sum(Student [] stus){
int sum =0;
for (int i = 0; i < stus.length; i++) {
sum += stus[i].scoer;
}
return sum;
}
}
public class TestStudent { public static void main(String[] args) {
// TODO Auto-generated method stub
Student xiaolongnv = new Student();
xiaolongnv.scoer=100;
Student mucheng = new Student();
mucheng.scoer=84.5;
Student zzhangruocheng = new Student();
zzhangruocheng.scoer=69.3;
Student [] stus={xiaolongnv,mucheng,zzhangruocheng};
double num = new Manager().sum(stus);
System.out.println(num);
} }

可变参数

可变参数的底层是数组  他的优点是传参灵活,注意多个参数的时候可变参数只能放在最后。

public class TestVarParam {

    public void f(String s ,int [] arr,int [] arr1) {
System.out.println(Arrays.toString(arr));
}
public void ff(int ...arr ) {// int []arr
System.out.println(Arrays.toString(arr));
System.out.println(arr.length);
}
public static void main(String[] args) {
TestVarParam test = new TestVarParam();
// 1
// int [] arr = {11,22,33};
// int [] arr = new int[]{11,22,33};
// test.f(arr);
// 2
// test.f(new int [] {11,22,33});
// 3.
test.ff();// new int []{}; 长度为0 的数组
test.ff(111);// new int[]{111}
test.ff(11,22,33,44);// new int[]{11,22,33,44};
test.ff(new int[] {11,22}); } }

递归

递归就是方法自身调用自身,但是一定要有出口

package com.test.day01;

public class Recursion {

    int count=0;
public void f(){
count++;
if(count==10){
return;
}
System.out.println("慢慢学,这要学的快");
f();
} public static void main(String[] args) {
// 使用递归的方法输出10个“慢慢学,这要学的快”
Recursion re = new Recursion();
re.f();
} }

使用递归实现阶乘

package com.test.day01;

public class Factorial {
public int fa(int n){
if(n==1){
return 1;
}else{
return n*fa(n-1);
}
}
public static void main(String[] args) {
// 使用递归实现阶乘 阶乘形式:1*2*3*4*5*6*7
Factorial factorial = new Factorial();
System.out.println(factorial.fa(6));
} }

java--带参方法 递归阶乘的更多相关文章

  1. java面向对象入门之带参方法创建

    /* Name :创建带参的方法 Power by :Stuart Date:2015.4.25 */ //创建Way类 class Way{ //Way类成员的基本变量 int add1=123; ...

  2. 我的java之路week2类的无参、带参方法

    2.1语法 public 返回值类型 方法名(){ //方法体 } 2.2方法的调用语法 对象名.方法名 计算平均分和总成绩 public class Score { /** * 创建类 ScoreC ...

  3. 带参方法的执行:普通方法的查询,可为空方法的查询。批量处理SQL语句。

    普通方法的查询: @Override public List<Map<String, Object>> selectSpentAmount(Integer MAT_TYPE_, ...

  4. 接口中带参方法,传入IB类型的数据

    不同的接口有不同的方法 不同的类有不同的作用 不同的作用产生不一样的效果 不同的效果让程序看似复杂,实际简单... 比如此程序,看似复杂,实际就那么点事: 谁生成了谁,谁设置了谁,谁传入了谁,谁被谁调 ...

  5. 慕课网-Java入门第一季-7-5 Java 中带参无返回值方法的使用

    public class HelloWorld { public static void main(String[] args) { // 创建对象,对象名为hello HelloWorld hell ...

  6. Java 中带参无返回值方法的使用

    有时方法的执行需要依赖于某些条件,换句话说,要想通过方法完成特定的功能,需要为其提供额外的信息才行.例如,现实生活中电饭锅可以实现“煮饭”的功能,但前提是我们必须提供食材,如果我们什么都不提供,那就真 ...

  7. thymeleaf的初次使用(带参请求以及调用带参js方法)

    之前对于前端框架接触较少,第一次接触thymeleaf,虽说看起来并不复杂但我还是花费了好一会儿才弄懂. 话不多少下面就简单说一下我在项目中的应用. 首先是java代码 controller层 将需要 ...

  8. Java中的方法应用

    一.如何定义java中的方法 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 语法: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 public.protected.priv ...

  9. Java中的方法(形参及实参)return返回类型

    如何定义 Java 中的方法 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 一般情况下,定义一个方法的语法是: 其中: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 pub ...

随机推荐

  1. 如何实现一个 windows 桌面动态壁纸

    ​ 更新: 2018/08/31 WS_MOUSE_LL 钩子,实现底层壁纸交互效果. 一.介绍 国内玩家第一次看到动态壁纸,都是出于一款来自 Wallpaper Engine 的 Steam 程序. ...

  2. 提高python运行效率的方法

    让关键代码依赖于外部包:你可以为紧急的任务使用C.C++或机器语言编写的外部包,这样可以提高应用程序的性能 使用生成器,因为可以节约大量内存 多个if elif条件判断,可以把最有可能先发生的条件放到 ...

  3. MongoDB之安装部署

    一.安装MongoDB 在安装MongoDB之前,应该先把MongoDB官方网站上下载下来,下载的地址如下: https://www.mongodb.com/download-center 下载完毕之 ...

  4. Java基础系列 - 抽象类,子类继承

    package com.company; /** * 抽象类继承 * 用abstract修饰类就是抽象类 * 用abstract修饰方法就是抽象方法(一般使用比较少) * 抽象类不能被实例化 */ p ...

  5. PyTricks-函数参数的解包操作

    def myfunc(x, y, z): print(x, y, z) tuple_vec = (1, 0, 1) dict_vec = {'x': 1, 'y': 0, 'z': 1} >&g ...

  6. linuxfind命令按照文件大小搜索的注意事项

    [root@localhost ~]# man find -size n[cwbkMG] File uses n units of space. The following suffixes can ...

  7. routine的加载

    // Hearthbuddy.Windows.MainWindow // Token: 0x06000245 RID: 581 RVA: 0x0008C318 File Offset: 0x0008A ...

  8. Java同步数据结构之ConcurrentHashMap

    前言 这是Java并发包最后一个集合框架的数据结构,其复杂程度也较以往任何数据结构复杂的多,顾名思义ConcurrentHashMap是线程安全版本的HashMap,总所周知HashMap是非线程安全 ...

  9. Linux中ctrl+z 、ctrl+c、 ctrl+d区别

    Ctrl + C 和Ctrl + Z都是中断命令,但是他们的作用却不一样. Ctrl + C 是强制中断程序的执行,进程已经终止. Ctrl + C 发送 SIGINT信号 参考:linux信号 Ct ...

  10. 使用Docker快速搭建Tensorflow开发环境

    当我刚开始学习使用scikit-learn时,总是会出现各种各样的包依赖问题,兜兜转转了一遍才全部安装好,现在的机器学习算法开发者大都使用tensorflow.pytorch来实现自己的想法,但依然会 ...