开发者社区> 问答> 正文

为什么method 需要传对象否则不能获取属性?

class Work {
    static int num = 3;
    public static void main(String[] args) {

        Work t = new Work();
        t.num = 5;
        method(t);
        System.out.println(t.num);
        System.out.println(num);

    }
    public static void method(Work t){
        t.num = 6;
    }
}

展开
收起
蛮大人123 2016-05-27 10:28:20 1958 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    因为静态成员不能直接访问非静态成员,你可以这么写,不需要传

    class Work {
        static int num = 3;
        public static void main(String[] args) {
    
            Work t = new Work();
            t.num = 5;
            t.method();
            System.out.println(t.num);
            System.out.println(num);
    
        }
        public void method(){
            t.num = 6;
        }
    }
    2019-07-17 19:17:13
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载