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

在静态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;
import static review.demo02.Test01.*;
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();

    }
}
  • 普通导包格式:import 包路径.类名称;方便new类对象,调用类对象的方法
  • 静态导入格式:import static 包路径.类名称.*;
    这里多了个static,还有就是类名后面多了个 .* ,意思是导入这个类里的静态方法。
    例如:此处Test01和Test02类不在同一个包,所以普通导入和静态导入都做。若在同一个包,new类对象.普通方法不用导包,类名.静态方法名也不用导包。
    但想要直接调用静态方法还是需要静态导入
    在这里插入图片描述

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

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();
    }
}

总结

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

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值