public class ThreadExample2 implements Runnable {
public static void main(String[] argv) {
Thread t = new Thread(new ThreadExample2()); // 產生Thread物件
t.start(); // 開始執行Runnable.run();
for (;;) {
System.out.println("Main Thread");
}
}
synchronized static public void syncMethod() {…}
}
public class Animal { public String moveMethod() { return "Unspecified"; }}
public class Bird extends Animal { public String moveMethod() { return "Fly"; }}
public class Dog extends Animal { public String moveMethod() { return "run"; }}
public class Fish extends Animal { public String moveMethod() { return "swim"; }}
public class InheritanceExample {
public static void main(String[] argv) {
Animal a1, a2, a3, a4;
Bird b;
Dog d;
Fish f;
a2 = a1 = new Animal();
b = new Bird();
d = new Dog();
f = new Fish();
System.out.println(a1.moveMethod());
System.out.println(b.moveMethod());
System.out.println(d.moveMethod());
System.out.println(f.moveMethod());
a1 = b; // Correct, we call this upcasting
b = a1; // Compile Error, type not compatible
b = (Bird)a1; // downcasting, Compile Correct
a2 = b; // Correct,we call this upcasting
d = a2; // Compile Error, type not compatible
d = (Dog)a2; // Compile Correct, but runtime error
}
}
<< thread >>
public class ThreadExample2 implements Runnable {
public void run() { // implements Runnable run()
System.out.println("Here is the starting point of Thread.");
for (;;) { // infinite loop to print message
System.out.println("User Created Thread");
}
}
public static void main(String[] argv) {
Thread t = new Thread(new ThreadExample2()); // 產生Thread物件
t.start(); // 開始執行Runnable.run();
for (;;) {
System.out.println("Main Thread");
}
}
}
沒有留言:
張貼留言