Java方法调用2

Java的方法调用2

在进行调用的时候,要注意静态类(static)和非静态类的调用规则

package oop;

public class Demo02 {
   public static void main(String[] args) {
       //跨文件调用非静态方法(public static void sayhello2)
       //在跨文件调用非静态类方法的时候要new一个类(new一个java文件名)的对象后就可以使用 类名.方法名来调用了
       Student student = new Student();
       student.sayhello2();

       //在跨文件调用静态类方法的时候,只需要类名.方法名就可以
       Student.sayhello();

       //在同文件下调用静态类方法,可以直接输入方法名使用
       hello();
       
       //在同文下调用非静态类方法,也是需要先new文件名,然后进行调用。
       Demo02 demo02 = new Demo02();
       demo02.world();
  }

   public static void hello(){
       System.out.println("hello000000");
  }

   public void world(){
       System.out.println("world111111");
  }
}

 

package oop;

public class Student {

   //静态方法,static是在类创建后就立即存在的,它的时间片特别早
   public static void sayhello(){
       System.out.println("hello world");
  }
   //非静态方法,在把对象实例化后才存在,也就是new之后才存在。
   public void sayhello2(){
       System.out.println("hello world222222");
  }
}
 
posted @ 2022-10-14 22:33  zhazhawei906  阅读(54)  评论(0)    收藏  举报