方法的调用和创建,小总结

方法的定义与调用

package com.lol.LiOu.oop;

import java.io.IOException;

//Demo01  类
public class Demo01 {

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

    }

    public String sayHello(){
        return "Hello,world";
    }
    public void hello(){
        return;
    }
    public int max(int a, int b){
        return  a>b  ? a:b; //三元运算符!
    }



    //数组下标越界:Arrayindexiytofbounds
    public void readFile(String file) throws IOException{

    }
package com.lol.LiOu.oop;

public class Demo02 {

    //静态方法 static

    //非静态方法
    public static void main(String[] args) {
        //要想不报仇就需要实例化这个类



        //Student.say();
        Student student = new Student();


        student.say();
    }
    //static和类一起加载
    public static void a(){
        //b();
    }

    //要new(实例化)之后才能存在
    public  void b(){
        a();
    }

}
package com.lol.LiOu.oop;

//学生类
public class Student {

    //静态方法,如去掉static变为非静态方法,那么Demo02的引用会报错
    //public  static void  say(){
    public   void  say(){
        System.out.println("学生说话了");
    }


}

package com.lol.LiOu.oop;

public class Demo04 {
    public static void main(String[] args) {

        int a = 1;
        System.out.println(a);


        Demo04.change(a);

        System.out.println(a);
    }

    //返回值为空
    public static void  change(int a){
        a = 10;
    }

}
package com.lol.LiOu.oop;


//引用传递
public class Demo05 {
    public static void main(String[] args) {
        Perosn perosn = new Perosn();
        System.out.println(perosn.name);//null
        Demo05.change(perosn);
        System.out.println(perosn.name);//秦疆

    }
    public static void  change(Perosn perosn){
        //perosn是一个对象:指向的--->Perosn perosn = new Perosn();这是一个具体的人,可以改变属性!
        perosn.name = "秦疆";
    }
}

//定义了一个perosn类,有一个属性:name
class Perosn{
    String name;//默认值null
}
package com.lol.LiOu.oop;

public class Demo03 {
    public static void main(String[] args) {
       int add = Demo03.add(1,2);
        System.out.println(add);
    }

    public static int add(int a,int b){
        return  a+b;


    }
}

![屏幕截图 2020-12-12 152424](C:\Users\LOL\Desktop\屏幕截图 2020-12-12 152424.png)![屏幕截图 2020-12-12 152256](C:\Users\LOL\Desktop\屏幕截图 2020-12-12 152256.png)

posted @ 2020-12-12 15:31  JAVA初当力  阅读(80)  评论(0编辑  收藏  举报