静态方法中调用非静态方法
来源:互联网 发布:linux系统iso镜像下载 编辑:程序博客网 时间:2024/05/07 20:46
我们都知道,静态static方法中不能调用非静态non-static方法,准确地说是不能直接调用non-static方法。但是可以通过将一个对象的引用传入static方法中,再去调用该对象的non-static方法。
其实这个事实的应用很经常,以至于我们不去重视:在主函数(static方法)中我们经常创建某个类的实例,再利用其饮用变量调用它的非静态方法。
//StaticMethodTest.java
//A ststic method cannot call a non-static method, but we can transfer a object reference, which include a non-static metho to the static method, thus, wo can call that non-static method in a indirect way.
//A ststic method cannot call a non-static method, but we can transfer a object reference, which include a non-static metho to the static method, thus, wo can call that non-static method in a indirect way.
public class StaticMethodTest{
void NonStaticMethod(){
System.out.println("This is a non-sataic method.");
}
static void StaticMethod(StaticMethodTest s){
System.out.println("This is a static method.");
s.NonStaticMethod();
}
static void StaticMethod(StaticMethodTest s){
System.out.println("This is a static method.");
s.NonStaticMethod();
}
public static void main(String[] args) {
StaticMethodTest sObj=new StaticMethodTest();
StaticMethod(sObj); //在主函数中可以直接调用静态方法
}
}
StaticMethod(sObj); //在主函数中可以直接调用静态方法
}
}
阅读全文
0 0
- 静态方法中调用非静态方法
- 静态方法中调用非静态方法
- 静态方法中调用非静态方法
- 静态方法中调用非静态方法
- 静态方法中调用非静态方法
- 静态方法中调用非静态方法
- main方法中调用非静态方法
- java中静态方法中调用非静态方法
- java中静态方法中调用非静态方法
- java中静态方法中调用非静态方法
- java中静态方法中调用非静态方法
- java中静态方法中调用非静态方法
- java中静态方法中调用非静态方法
- java静态方法中调用非静态方法的实现
- 在静态方法中如何调用非静态方法
- java中 静态方法和非静态方法的调用
- SpringMVC中静态方法调用非静态方法
- Java_静态方法中调用非静态方法
- 数据分析之《菜鸟侦探挑战数据分析》-1-R语言-均值方差t检验
- 递归
- 基于遗传算法的最优化仿真(Java)
- 算法分析
- 实验二 词法分析器的实现
- 静态方法中调用非静态方法
- codeforces888e(折半枚举+二分搜索)
- 阿D下载效验
- codeblocks:使用多个头文件和源文件会遇到的问题
- RMI与RPC的区别
- Chrome中input自动填充问题
- IDEA自动部署WEB项目至Tomcat-原理解析
- 【学习笔记】单例模式
- com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:178) com.alibaba.dubbo.config.Re