java基礎的基礎

          java基礎

          static是靜態。
          靜態方法不能訪問非靜態成員。
          非靜態方法可以訪問靜態成員。

          int i = 3;
          public static void main(String[] args)?
          {?
          ?System.out.println(i);
          }
          這個就會報錯。因為靜態方法不能訪問非靜態成員!!

          ?static int i = 3;
          ?public static void main(String[] args)
          ?{
          ??nbn n = new nbn();
          ??n.abc();
          ?}
          ?
          ?public void abc()
          ?{
          ??System.out.println(i);
          ?}

          這個就不會報錯。因為非靜態方法可以訪問靜態成員。

          ?

          public(訪問修飾符)static(訪問修飾符)void(返回類型) main(方法名)(String[] args)(參數表){}

          import javax.swing.*;
          import java.text.SimpleDateFormat;
          class jframe
          {
          ?public static void main(String[] args)
          ?{
          ??JFrame jf = new JFrame();//窗口
          ??
          ??SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd");//時間格式
          ??java.util.Date da = new java.util.Date();//顯示時間的對象申明
          ??
          ??
          ??jf.setTitle("w.a.n");//設置標題
          ??jf.setSize(300,300);//設置窗體大小
          ??jf.setVisible(true);//顯示
          ??JOptionPane.showMessageDialog(jf(位置),"hello");//彈出窗口顯示HELLO
          ??String a = JOptionPane.showInputDialog(null,"can you see?");//輸入窗口,窗口提示為can you see
          ??

          ??JOptionPane.showMessageDialog跟JOptionPane.showInputDialog還有一種參數寫法。
          ??JOptionPane.showMessageDialog(null," "(顯示信息)," "(標題信息),JOptionPane.INFORMATION_MESSAGE);
          ??System.out.println(sf.format(da.getTime())); //輸出年月日。用SimpleDateFormat
          ??
          ?}
          }


          三種注釋
          /**
          ?*文檔註釋,寫在開頭 ( 注明程序的功能,以及相關信息)
          ?*功能:
          ?*作者:
          ?*版本:
          ?*開發日期:
          ?**/

          /*
          ? *多行註釋
          ?
          *///單行註釋


          import javax.swing.JOptionPane;
          class Area
          {
          ?final private static double PI = 3.1415;? //常量的申明:final
          ?private String r;
          ?private double rr;
          ?private double s;
          ?
          ?
          ?public static void main(String[] args)
          ?{
          ??Area a = new Area();
          ??a.Input();
          ??a.Areas();
          ?}
          ?
          ?public void Input()
          ?{
          ??r? = JOptionPane.showInputDialog(null,"請輸入圓半徑");
          ?}
          ?
          ?public void Areas()
          ?{??
          ??rr = Double.parseDouble(r);//類型轉換?
          ??s? = rr*rr*PI;
          ??JOptionPane.showMessageDialog(null,"圓面積是:"+s);
          ?}
          }

          類型轉換

          String轉double
          xx = Double.parseDouble(要轉換的數據名字);
          String轉int
          xx = Integer.parseInt(要轉換的數據名字);

          int轉String
          String s = String.valueOf(12);

          String s = new Integer(10).toString();

          注意!!
          float f = 2.33 是錯的。因為2.33默認類型是double.
          改正:float f = 2.33 F;

          還要注意
          高內聚,松耦合。
          函數分解。
          這樣代碼看起來會很清爽。


          import java.text.DecimalFormat;
          class Dformat
          {
          ?public static void main(String[] args)
          ?{
          ??DecimalFormat df = new DecimalFormat("0");//格式0后面幾個小數點表示保留幾位小數
          ??System.out.println(df.format(66.6666)+"%");//這樣66.666就是67%
          ?}
          }


          標準輸出
          System.out.print("");//加ln是換行
          標準輸入
          System.out.println(System.in.read());//這個方法用來讀取阿科斯碼
          System.out.println((char)System.in.read());//這個方法是強制轉換,只能讀取一個字符

          import java.io.*;//用BufferedReader 要引用io包
          class c
          {?
          ?public static void main(String[] args)throws Exception //要拋異常
          ?{
          ?BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          ?String s = br.readLine();
          ?System.out.println(s);
          ?}
          }
          ?要導入util包,util是工具包.
          ?Scanner sc = new Scanner(System.in);
          ?int s = sc.nextInt();
          ?int s1 = sc.nextInt();
          ?System.out.println(s*s1);//用Scanner可以省去判斷的步驟 在jdk1.5以下的版本不能用

          GregorianCalendar calendar = new GregorianCalendar(year,month,day);
          int d = calendar.get(Calendar.DAY_OF_WEEK);//今天是一周內的哪一天

          ?

          ?

          ?

          ?

          ?

          ?

          ?

          ?

          ?

          ?

          ?

          posted on 2007-01-11 21:10 閱讀(255) 評論(0)  編輯  收藏


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


          網站導航:
           
          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 牙克石市| 二连浩特市| 台安县| 称多县| 新宁县| 承德县| 宣化县| 大港区| 谢通门县| 太湖县| 越西县| 达拉特旗| 安顺市| 合水县| 温州市| 汉川市| 丹江口市| 大理市| 天津市| 福鼎市| 河池市| 永定县| 绥滨县| 吉林省| 班戈县| 临朐县| 姜堰市| 江西省| 和田县| 丹凤县| 长岛县| 康定县| 泗水县| 伊金霍洛旗| 双辽市| 田东县| 丰原市| 翁源县| 马尔康县| 双城市| 天台县|