zoukankan      html  css  js  c++  java
  • "静态方法里仅仅能调用静态变量和静态方法"具体解释

    静态方法里能够调用静态方法和静态变量,同一时候也能调用非静态方法和非静态变量。

    public class Test {
    
    public Test() {};
    public Test(int i) {this.n = i;}
    
    public static int m = 5;
    public int n = 10;
    
    public void fun1() {System.out.println("非静态方法fun1");}
    public static void fun2() {System.out.println("静态方法fun2");}
    
    //測试“静态方法里仅仅能调用静态变量和静态方法”详细是什么意思
    public static void main(String[] args) {
     //调用静态方法不会报错
     //fun2();
    
     //调用静态变量不会报错
     System.out.println(m);
    
     //无法从静态上下文中引用非静态方法fun1()
     //fun1();
    
     //无法从静态上下文中引用非静态变量n
     //System.out.println(n);
    
     //能够在静态方法中new对象,调用到了非静态方法的构造方法。无论是否默认构造方法。下列代码均能正确运行。故觉得:“编译阶段,编译器不会检查静态方法中牵扯到 详细对象以及对象的相关操作”。如此,也提供了静态方法中訪问非静态方法和非静态属性的解决方式。
     //Test t = new Test(8);
     //t.fun2();
     //t.fun1();
     //System.out.println(t.m);	
     //System.out.println(t.n);
     
    }
    }


查看全文
  • 相关阅读:
    阅读心得10:《京东咚咚架构演进 》
    hadoop beginning
    ubuntu docker
    Ubuntu学习——第一篇
    flexsim diary
    apollo 3.0 硬件系统
    这是一份详细的Apollo自动驾驶平台上手指南
    详解百度Apollo感知技术、高精地图和小度车载系统
    Apollo 2.5推基于动态的实时相对地图解决方案
    Apollo在功能安全方面的探索
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10800450.html
  • Copyright © 2011-2022 走看看