關(guān)于靜態(tài)方法
為什么我直接調(diào)用靜態(tài)方法會(huì)報(bào)錯(cuò)
package com.Wujin;
public class Tel {
? ?String name;
? ?int no;
? ?static String ?num;
? ?// public Tel(){} ? //無參構(gòu)造方法
? ?public Tel(){name="yexiaoxia";} ?//有參構(gòu)造方法
? ?
? ?{
? no=10; ? ? ? ? ? ? ? //普通初始化模塊
? ?}
? ?
? ?static
? ?{
? num="5685968"; ? ? ? ? ? ? ? //靜態(tài)初始化模塊
? ?}
? ?
? ?public void print(){ ? ? ? ? ? ? ? ? ?//普通方法
? System.out.println(name);
? System.out.println(num);
? ?}
? ?
? ?public static void show() ? ? ? ? ? //靜態(tài)方法
? ?{
? System.out.println(num);
? Tel a=new Tel();
? System.out.println(a.name);
? ?}
? ?
}
package com.Wujin;
public class Wu {
public static void main(String[] args) {
// TODO Auto-generated method stub
Tel hello = new Tel();
hello.print();
Tel.show();
show();
}
}
2015-07-23
在同一個(gè)類中,可以直接訪問類中的靜態(tài)方法。不在同一個(gè)類中需要類名.方法名或者對(duì)象.方法名來實(shí)現(xiàn)調(diào)用。
2015-07-24