1、HelloWorld
package ch04; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.Console; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.regex.Pattern; public class HelloWorld { // enum WEEKDAY {ONE,WEO,THREE}; public static void main(String arg[]){ /** int express verison & StringBuilder **/ System.out.println("HelloWorld!!"); int i = 100_00_00; int j = 0b1001; char k = '\u2122'; String CodePointString = "12345中文aa"; StringBuilder a_StringBuilder = new StringBuilder(); // Single Thread version of StringBuffer a_StringBuilder.append(CodePointString); a_StringBuilder.append(k); /** Scanner **/ String a_testString = "1 hehe 2 cat dog hehe # hehe"; Pattern mPattern = Pattern.compile("\\s*hehe\\s*"); Scanner mScanner = new Scanner(a_testString).useDelimiter(mPattern); Scanner mScanner = new Scanner(System.in); // Scanner mScanner = new Scanner(a_testString); boolean isNotEnd = true; while(isNotEnd&&mScanner.hasNext()){ if(mScanner.hasNextInt()){ System.out.println(mScanner.nextInt() + "##"); } else{ String nextString = mScanner.next(); if(!nextString.equals("end")){ System.out.println(nextString); }else{ isNotEnd = false; } } } mScanner.close(); /** Console **/ //TODO Not Make Console Success Start Console mConsole = System.console(); String username = mConsole.readLine("Username:"); char[] password = mConsole.readPassword("Paaword:"); StringBuilder a_passwordBuilder = new StringBuilder(); for(char s : password ){ a_passwordBuilder.append(s); } System.out.println(username); System.out.println(a_passwordBuilder.toString()); //TODO Not Make Success End String a_stringName = mScanner.next(); System.out.println(i); System.out.println(j); System.out.println(WEEKDAY.ONE); System.out.println(CodePointString.codePointCount(0,CodePointString.length())); //True Length System.out.println(CodePointString.length()); System.out.println(a_StringBuilder.toString()); System.out.println(a_StringBuilder.reverse().toString()); System.out.printf("%tc", new Date()); //星期三 七月 29 19:01:12 CST 2015 /** File Operate **/ try { PrintWriter out = new PrintWriter("myfile2.txt"); out.println("Hello World~~!"); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } Scanner mTxtScanner = null; try { mTxtScanner = new Scanner(Paths.get("myfile2.txt")); if(mTxtScanner.hasNext()){ System.out.println(mTxtScanner.nextLine()); } } catch (IOException e) { e.printStackTrace(); } /** Date **/ Date mDate = new Date(); Calendar mCalender = Calendar.getInstance(); System.out.println(mDate.toString()); int year = mCalender.get(Calendar.YEAR); int day_of_year = mCalender.get(Calendar.DAY_OF_YEAR); System.out.println(year + " " + day_of_year); /** Final Value **/ final String a = "gege"; // a = "d"; // Error for " can not assign a value to final variable 'a' " final String[] ab = new String[]{"12","23"}; ab[1] = "df1"; ab[0] = "df2"; // Can change In this way String[] b = new String[]{"er1","er2"}; // ab = b; // Error for " can not assign a value to final variable 'ab' " /** Array to String & use random() **/ String[] a_test = new String[3]; for(int i=0; i<3; i++){ a_test[i] = String.valueOf(Math.random()); } for(String aa : a_test){ System.out.println(aa); } System.out.println(Arrays.toString(a_test)); /** ArrayList **/ ArrayList<Employees> staffList = new ArrayList<>(); staffList.add(new Employees()); staffList.add(new Employees("harry", 1234.56, 2000, 9, 0)); for(Employees e : staffList){ System.out.println(e.toString()); } staffList.set(0, new Employees("alter", 234.678, 1027, -1, 8)); for(Employees e : staffList){ System.out.println(e.toString()); } //Alter ArrayList to Array Employees[] a_storage = new Employees[staffList.size()]; staffList.toArray(a_storage); System.out.println(Arrays.toString(a_storage)); /** Multi parameters Methods **/ System.out.println(max(12,23.45,45.67)); /** About Clone Data **/ Employees original = new Employees("Original",100,2015,10,13); Employees copy = original; copy.raiseSalary(100); // it will change original System.out.println(original.getSalary() + ";" + copy.getSalary()); try { Employees clone = original.clone(); //TODO why must in the same package clone.raiseSalary(100); // it will not change original System.out.println(original.getSalary() + ";" + copy.getSalary()+";"+clone.getSalary()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } /** Call Back **/ class TimePrinter implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { Date now = new Date(); System.out.println("Time now :" + now); Toolkit.getDefaultToolkit().beep(); // beep sound but not work. } } ActionListener mActionListener = new TimePrinter(); Timer t = new Timer(3000,mActionListener); //javax.swing.timer t.start(); JOptionPane.showMessageDialog(null, "Quit ?"); System.exit(0); /** try catch finally **/ System.out.println(tryTest()); /** assert **/ assert 5 < 0 : "hello"; assert 5 < 0; /** Thread Methods **/ Thread mThread = new Thread(new Runnable() { @Override public void run() { while(true){ System.out.println("HeartBeat~!"); try { Thread.sleep(500); // while(!Thread.currentThread().isInterrupted()){ // System.out.println("Thread is interrupted!"); // Blocked by sleep // } } catch (InterruptedException e) { e.printStackTrace(); } } } }); mThread.start(); } /** variable parameters methods **/ public static double max(double... values){ double largest = Double.MIN_VALUE; for(double v : values){ if(v > largest){ largest = v; } } return largest; } /** try catch finally **/ private static int tryTest() { try{ return 1; } finally { return 2; } } /** Method for thread **/ public void runThread(){ new Thread(new Runnable() { @Override public void run() { System.out.println("HeartBeat~!"); } }).start(); } // lambda expressions public void runThread(){ new Thread(() -> { System.out.println("HeartBeat~!"); }).start(); } }
2、Employees.java
package ch04; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * Created by harry on 15-7-30. */ public class Employees implements Comparable,Cloneable{ /** instance fields **/ private String name; private double salary; private Date hireday; private static int nextId = 1; private int id; /** constructor **/ // no return value public Employees(String a_name, double a_salary, int a_year, int a_month, int a_day){ // this.name = a_name; // distinct instance value from local value; name = a_name; salary = a_salary; GregorianCalendar calendar = new GregorianCalendar(a_year,a_month-1,a_day); hireday = calendar.getTime(); System.out.println("Normal Constructor is called ."); } public Employees(){ System.out.println("Empty Constructor is called ."); name = "Guest"; salary = 111.111; hireday = Calendar.getInstance().getTime(); } /** Methods **/ public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Date getHireday() { // return hireday; return (Date)hireday.clone(); } public void setHireday(Date hireday) { this.hireday = hireday; } public int getId() { this.id = nextId; nextId++; return id; } public void setId(int id) { this.id = id; } public static int getNextId() { return nextId; } // public static void setNextId(int nextId) { // Employees.nextId = nextId; // } /** Other Methods **/ public void raiseSalary(double byPercent){ double raise = salary * byPercent / 100; salary += raise; } public boolean myEquals(Employees other){ return name.equals(other.name); } public String toString(){ return "["+getId()+","+getName()+","+getSalary()+","+getHireday()+"]"; } /** override implements **/ @Override public int compareTo(Object otherObject) { Employees others = (Employees)otherObject; return Double.compare(salary,others.salary); } @Override protected Employees clone() throws CloneNotSupportedException { return (Employees)super.clone(); } }
3、Managers.java
package ch05; import ch04.Employees; /** * Created by harry on 15-7-31. */ public class Managers extends Employees { private double bonus; public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } public Managers(String a_name, double a_salary, int a_year, int a_month, int a_day) { super(a_name, a_salary, a_year, a_month, a_day); } public Managers() { super(); } @Override public double getSalary() { return super.getSalary()+bonus; } }
4、MyFirstHandMakeClass
package ch04; import ch05.Managers; /** * Created by harry on 15-7-30. */ public class MyFirstHandMakeClass { public static void main(String[] args){ // operateEmployee(); // operateManager(); testPolymorphism(); } private static void operateEmployee() { Employees[] mEmployees = new Employees[2]; mEmployees[0] = new Employees("user9",999.999,2099,9,9); mEmployees[1] = new Employees(); for(Employees e : mEmployees){ System.out.println("name:"+e.getName()+",salary:"+e.getSalary()+",hireday:"+e.getHireday()); } for(Employees e : mEmployees){ e.raiseSalary(5); System.out.println("name:"+e.getName()+",salary:"+e.getSalary()+",hireday:"+e.getHireday()); } // if(mEmployees[0].myEquals(mEmployees[0])){ if(mEmployees[0].myEquals(mEmployees[1])){ System.out.println("yes"); }else { System.out.println("no"); } for(Employees e : mEmployees){ System.out.println(e.getId()); } } private static void operateManager() { Managers[] mManagers = new Managers[2]; mManagers[0] = new Managers("user9",999.999,2099,9,9); // Can not extends constructor mManagers[1] = new Managers(); for(Managers m : mManagers){ System.out.println(m.getName()); } } private static void testPolymorphism() { Managers boss = new Managers("boss",900,2017,7,7); boss.setBonus(500); Employees[] stuffs = new Employees[3]; stuffs[0] = boss; stuffs[1] = new Employees(); stuffs[2] = new Employees("harry",200.999,2016,8,8); for(Employees e : stuffs){ System.out.println(e.getName()+" : "+e.getSalary()); System.out.println(e.toString()); System.out.println(e.getClass().getName()+" "+e.getName()); } } }