2019-9-10【Javase】方法、this、成员变量和局部变量、带参方法、参数传递、引用参数

本文详细介绍了Java中的方法定义、好处及注意事项,包括方法的调用方式。此外,还讲解了`this`关键字的含义及其在调用成员变量和方法时的作用。接着,探讨了成员变量与局部变量的区别,强调了它们的作用域、初始化和优先级。文章进一步阐述了带参方法的定义和参数传递机制,区分了值类型和引用类型的传递差异。最后,通过实例展示了引用参数在实际操作中的应用,如数组的求和与反转等。
摘要由CSDN通过智能技术生成

一、方法

1.语法:

在这里插入图片描述
返回值类型(基本,引用)
public void(无返回值类型) (参数 ) { 方法体; // 功能实现}

2.方法的好处:

1.代码的重用;
2.便于维护;
3.代码的安全性。

public String getComputer() {// "电脑”
    return "HP电脑";
}

3.一个文件定义多个类注意:

1.只能有一个类定义类public的;
2.public修饰的类名 与 文件名一致;
3.public 修饰的类 定义主方法。

4.注意:

1.返回的值的类型与声明的类型要相符;
2.只能返回一个值。
3. 不同的类方法调用:
必须new创建对象, 调用方法。
4.同一个类的方法调用:
自定义方法之间调用,可以使用this,也可以省略
主方法main调用其他的自定义方法,必须new对象调用,
(原因:main是static的,不能使用this)

5.方法调用:

1)不同类必须创建对象new 调用方法;
2)同一个类中,自定义方法 可以直接互相调用,可以使用this,也可以省略;
同一个类,主方法 调用 其它的实例成员方法,必须显示new 创建对象去调用。因为 main是static.

二、this

表示 本类对象。

作用: 1) 调用成员变量 和成员方法;
2)成员变量和 局部变量同名,使用this区分调用成员变量。
在这里插入图片描述
在这里插入图片描述

public class Person {
    // 属性: 成员变量 
    String name;
    int age;
    // 方法
    // 跑 方法的定义,声明
    public void run() {
        System.out.println("跑步");
    }
    public void say() {
        System.out.println("我是一名学生");
    }
    // 
    public String getComputer() {// "电脑”
        return "HP电脑";
​
    }
    public void show() {
//      Person guojing = new Person();
        // this: 本类对象                       显示的使用了this 
//      System.out.println("我是:" + this.name +",我的年龄是:" + this.age  );
        //                           隐式的用了this  this.name this.age
        System.out.println("我是:" + name +",我的年龄是:" + age  );
    }
/*  public int[] getNumber() {
        int [] arr = {11,22,33};
        return arr;
    }*/
    
    
}
public class TestPerson2 {
​
    public static void main(String[] args) {
        Person guojing = new Person();
        guojing.name = "郭靖";
        guojing.age = 18;
        System.out.println(guojing.name + "," + guojing.age);
        // 方法的调用
//      guojing.run();
//      guojing.say();
        System.out.println("跑步");
        System.out.println("我是一名学生");
        // 对返回值处理了
        String str = guojing.getComputer();
        System.out.println(str);// 
        //
        guojing.show();
    }
    
​
}

三、成员变量和局部变量的区别

变量:
局部变量:在方法中定义的变量。
成员变量:在类中定义的变量。
1)实例成员变量(对象)
2)静态成员变量(static )

区别:

1.作用域。
成员变量 在整个类中 都是有效的;
局部变量 只能在它声明的代码块中有效。
2.初始值。
成员变量系统自动初始化。
局部变量 必须自己初始化,系统不管;
3.优先级。
成员变量和局部变量同名,在局部变量作用域内,优先使用局部变量。

