04747 Java
Published:
Java
第一章 Java概述
- JVM 是一台虛擬計算機, 屏蔽了具體平台的差異
javac a.java b
java b
import java.util.Scanner;
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
第二章 數據和表達式
- byte :1
- short 2
- int:4
- long:8
- float:4
- double:8
第五章 數組和字符串
String s1="a";
String s2="a";
s1.equal(s2); // true
s1==s2; //true
String s3=new String("a");
s1==s3; // false
s1.equal(s3); //true
繼承與多態
BankAccount a=new BankAccount("a",123);
BankAccount b=new BankAccount("a",123);
a==b; //false
a.equal(b); //false
BankAccount c=a;
c==b; //true
c.equal(b); //true
class A extends B
{
super(s); // 調用B的構造函數
}
第六章 多態
superClass a=new superClass();
subClass b=new subClass();
superClass c=new subClass();
c.method(); // 調用子類
final
是終態, 不能有子類,也不能改變- final 變量也會被繼續到子類
final public class FinalClass
{
}
abstract class E
{
void put(char c);
char get();// 不定義方法體
}
- 不能直接由接口來創建對象,而必須通過由實現接口的類來創建
public interface I
{
}
第七章 輸入和輸出流
- InputStream , OutputStream 是抽象類
try
{
}
catch(FileNotFoundException e)
{
}
catch(EOFException e)
{
}
catch(IOException e)
{
}
// ObjectOutputStream ObjectInputStream
import java.io.*
File OutputStream f=new FileOutputStream("datee.ser");
ObjectOutputStream s=new ObjectOutputStream(f);
s.writeObject(d);
s.close();
File OutputStream f=new FileOutputStream("datee.ser");
ObjectInputStream s=new ObjectInputStream(f);
d=(Date) s.readObject(d);
s.close();
- 能夠記錄自己的狀態以便將來得到復原能力, 稱為對象的永久性
- 把對象變字節序列的過程稱為對象的序列化
- 把字節序列恢復為對象的過對稱反序列化
- 變量是另一對象的引用, 則引用的對象也要序列化, 過程是遞歸
import java.io.*
public class A implements Serializable
{
}
transient
標明的成員變量是不會被保存
基本字節流
- Reader, Writer是抽象類
- InputStream, OutputStream, 參數是字符或字符數組
- BufferedWriter使用flush(),強制清空緩沖區中的剩內容, 防止漏洞
FileInputStream f=new FileInputStream("datee.ser");
InputStreamReader s=new InputStreamReader(f);
BufferedReader reader =new BufferedReader(s);
while(a=reader.readLine()!=null)
{
System.out.PrintIn(a);
}
s.closed
BufferedReader reader =new BufferedReader(new FileReader("aa.txt"));
while(a=reader.readLine()!=null)
{
System.out.PrintIn(a);
}
s.closed
第八章 圖形界面設計
- AWT重型, Swing輕型
JFrame frame=new JFrame("demo");
// the first method
Container contentPane=frame.getContentLane();
contentPande.add(button,BorderLayout.CENTER);
// the second method
JPanel contentPane=new JPanel();
contentPande.add(button,BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setvisible(true);
java.awt
setLayout(new FlowLayout(Flow.Left,20,40))
// FlowLayout.Left, Right, Center
// FlowLayout(int align, int hgap, int vgap);
BorderLayout(int hgap, int vgpap)
// NORTH, SOUTH,EAST, WEST, CENTER
frame getContentPane).add(new JButton("A"),BorderLayout.South)
contentPane.setLayout(new GridLayout(2,3))
new GridLayout(int rows, int cols,int hgap, int vgap)
// rows cols=0 根據實際情況
java.awt
CardLayout(int hgap, int vgap);
myCard=new CardLayout();
contentPane.setLayout(myCard);
myCard.show(contentPane,"First");
java.swing
//BoxLayout.X_AXIS, BoxLayout.Y_AXIS
//BoxLayout(Container target , int axis)
pv=new JPanel();
pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS))
Box.createHorizationalGlue()
Box.createHorizationStrut(20);
Box.createRigidArea(new Dimension(50,90));
contentPane.setLayout(null);
b1=new JButton("m0");
b1.setBounds(30,60,70.20);
setBounds(int x, int y, int width, int height)
事件處理
b1=new JButton("m0");
b.addActionListener(new ButtonHandler());
public class ButtonHandler implements ActionLister
{
public void actionPerformed(ActionEvent e)
{
}
}
frame.addMouseMotionListener(this);
frame.addMouseListener(this);
// MouseMotionListener
void mouseDragged(MouseEvent e)
void mouseMoved(MouseEvent e)
// MouseListener
void mouseClicked(MouseEvent e)
void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)
ActionListener, actionPerformed(ActionEvent e)
TextListener, textValueChanged(TextEvent e)
圖形
drawArc(int x, int y, int startAngle , int arcAngle)
drawRect(int x, int y , int width ,int height)
fillArc(int x, int y, int startAngle , int arcAngle)
fillPolygon(int []xs , int[]ys,int nPoint)
fillRect(int x, int y , int width ,int height)
panel.repaint();
第九章 Swing 組件
String[] itemList={"a","n"}
jcb1=new JComoBox<String>(itemList)
jcb1.setSelectedIndex(1);
ActionListener al=new ActionListener()
{
public void actionPerformed(actionEvemt e)
{
jComoBox jcb=(JComoBox) e.getSorce();
if (jcb==jcb1)
{
}
}
}
jcb1.addActionListener(al);
- textArea
t1.setSelectedTextColor(Color.red);
ta2.setEditable(false);
- 菜單
JMenuItem m1=new JMenuItem("a",KeyEvent.VK_S);// Ctrl+S
JMenu menu=new JMenu("File");
menu.add(m1)
menu.addSeparator();
meunItem.addActionListener(this);
cm.addItemListener(this);
public void itemStatChanged(ItemEvent e)
{
int state=e.getStateChange();
JMenuItem aa=(JMenuItem)e.getSource();
String command=aa.getText();
if (state==ItemEvenmt.Selected)
tf.setText();
}
menuItem.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
tf.setText(e.getActionCommand());
if (e.getActionCommand=="Exit")
{
System.exit(0)
}
}
- 對話
JDialog(frame,"Dialog",true)
- 選文件
public void actionPerformed(ActionEvent e)
{
int select=fc.showOpenDialog(frame);
if (select==JFileChooser.APPROVE_OPTION)
{
File file=fc/getSelectedFile();
}
}
第十章 多線程
- 新建, 可運行狀態, 死亡, 阻塞
- 創建線程的兩種方法
- 可繼承thread
- 可用Runnable 接口
Thread th1=new Thread(t1,"test");
th1.start()
class TwoThread implements Runnable
{
public void run()
{
}
}
TwoThread tt= new TwoThread()
- start
- run
- yeild:終止
- isAlive
- sleep
- void wait
- wait() 等待直至notify(), notifyAll()
- join():引起現行線程等待, 直至join()方法所調用的線程結束
- 對象對鎖
- synchronized()的參數是this
public void push(char c)
{
synchronized(this)
{}
}