Java-面向对象_static修饰方法

package com.gouzao;

public class Static_function {
    int id;
    static int sid;
    
    public void a() {
        System.out.println(id);
        System.out.println(sid);
        System.err.println("------a");
    }
    
    //static和public都是修饰符,并列没有先后顺序
    public static void b() {
//        System.out.println(this.id); 在静态方法中不能使用this关键字
//        a() 在静态方法中不能访问非静态方法
//        System.out.println(id); 在静态方法中不能访问非静态属性
        System.out.println(sid);
        System.out.println("--------b");
    }
    
    public static void main(String[] args) {
        
        //非静态方法只能使用对象名.方法名调用
        Static_function sf = new Static_function();
        sf.a();
        
        //静态方法可以用类名.方法名调用或者对象名.方法名调用
        Static_function.b();
        sf.b();
        
        //在同一个类中可以直接调用方法
        b();
        
    }
}

运行:

 

posted @ 2021-02-26 16:19  别看我看路  阅读(60)  评论(0编辑  收藏  举报