静态方法

 1 public class NeiBu {
 2     public static void main(String[] args) {
 3         System.out.println("Hello,This is Function main...");
 4         Zi z = new Zi();
 5         z.show();
 6     }
 7 }
 8 
 9 class Fu{
10     public static void show(){
11         System.out.println("Fu static...");
12     }
13 }
14 
15 class Zi extends Fu{
16     public static void show(){
17         System.out.println("Zi static...");
18     }
19 }

打印结果为:Zi static...

但是在第5行有黄色叹号出现,提示:The static method show() from the type Zi should be accessed in a static way

        原因:静态方法只要在类被加载后就被绑定到类上面了,所以第4,5行改为Zi.show();即可。

而非静态方法需要动态绑定到对象上才可以用。

       若第4行改为Fu z = new Zi();则打印结果为:Fu static...

 

       对于静态方法,是不需要对象的,只要用类名调用就可以!

 

posted @ 2012-10-23 17:31  洒洒  阅读(169)  评论(0编辑  收藏  举报