java基礎(chǔ)的基礎(chǔ)
java基礎(chǔ)
static是靜態(tài)。
靜態(tài)方法不能訪問非靜態(tài)成員。
非靜態(tài)方法可以訪問靜態(tài)成員。
int i = 3;
public static void main(String[] args)?
{?
?System.out.println(i);
}
這個就會報錯。因為靜態(tài)方法不能訪問非靜態(tài)成員!!
?static int i = 3;
?public static void main(String[] args)
?{
??nbn n = new nbn();
??n.abc();
?}
?
?public void abc()
?{
??System.out.println(i);
?}
這個就不會報錯。因為非靜態(tài)方法可以訪問靜態(tài)成員。
?
public(訪問修飾符)static(訪問修飾符)void(返回類型) main(方法名)(String[] args)(參數(shù)表){}
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");//設(shè)置標(biāo)題
??jf.setSize(300,300);//設(shè)置窗體大小
??jf.setVisible(true);//顯示
??JOptionPane.showMessageDialog(jf(位置),"hello");//彈出窗口顯示HELLO
??String a = JOptionPane.showInputDialog(null,"can you see?");//輸入窗口,窗口提示為can you see
??
??JOptionPane.showMessageDialog跟JOptionPane.showInputDialog還有一種參數(shù)寫法。
??JOptionPane.showMessageDialog(null," "(顯示信息)," "(標(biāo)題信息),JOptionPane.INFORMATION_MESSAGE);
??System.out.println(sf.format(da.getTime())); //輸出年月日。用SimpleDateFormat
??
?}
}
三種注釋
/**
?*文檔註釋,寫在開頭 ( 注明程序的功能,以及相關(guān)信息)
?*功能:
?*作者:
?*版本:
?*開發(fā)日期:
?**/
/*
? *多行註釋
?
*///單行註釋
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);//類型轉(zhuǎn)換?
??s? = rr*rr*PI;
??JOptionPane.showMessageDialog(null,"圓面積是:"+s);
?}
}
類型轉(zhuǎn)換
String轉(zhuǎn)double
xx = Double.parseDouble(要轉(zhuǎn)換的數(shù)據(jù)名字);
String轉(zhuǎn)int
xx = Integer.parseInt(要轉(zhuǎn)換的數(shù)據(jù)名字);
int轉(zhuǎn)String
String s = String.valueOf(12);
或
String s = new Integer(10).toString();
注意!!
float f = 2.33 是錯的。因為2.33默認(rèn)類型是double.
改正:float f = 2.33 F;
還要注意
高內(nèi)聚,松耦合。
函數(shù)分解。
這樣代碼看起來會很清爽。
import java.text.DecimalFormat;
class Dformat
{
?public static void main(String[] args)
?{
??DecimalFormat df = new DecimalFormat("0");//格式0后面幾個小數(shù)點表示保留幾位小數(shù)
??System.out.println(df.format(66.6666)+"%");//這樣66.666就是67%
?}
}
標(biāo)準(zhǔn)輸出
System.out.print("");//加ln是換行
標(biāo)準(zhǔn)輸入
System.out.println(System.in.read());//這個方法用來讀取阿科斯碼
System.out.println((char)System.in.read());//這個方法是強(qiáng)制轉(zhuǎn)換,只能讀取一個字符
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);
?}
}
?要導(dǎo)入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);//今天是一周內(nèi)的哪一天
?
?
?
?
?
?
?
?
?
?
?