1 / 128

Java Programming Matt Tsai

Java Programming Matt Tsai. JavaOne. Outline. SDK and tools Object-Oriented Programming Java concept. Java Development Kit (JDK). JDK 1.4 ( http://java.sun.com ) Other tools — kawa ,UltraEdit,EditPlus jar xvf src.jar jar cvf myClass.jar test1.class [test2.class] …. JDK 主要程式.

tallys
Download Presentation

Java Programming Matt Tsai

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming Matt Tsai

  2. JavaOne

  3. Outline • SDK and tools • Object-Oriented Programming • Java concept

  4. Java Development Kit (JDK) • JDK 1.4 (http://java.sun.com) • Other tools—kawa,UltraEdit,EditPlus • jar xvf src.jar • jar cvf myClass.jar test1.class [test2.class]…

  5. JDK主要程式 • javac:java的編譯器,source code->byte code • javac HelloWorld.java HelloWorld.class • java:java的解譯器 • java HelloWorld • appletviewer:java的解譯器,可以執行HTML檔案中的applet程式 • 在HelloWorld.html檔案中加入 <applet code=HelloWorld.class width=300 height=400></applet> • appletviewer HelloWorld.html • 其他:javadoc,jdb,javah,javap,etc.

  6. Java程式的執行 • 假設JDK安裝在c:\jdk1.4 • autoexec.bat中加入 • Set path= %path%;.;c:\jdk1.4\bin • Classpath以前舊的版本要加,現在的版本不用加,除非是自己定義的且不在程式目前所在的目錄 • Set classpath=%classpath%;d:\myJava

  7. Java程式的執行(classpath) Set classpath=%classpath%;d:\myJava E:\Hello.java D:\myJava\iecs.java import iecs; public class Hello{ public static void main(String args[]){ iecs myIECS=new iecs(); System.out.println(myIECS.Information()); } } public class iecs{ public iecs(){ } public String Information(){ return " Hello World!!!"; } } D:\myJava\java iecs.java  E:\javac Hello.java  E:\java Hello  Hello World!!!

  8. Java程式的執行(package) Set classpath=%classpath%;d:\myJava E:\Hello.java D:\myJava\edu\fcu\iecs\iecs.java import edu.fcu.iecs.*; public class Hello{ public static void main(String args[]){ iecs myIECS=new iecs(); System.out.println(myIECS.Information()); } } D:\myJava\jar cvf myJava.jar edu Set classpath=%classpath%;d:\myJava\myJava.jar package edu.fcu.iecs; public class iecs{ public iecs(){ } public String Information(){ return " Hello World!!!"; } } D:\myJava\edu\fcu\iecs\java iecs.java  E:\javac Hello.java  E:\java Hello  Hello World!!!

  9. Object-Oriented Programming • 封裝 • 資訊隱藏 • 繼承性 • 多型:同一個函式在多個類別中 • 繼承 • Overloading(同名但參數相異) • 介面

  10. private int ID; private String name; getInformation() setup(…) reset() 封裝,資訊隱藏 Class MyObject{ private int ID; private String name; public void getInformation(){ System.out.println(ID+”:”+name); } public void setup(int ID,String name){ this.ID=ID; this.name=name; } private void reset(){ } } MyObject

  11. 封裝,資訊隱藏 public class Test{ public static void main(String argv[]){ MyObject MyObj=new MyObject(); MyObj.setup(123,”ChinYiTsai”); System.out.println(MyObj.ID+”:”+MyObj.name); MyObj.getInformation(); MyObj.reset(); } }

  12. A 繼承性 B C D E F

  13. public class Parent{ Parent(){ } public void funcation1(){ System.out.println(“Parent”); } } public interface MyInterface{ } public class Child extends Parent { public void function1(){ System.out.println(“child”); } public void function2(){ System.out.println(“child child”); } } implements MyInterface

  14. Java Concept • First Program(Hello World) • Java 類別函式庫 • Abstract class and Interface • Inner class • Static • this

  15. //import java.io.*; //import javax.swing.*; public class HelloWord{ public static void main(String argv[]){ System.out.println(“Hello Word”); } } 檔名必須為HelloWorld.java 程式的進入點 不必先new一個實體即可使用

  16. 繼承JFrame類別 import javax.swing.*; import java.awt.*; public class HelloWord extends JFrame{ JButton Button_hello; JLabel Label_hello; HelloWord(){ Container con=getContentPane(); con.setLayout(new FlowLayout()); Button_hello=new Jbutton(“Hello Button”); Label_hello=new Jlabel(“Hello Label”); con.add(Label_hello); con.add(Button_hello); } public static void main(String argv[]){ HelloWord helloword=new HelloWord(); helloword.setBounds(0,0,400,400); helloword.setVisible(true); } } 設定版面管理 建構子 加入Container 實體化

  17. Java類別函式庫 • java.applet • java.io • java.awt • javax.swing • java.awt.event • org.omg.CORBA • …

  18. Abstract class and Interface • Abstract class • 不能直接使用,須以繼承(extends)的方式才可以 • Interface • 以implements的方式來實作

  19. public interface windowListener extends EventListener{ public void windowOpened(WindowEvent e){ } public void windowClosing(WindowEvent e){ } public void windowClosed(WindowEvent e){ } public void windowIconified(WindowEvent e){ } public void windowDeiconified(WindowEvent e){ } public void windowActivated(WindowEvent e){ } public void windowDeactivated(WindowEvent e){ } } public WinHandler implements windowListener{ public void windowOpened(WindowEvent e){ } public void windowClosing(WindowEvent e){ System.exit(1); } public void windowClosed(WindowEvent e){ } public void windowIconified(WindowEvent e){ } public void windowDeiconified(WindowEvent e){ } public void windowActivated(WindowEvent e){ } public void windowDeactivated(WindowEvent e){ } }

  20. Inner class public class A{ int ID; A(){ } public void A2(){ } class B{ B(){ A2(); } } } Class A Class B

  21. public class Sample{ private JTextArea show_textatea; private JButton button; Sample(){ Container con=getContentPane(); show_textarea=new JTextArea(20,20); button=new Jbutton(“show”); con.add(button); con.add(show_textatea); button.addActionListener(new ActionHandler()); } class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ show_textarea.append("textfield\n"); } } }

  22. public class Sample{ JTextArea show_textatea; JButton button; Sample(){ Container con=getContentPane(); show_textarea=new JTextArea(20,20); button=new Jbutton(“show”); con.add(button); con.add(show_textatea); button.addActionListener(new ActionHandler(this)); } } class ActionHandler implements ActionListener{ Sample samp; ActionHandler(Sample samp){ this.samp=samp; } public void actionPerformed(ActionEvent e){ samp.show_textarea.append("textfield\n"); } }

  23. 匿名內部類別 • public class sample{ • Sample(){ • … • button.addActionListener(new ActionListener(){ • public void actionPerformed(ActionEvent e){ • show_textarea.append("textfield\n"); • } • }); • } • }

  24. Static • 類別載入即自動建立 • 不須實體化即可使用 public class TryWin{ static JFrame aWin=new JFrame(“This is the Window”); public static void main(String args[]){ aWin.setBounds(0,0,400,400); aWin.setVisible(true); } } JFrame aWin=new JFrame(“This is the Window”);

  25. public class Color extends Object implements Paint, Serializable{ public static final Coloryellow public static ColorgetColor(Stringnm) … } JFrame MyFrame=new JFrame(); Color myColor=new Color(); MyFrame.setBackground(myColor.yellow); MyFrame.setBackground(myColor.getColor(“yellow”));

  26. JavaTwo

  27. Outline • Object-Oriented Programming • Class and Object • Abstract class and interface • Books • Practice • Schedule

  28. Object-Oriented Programming • 封裝 • 資料隱藏 • 繼承性

  29. private int ID; private String name; getInformation() setup(…) reset() 封裝,資訊隱藏 Class MyObject{ private int ID; private String name; public void getInformation(){ System.out.println(ID+”:”+name); } public void setup(int ID,String name){ this.ID=ID; this.name=name; } private void reset(){ } } MyObject

  30. 封裝,資料隱藏 public class Test{ public static void main(String argv[]){ MyObject MyObj=new MyObject(); MyObj.setup(123,”ChinYiTsai”); System.out.println(MyObj.ID+”:”+MyObj.name); MyObj.getInformation(); MyObj.reset(); } }

  31. A public class A{ public void A1(){} public void A2(){} } A1 A2 繼承性 B public class B extends A{ public B3(){} } A1 A2 B3 C public class C extends B{ public void B3(){…} public void C4(){} } A1 A2 B3 C4

  32. 不可以被繼承 • final • public static final int FINAL • public final class Test • static • public static int ID; • public static boolean isStatic(intmod) • private • private int ID • private funtion1()

  33. Class and Object 存取控制

  34. public class MyObject { private int ID; private String name; public void getInformation(){ System.out.println(ID+”:”+name); } public void setup(int ID,String name){ this.ID=ID; this.name=name; } private void reset(){ } } public class Test { public static void main(String argv[]){ MyObject MyObj=new MyObject(); MyObj.setup(123,”ChinYiTsai”); System.out.println(MyObj.ID+”:”+MyObj.name); MyObj.getInformation(); MyObj.reset(); } }

  35. public class System extends Object{ public static final PrintStreamout; . . . } public class HelloWord{ public static void main(String argv[]){ System.out.println(“Hello Word”); } } 程式的進入點 public class PrintStream extends FilterOutputStream{ public void println(boolean x){…} public void println(char x) {…} public void println(char[] x) {…} public void println(double x) {…} public void println(float x) {…} public void println(int x) {…} public void println(long x) {…} public void println(Object x) {…} public void println(String x) {…} … } 屬於類別,不屬於任何物件

  36. Class and Object • Class • 類別欄位 • 類別方法 • 實體欄位 • 實體方法 • 成員類別 • Object: Class1 obj=new Class1();

  37. 類別欄位 實體欄位 類別方法 實體方法 成員類別 public class Test{ public static int class_ID; public int obj_ID; public ststic void class_Function(){ } public void obj_Function(){ } class Test1{ … } }

  38. 類別轉換 • 子類別型別的物件可視為父類型別的物件 • Object o=new Object() • String s=new String() • o=s (子->父) ; s=(String)o (父->子)

  39. 內部類別 • 成員類別 • 與所處類別的實體相關連 • 不可含有static欄位,方法,類別 • 介面不可被定義為成員類別 • 匿名類别 • 沒有名字 • 類別很小 • 使用一次 • 馬上使用

  40. 內部類別 public class A{ int ID; A(){ } public void A2(){ } class B{ B(){ A2(); } } } Class A Class B

  41. public class Sample{ private JTextArea show_textatea; private JButton button; Sample(){ Container con=getContentPane(); show_textarea=new JTextArea(20,20); button=new Jbutton(“show”); con.add(button); con.add(show_textatea); button.addActionListener(new ActionHandler()); } class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ show_textarea.append("textfield\n"); } } }

  42. Mybutton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println(“click button”); } } ); public class ActionHandler implements ActionListener{

  43. Abstract class and Interface • Abstract class • 至少含有一個抽象方法,也可以有已實作的方法 • Interface • 介面中的方法是抽象方法,若沒有完全實作,則會變作抽象類別 • 可多重繼承與實作

  44. public abstract class Car{ public abstract void move(); public abstract void stop(); public void back(){ System.out.println(“back”); } } public class March extends Car{ March(){ } public void move(){ } public void stop(){ back(); } }

  45. public interface user{ public void setNumber(int num); public void setTime(int time); public void getInformation(); } public class student implements user{ public void setNumber(int num){… } public void setTime(int time){… } public void getInformation(){… } … } public abstract class student2 implements user{ public void setTime(int time){… } public void getInformation(){… } … }

  46. public class Test extends Object implements Interface1,Interface2,Interface3,Interface4{ … } public interface MyInterface extends Interface1, Interface2,Interface3{ … }

More Related