class Demo{
    public void f() {
        TestDemo2 t = new TestDemo2();
        System.out.println(t.x);
    }
}+
public class TestDemo2 {
    int x = 11 ;// 成员变量,声明处初始化
    public void f1() {
        int n  = 11;// 局部变量
        System.out.println(n);
        System.out.println(x);
    }
    public void f2() {
        for(int i = 1; i <= 3; i++) {
            System.out.println(i);
        }
        System.out.println(x);
    }
    public void f3() {
        int x = 22;// 局部变量
//      System.out.println(x); // 22优先使用局部
        System.out.println(this.x);
    }
    public static void main(String[] args) {
        TestDemo2 t = new TestDemo2();
//      System.out.println(t.x);
        t.f3();
​
    }
​
}

四、带参方法定义

参数定义:

在这里插入图片描述
参数传递:
在这里插入图片描述
大的整数:

public BigInteger Fac1(int n) {
    BigInteger sum = new BigInteger("1");
    for(int i = 1; i <= n ; i++) {
        sum = sum.multiply(BigInteger.valueOf(i));
    }
    return sum;
}

五、方法参数传递

1、值类型

public class TestParam {
    public void f1(int n) {// int n= n;int n = 11;
        n = 22;
    }
    public static void main(String[] args) {
        TestParam test = new TestParam();
        int n = 11;
        test.f1(n);
        System.out.println(n);// 11
    }
​
}

2、引用类型

1)两个引用名称 (对象名) 指向了同一个对象。

class Param{
    int value;
}
public class TestParam {
    public void f2(Param p) {// Param p = p;
        p.value = 22;
    }
    public static void main(String[] args) {
        TestParam test = new TestParam();
        //2.
        Param p = new Param();
        p.value = 11;
        test.f2(p);
        System.out.println(p.value);//22
​
    }
​
}

2)两个引用指向了不同的对象。

class Param{
    int value;
}
public class TestParam {
    public void f3(Param p ) {// Param p = p;
        p = new Param();// 
        p.value = 22;
    }
    public static void main(String[] args) {
        TestParam test = new TestParam();
        //3.
        Param p = new Param();
        p.value = 11;
        test.f3(p);
        System.out.println(p.value);// 11
​
    }
​
}

六、引用参数

1)5个数求和。

public class TestParam1 {
​
/*  public int sum(int n1,int n2,int n3,int n4,int n5) {
        return n1 + n2 + n3 + n4 + n5;
    }*/
    public int sum(int[] arr) {// int[] arr = arr;
        int sum = 0;
        for(int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }
    public static void main(String[] args) {
        TestParam1 test = new TestParam1();
//      System.out.println(test.sum(11, 22, 22, 44, 66));
        int []arr = {1,2,3,4,5};
        System.out.println(test.sum(arr));// arr 存了 地址
    }
​
}

2)5个数反转

public int [] reverse(int[]  arr) {
        int [] arr1 = new int[arr.length];
        for(int i = 0,j = arr.length-1; i < arr1.length; i++,j--) {
            arr1[i] = arr[j];
        }
        return arr1;
    }       
    public static void main(String[] args) {
        TestParam1 test = new TestParam1();
        int [] arr = {1,2,3};
        System.out.println(Arrays.toString(test.reverse(arr)));
    }

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

class Student{
    String name;
    int age;
    int score;
}
class StuManager{
    // Student stu1 = guojing;Student stu2 = yangkang;
/*  public int sum(Student stu1,Student stu2,Student stu3) {
        return stu1.score + stu2.score + stu3.score;
    }*/
    public int sum(Student [] stus) {
        int s = 0;
//      return stus[0].score + stus[1].score + stus[2].score;
        for(int i = 0; i < stus.length; i++) {
            s = s + stus[i].score;
        }
        return s;
    }
}
public class TestStudent {
    public static void main(String[] args) {
        Student guojing = new Student();
        guojing.score = 100;
        Student yangkang  = new Student();
        yangkang.score = 90;
        Student huangrong = new Student();
        huangrong.score = 100;
        StuManager manager = new StuManager();
        Student [] stus = {guojing,yangkang,huangrong};
        int n = manager.sum(stus);
        System.out.println(n);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值