|
《Java就业培训教程》P127源码 程序清单:Student.java class Person { public String name; public int age; public Person(String name,int age) { this.name=name; this.age=age; } public Person() //如果不写这个构造函数,看看对类Student有什么影响。 { } public void getInfo() { System.out.println(name); System.out.println(age); } } class Student extends Person { public void study() { System.out.println("Studding"); } public static void main(String[] args) { Person p=new Person(); p.name="person"; p.age=30; p.getInfo(); Student s=new Student(); s.name="student"; s.age=16; s.getInfo(); s.study(); } }
《Java就业培训教程》P135源码 interface Animal extends Runner { void breathe(); } class Fish implements Animal { public void run() { System.out.println("fish is swimming"); } public void breathe() { System.out.println("fish is bubbling"); } } abstract LandAnimal implements Animal { public void breathe() { System.out.println("LandAnimal is breathing"); } } 《Java就业培训教程》P138源码 程序清单:C.java class A { public void func1() { System.out.println("A func1 is calling"); } public void func2() { func1(); } } class B extends A { public void func1() { System.out.println("B func1 is calling"); } public void func3() { System.out.println("B func3 is calling"); } } class C { public static void main(String [] args) { B b=new B(); A a = b; callA(a); callA(new B()); } public static void callA(A a) { a.func1(); a.func2(); } }
《Java就业培训教程》P141源码 程序清单:Student.java class Student { String name; int age; boolean equals(Object obj) { Student st=null; if(obj instanceof Student) st = (Student)obj; else return false; if(st.name==this.name && st.age==this.age) return true; else return false; } public static void main(String[] args) { Student p=new Student(); Student q=new Student(); p.name="xyz"; p.age=13; q.name="xyz"; q.age=13; if(p.equals(q)) System.out.println("p 与 q 相等"); else System.out.println("p 与 q 不等"); } }
《Java就业培训教程》P144源码 程序清单:Interface.java interface PCI { void start(); void stop(); } class NetworkCard implements PCI { public void start() { System.out.println("Send ..."); } public void stop() { System.out.println("Network Stop."); } } class SoundCard implements PCI { public void start() { System.out.println("Du du..."); } public void stop() { System.out.println("Sound Stop."); } } class MainBoard { public void usePCICard(PCI p) { p.start(); p.stop(); } } class Assembler { public static void main(String [] args) { MainBoard mb=new MainBoard(); NetworkCard nc=new NetworkCard(); mb.usePCICard(nc); SoundCard sc=new SoundCard(); mb.usePCICard(sc); } }
《Java就业培训教程》P149源码 public class TestException { public static void main(String [] args) { try { int reslut = new Test().devide( 3, 0 ); System.out.println("the result is" + reslut ); } catch(Exception e) { System.out.println(e.getMessage()); } System.out.println("program is running here ,that is normal !"); } } class Test { public int devide(int x, int y) { int result = x/y; return x/y; } }
《Java就业培训教程》P154源码 public class TestException { public static void main(String [] args) { try { int result = new Test().devide( 3, 0 ); //int result = new Test().devide( 3, -1 ); //int result = new Test().devide( 3, 1 ); System.out.println("the result is " + result ); } catch(DevideByMinusException e) { System.out.println("program is running into"+ "DevideByMinusException"); System.out.println(e.getMessage()); System.out.println("the devisor is " + e. getDevisor()); } catch(ArithmeticException e) { System.out.println("program is running into"+ "DevideByMinusException"); System.out.println(e.getMessage()); } catch(Exception e) { System.out.println("program is running into"+ "other unknowned Exception"); System.out.println(e.getMessage()); } System.out.println("program is running here ,that is normal !"); } }
《Java就业培训教程》P158源码 package org.it315; public class TestPackage { public static void main(String [] args) { new Test().print(); } } class Test { public void print() { System.out.println("the program is demostrating how to using package!"); } }
|