public class Test { |
int id; |
static int sid; |
//普通方法 |
public void a(){ |
System.out.println(id); |
System.out.println(sid); |
} |
|
//静态方法: |
//id不能访问的原因:static修饰的东西限于对象存在,在调用b()的时候, |
//对象很可能还没有创建,如果对象没有创建, |
//那么属于对象的id也没有存在呢,直接访问不行,报错! |
public static void b(){ |
//System.out.println(id);---》报错! |
System.out.println(sid); |
} |
|
public static void main(String[] args) { |
//static修饰的方法,有两种访问方式: |
//1.创建对象,对象名.方法名 |
Test t= new Test(); |
t.b(); |
//2.类名.方法名----》建议 |
Test.b(); |
|
b(); //在同一个类中,类名.可以省略不写! |
} |
} |
中级程序员
by: 云代码会员 发表于:2019-07-30 14:13:43 顶(0) | 踩(0) 回复
111
回复评论