基础12回顾方法

回顾方法的定义和调用

回顾 方法:

方法

package com.oop.demo01;

import java.io.IOException;

//Demo01 类
public class Demo01 {
    //main  方法
    public static void main(String[] args) {

    }

    /*
    修饰符  返回值类型  方法名(...){
        //方法体
        return 返回值;
    }
     */
    //结束方法,返回一个结果
    public String sayHello() {
        return "Hello,world";
    }

    public void hello() {
        return;
    }

    public int max(int a, int b) {
        return a > b ? a : b;//三元运算符
    }
    //数组下标越界:Arrays index out of bounds
    //异常
    public void readFire(String file) throws IOException{

    }
}

静态方法和非静态方法

package com.oop.demo01;

public class Demo02 {
    public static void main(String[] args) {
        //静态方法 : 类.方法()
        Student.say();
        //非静态方法
        //实例化这个类 new
        //对象类型  对象名 = 对象值
        Student stu = new Student();
        stu.say1();
    }
    //static修饰时,和类一起加载
    public static void a(){

        //b();//此时不可调用
    }
    //类实例化之后才存在
    public void b(){

    }
}

接下

package com.oop.demo01;

//学生类
public class Student {
    //静态方法
    public static void say() {
        System.out.println("静态:学生说话了");
    }

    //非静态方法
    public void say1() {
        System.out.println("非静态:学生说话了");
    }
}

值传递和引用传递

package com.oop.demo01;

//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);
        change(a);
        System.out.println(a);//1
    }

    //返回值为空
    public static void change(int a) {
        a = 10;
    }
}
package com.oop.demo01;

//引用传递:对象 ,本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        Person per = new Person();
        System.out.println(per.name);//null
        Demo05.change(per);
        System.out.println(per.name);//天
    }

    //返回值为空
    public static void change(Person per) {
        //person是一个对象: 指向的 --->Person per = new Person();这是一个具体的人,可以改变属性!
        per.name = "天";
    }
}

class Person {
    String name;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值