Java蜘蛛人 歡迎大家

          歡迎大家 來到我的blog , 如果我身邊的朋友 有什么不懂可以直接來問我 我會細心的幫助你的. 如果網絡上的朋友有什么不懂的 可以加我Java蜘蛛人 QQ48187537
          posts - 54, comments - 192, trackbacks - 0, articles - 1
            各位同學. 這段時間沒有寫東西 是忙了點.. 今天就教大家使用static

          首先看一段代碼.這個代碼就是不用main函數也可以輸出...
          public class Hello
          {
              
          static
              
          {
                  System.out.println(
          "Hello,word");
                  System.exit(
          0);  //如果不加上這行代碼會得到一個" main is not defined"的錯誤
              }

              
          }



          下面更具體點的函數...注意 必須要用static 靜態的才能完成.. 否則就必須要寫main函數了...

          public class Yanshi
          {
              Yanshi()
              
          {
                  
              }

              
          public static int haha()
              
          {
                  
          int r=3*10;
                  
          return r;

              }

              
          private static int id=haha();
              
          static 
                 
          {
                   System.out.println(id);
                       System.exit(
          0);
                 }

          }


            具體的static 方法. 我明天在和大家講解吧.~ 今天先睡了.... 明天請繼續來收看我的blog...



          大家好.!  今天又來了.   大家看下面的代碼就會懂了  static  謝謝大家

          我先定義一個接口 和一個抽像的類.

          1public interface SinterFace {
          2   public final static String intstr="hello is interface";
          3   public abstract void nostat();
          4}

           

           

           1public class Useinter implements SinterFace {
           2
           3    int inter = 5;
           4    static String str = "hello is class";
           5    public double dou = 1.0;
           6    
           7    public  void nostat() {
           8        System.out.println("is no static method");
           9    }

          10
          11    public static void stat() {
          12        System.out.println("is static method");        
          13    }

          14
          15}



           

           1public class Main {
           2
           3    public static void main(String[] args) {
           4        
           5        System.out.println("靜態變量方法在類實例化之前都可以調用:");
           6        Useinter.stat();
           7        System.out.println(Useinter.str
           8                +"\n");
           9        
          10        System.out.println("非靜態變量方法要在類實例化之后才可以調用:");        
          11        Useinter useinter = new Useinter();
          12        useinter.nostat();
          13        System.out.println(useinter.dou);
          14        System.out.println("類中沒聲明是private的變量默認都是public");
          15        System.out.println(useinter.inter);
          16
          17
          18        
          19        System.out.println("\n類里的靜態變量可以被實例調用:");
          20        System.out.println(useinter.str
          21                  +"\n");
          22       
          23        System.out.println("接口里的靜態變量可以直接被調用:");    
          24        System.out.println(SinterFace.intstr);
          25    
          26    }

          27
          28}



          這是靜態導入類.

          1import  static java.lang.System.*;//如果前面像這樣定義了.那么請看下面
          2public class Test 
          3{
          4  public static void main (String[] args)
          5    {
          6      out.println("你好"); //這樣就不用加System.
          7       
          8    }
          9}

          還有 如果對Math使用靜態導入的話:
          sqrt(pow(x,2)+pow(y,2))
          看起來比
          Math.sqrt(Math.pow(x,2)+Math.pow(y,2))
          清晰的多.. 呵呵.

                                                                                                                                                                    __原創作者: 鄭成橋


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 增城市| 无棣县| 高陵县| 延寿县| 铜梁县| 南京市| 宾川县| 定远县| 江津市| 苍南县| 霍邱县| 郧西县| 新野县| 信阳市| 南丰县| 鱼台县| 金坛市| 新源县| 剑阁县| 汝南县| 东城区| 内江市| 文安县| 成都市| 手游| 绿春县| 龙海市| 宣威市| 五家渠市| 赣州市| 合阳县| 临汾市| 南川市| 贵州省| 昌平区| 葫芦岛市| 盱眙县| 乾安县| 石楼县| 乌鲁木齐县| 明光市|