回顾方法及加深

回顾方法及加深

笔记

  1. 方法的定义
  • 修饰符
  • 返回类型
  • Break:跳出swich 结束循环 和return的区别
  • 方法名:注意规范就OK 见名知意
  • 参数列表 :(参数类型,参数名)...
  • 异常抛出:后面讲解(疑问)
  1. 方法的调用:递归
  • 静态方法
  • 非静态方法
  • 形参和实参
  • 值传递和引用传递
  • this关键字

方法的回顾以及加深代码

import java.io.IOException;
//回顾方法及加深
public class Dome01 {
    public static void main(String[] args) {
    }
    /*
    修饰符  返回值类型  方法名(。。。)
        //方法体
         return 返回值
     */
    //return 结束方法,返回一个结果!
    public String sayHello(){
        return "hello world!";
    }
    public void print(){
        return ;//返回空
    }
    public int max (int a,int b){

        return a>b?a:b;//三元运算符
    }
    //数组下标越界:Arrayindexoutyofbounds
    public void redFile(String file)throws IOException {
    }
}

静态与非静态的比较

//静态方法与非静态方法1
public class Dome02 {
    public static void main(String[] args) {
        //实例化这个类new
        //对象类型  对象名 = 对象值
        // 非静态方法
        student Student = new student();
        student.say();
        //静态//静态方法static
        Student.say1();
    }
    public void a(){//可以直接调用a
        b();
    }
    //没有static 就是类实例化之后才存在
    public void b(){//可以直接调用b
        a();
    }
    //static 和类一起加载的
    public static void c(){
        //无法直接调用a();b();
    }
}

//静态方法与非静态方法2
//学生类
public class student {
    //非静态方法
    public static void say(){
        System.out.print("学生说话了");
    }
    //静态
    public void say1(){
        System.out.print("学生说话了");
    }
}

实参与形参

public class Dome03 {

    public static void main(String[] args) {
        //实际参数和形势参数的类型要对应
       int add = Dome03.add(a:1,b:2);
       System.out.print(add);
    }
    public static int add(int a,int b){
        return a+b;
    }
}

值传递

//值传递
public class Dome04 {
    public static void main(String[] args) {
        int a = 1;
        System.out.print(a);//1
        Dome04.chang(a);
        System.out.print(a);//1
    }
    public static void chang(int a){
        a = 10;
    }

引用传递

//引用传递:对象,本质还是值传递
public class Dome05 {
    public static void main(String[] args) {
        person Person = new person();

        System.out.print(Person.name);//null

        Dome05.change(Person);

        System.out.print(Person.name);//秦疆
    }
    public static void change(person Person){
        //Person 是一个对象:指向的--->person Person = new person();这是一个具体的人,可以改变属性!
        Person.name = "秦疆";
    }
}
//定义了一个person类,有一个属性 name
class person {
    String name;
}

posted @ 2021-07-28 22:06  柏阳光菜菜  阅读(36)  评论(0)    收藏  举报