【JavaSE语法】类之间方法的调用(静态方法与非静态方法间的互相调用)

目录)

  • 在静态main方法中无法调用同类里的内部类(非静态)
  • Java类之间方法的调用
    • 一、静态方法调用其他方法
      • 1.静态方法调用非静态方法
      • 2.静态方法调用静态方法
      • 扩展,若导入外部静态方法
    • 二、非静态方法调用其他方法
      • 1.非静态方法调用类内部方法
      • 2. 非静态方法调用外部类方法
  • 总结

在静态main方法中无法调用同类里的内部类(非静态)

public class Code01_Trie {

    //该前缀树的路用数组实现
    class Trie{//前缀树类
 	}
    public static void main(String[] args) {
     Code01_Trie.Trie trie1 = new Code01_Trie.Trie();//错
     Trie trie1 = new Trie();//错,执行不了
     Code01_Trie trie1 = new Code01_Trie();//对,但调不到内部类
    }
}

解决方法,把内部类改为静态类

Java类之间方法的调用

一、静态方法调用其他方法

1.静态方法调用非静态方法

可见,不论是调用类内的非静态方法还是外部类的非静态方法,在静态方法中调用时只能用 对象名.方法名 调用。
对象只用一次的话可以用匿名对象

(1)类内部调用

public class Test01 {
    public void test01Method(){
        System.out.println("执行test01的非静态方法");
    }
    public static void main(String[] args) {
        //调用本类方法------------------------
        //错误调用非静态方法
        //test01Method();
        
        //匿名对象调用非静态方法
        new Test01().test01Method();
        
        //创建对象调用非静态方法
        Test01 t1 = new Test01();
        t1.test01Method();
    }
}

(2)从外部类调用

public class Test02 {
    public void test02Method(){
        System.out.println("执行test02的非静态方法");
    }
}
public class Test01 {
    public static void main(String[] args) {
		//调用同包不同类方法-------------
        new Test02().test02Method();
        
        Test02 t2 = new Test02();
        t2.test02Method();
    }
}

2.静态方法调用静态方法

类内部的静态方法可以直接调用,类外部的需要类名.静态方法名

  • (1)类内部调用
public class Test02 {
    public static void test02StaticMethod(){
        System.out.println("执行test02的静态方法");
    }
    public static void main(String[] args) {
        //调类内部静态方法
        test02StaticMethod();
    }
}
  • (2)从外部类调用
public class Test01 {
    public static void test01StaticMethod() {
        System.out.println("执行test01静态方法");
    }
}
public class Test02 {
    public static void main(String[] args) {
        //调用同包不同类的静态方法
        Test01.test01StaticMethod();
    }
}

扩展,若导入外部静态方法

可以import static导入外部类的静态方法,在本类的静态方法中就可以直接调用,不用类名.方法名

public class Test01 {
    public static void test01StaticMethod() {
        System.out.println("执行test01静态方法");
    }
}
import static review.demo02.Test01.test01StaticMethod;
public class Test02 {
    public static void test02StaticMethod(){
        System.out.println("执行test02的静态方法");
    }
    public void test02Method(){
        System.out.println("执行test02的非静态方法");
    }

    public static void main(String[] args) {
        //调类内部静态方法
        test02StaticMethod();
        
        //调用同包不同类的静态方法
        test01StaticMethod();

    }
}

二、非静态方法调用其他方法

1.非静态方法调用类内部方法

静态方法和非静态方法都直接调用即可

public class Test03 {
    public static void test03StaticMethod(){
        System.out.println("执行test03静态方法");
    }
    public void test03Method(){
        System.out.println("执行test03非静态方法");
    }
    
    public void method(){//非静态方法调用内部类方法
        test03StaticMethod();
        test03Method();
    }
    
    public static void main(String[] args) {
        new Test03().method();
    }

}

2. 非静态方法调用外部类方法

通过类对象调用该对象方法,通过类名调用静态方法
非静态方法调用外部非静态方法时,必须通过对象.方法名才可以调用。调用静态方法时,可以类.方法名也可以对象名.方法名
但建议静态方法还是使用类名调用,以免与对象方法混淆,且编译期间也会把调用静态方法的对象名.方法名改为类名.方法名

public class Test03 {
    public static void test03StaticMethod(){
        System.out.println("执行test03静态方法");
    }
    public void test03Method(){
        System.out.println("执行test03非静态方法");
    }
}
public class Test04 {
    public void method(){//非静态方法调用外部类方法
        new Test03().test03Method();
        Test03.test03StaticMethod();
    }
    public static void main(String[] args) {
        new Test04().method();
    }
}

总结

非静态方法调用内部类方法:一律直接调
非静态方法调用外部类方法:静态方法用类名.方法名。非静态方法用对象名.方法名
静态方法调用静态方法:内部类的方法直接调,外部类的方法用类名.方法名
静态方法调用非静态方法:一律用对象.方法名

你可能感兴趣的:(Java语法,java,开发语言)