java学习记录:关于方法

方法的重载

在一个类中,方法名相同,形参的个数不同或形参的类型不同的两个和两个以上的方法叫做重载。

方法重载不需要看返回值类型,也就是返回值类型可以不同

调用方法的重载时,从上往下依次查找对应的类型(实参的类型与个数 和 形参的数据类型与个数)

class Test{
    public int t1(int i,int j){
        return i+j;
    }
    //重载
    public int t1(int i,int j,int k){
        return i+j+k;
    }
    //重载
    public double t1(int i,double j){
        return i+j;
    }
}

方法的递归

简单的说就是方法自己调用自己

比如:求n的阶乘 n!

public class Test02{
    int TestN(int n){
        if(n<=0) return 0;
        if(n==1) return 1;
        //注意:递归条件很重要,写错可能导致结果不正确或者栈溢出。
        return n*(TestN(n-1));
    }
}
public class T2{
    public static void main(String[] args){
        Test02 t = new Test02();
        int result = t.TestN(5);
        System.out.println(result);
    }
}

方法的参数传递机制

方法中的参数传递有两种形式

1:形参是基本数据类型时(相当于传递过去的时这个数的值副本,不影响原来的值)

如:比较n在两个类中的差别

public class Test03{
    public void Test(int n){
        n=666;
        System.out.println("Test03当中n的值"+n);
    }
}
public class T3{
    public static void main(String[] args){
    int n = 555;
    Test03 t3 = new Test03();
    t3.Test(n);
    System.out.println("主类当中n的值"+n);
    }
}

2:形参时引用数据类型时(传过去的是地址值,影响原来的值)

如:这里用数组来举例

public class Test04 {
    public void Test04(int[] arr, int length){
        for(int i = 0;i<length;i++){
            //我们把里面的值全部乘以2,看结果
            arr[i]*=2;
        }
        //遍历输出
        System.out.println("打印数组修改后的值");
        for(int i = 0;i<length;i++){
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
    }
}
public class T4 {
    public static void main(String[] args){
        int[] arr = {1,2,3,4,5};
        System.out.println("打印数组原来的值");
        for(int i = 0;i<arr.length;i++){
            System.out.print(arr[i]+"\t");
        }
        System.out.println();

        Test04 t4 = new Test04();
        t4.Test04(arr,arr.length);

        //遍历输出
        System.out.println("因为传的是地址值,所以数组中原来的值也会变");
        for(int i = 0;i<arr.length;i++){
            System.out.print(arr[i]+"\t");
        }
    }
}

本文作者:傻孩子不吃辣

本文链接:https://www.cnblogs.com/Asillychild/p/16504565.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   傻孩子不吃辣  阅读(24)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 The Flood Joshua Hyslop
The Flood - Joshua Hyslop
00:00 / 00:00
An audio error has occurred.

作词 : Joshua Hyslop

作曲 : Joshua Hyslop

Here comes the flood again

Watch it fall from the sky

Feel it soak through my flesh and my blood

Feel it burn in my eyes

When I say how much more can I take?

I know the water’s rising up, watch the waves crest and break

And though I’ve made nothing but a sound

I fear that I may drown

I fear that I may drown

.

Here comes the wind again

Cold that cuts to the bone

Pack my bags and I’ll head out the door

Here I am on my own

When I say how much more can I stand?

I know my walls are falling down; I left the rocks and chose the sand

And though I’ve no one left to blame

Still, I cursed your name

I cursed your name

.

So now the end

What I’ve been running from

Though I’ve tried I cannot lift my head

Oh, what have I become?

When I say how much more can I take?

I know my time is running short, I am broken and I’ll break

And though I’ve worn myself so thin

I’m coming home again

And though I do not know my heart

Well, I know myself down to my bones, but if my bones should come apart

Then I’ll have nothing left to give

But if you take me in,

I’m coming home again

If you take me in

I’m coming home again