3¡¢³ÌÐò±àÒëÄÜͨ¹ý£¬Êä³ö½á¹ûΪ¡°¶¯Îï½Ð£¡¡±ºÍ¡°ÍôÍô¡¡¡±£¬ÒòΪÔÚ³ÌÐòÖе÷ÓÃshout()·½·¨Ê±£¬Ê×ÏÈ»áͨ¹ýsuper.shout()µ÷Óø¸ÀàµÄ·½·¨Ëµ³ö¡°¶¯Îï½Ð£¡¡±Ö®ºóÔÙÊä³ö¡°ÍôÍô¡¡¡±
4¡¢³ÌÐò±àÒ벻ͨ¹ý£¬ÒòΪ½Ó¿ÚÖж¨ÒåµÄ·½·¨²»ÄÜÓз½·¨Ì壬ËùÒÔ¶¨ÒåµÄeat()·½·¨ÊÇ´íÎóµÄ¡£½Ó¿ÚÖеķ½·¨±ØÐëÔÚ×ÓÀàÖÐÈ«²¿ÊµÏÖ£¬ÓÉÓÚrun()·½·¨ÔÚ×ÓÀàÖв¢Ã»ÓÐÖØÐÂʵÏÖ£¬ËùÒÔÕâÒ²ÊÇ´íÎóµÄ¡£
Îå¡¢¼ò´ðÌâ
1¡¢Ôڼ̳йØÏµÖУ¬×ÓÀàµÄ·½·¨Ó븸ÀàµÄijһ·½·¨¾ßÓÐÏàͬµÄ·½·¨Ãû¡¢·µ»ØÀàÐͺͲÎÊýÁÐ±í£¬Ôò³Æ×ÓÀàµÄ¸Ã·½·¨ÖØÐ´(¸²¸Ç)¸¸ÀàµÄ·½·¨¡£
2¡¢¶à̬Òâζ×ÅÒ»¸ö¶ÔÏóÓÐ×ŶàÖÖÐÎ̬£¬¿ÉÒÔÔÚÌØ¶¨µÄÇé¿öÏ£¬±íÏÖ²»Í¬µÄ״̬£¬´Ó¶ø¶ÔÓ¦×Ų»Í¬µÄÊôÐԺͷ½·¨¡£¼òµ¥µÄ˵£¬¶à̬¾ÍÊÇʹÓø¸ÀàÀàÐ͵ıäÁ¿ÒýÓÃ×ÓÀà¶ÔÏ󣬸ù¾Ý±»ÒýÓÃ×ÓÀà¶ÔÏóµÄÌØÐÔ£¬³ÌÐò»áµÃµ½²»Í¬µÄÔËÐÐЧ¹û¡£
3¡¢ÔÚJavaÖУ¬Ê¹ÓÃabstract¹Ø¼ü×ÖÐÞÊεÄÀà³ÆÖ®Îª³éÏóÀà¡£³éÏóÀàÊDz»Äܱ»ÊµÀý»¯µÄ£¬Í¨³£ÐèҪдһ¸ö×ÓÀàÀ´¼Ì³Ð³éÏóÀ࣬ͬʱʵÀý»¯×ÓÀàÀ´»ñµÃ¸ÃÀàµÄ¶ÔÏó¡£³éÏóÀàͨ³£ÓÃÓÚ±íʾһÖÖ³éÏóµÄ¸ÅÄî¡£ ½Ó¿Ú¿ÉÒÔ˵ÊÇÒ»ÖÖÌØÊâµÄ³éÏóÀ࣬½Ó¿ÚÖÐÖ»Äܶ¨Òå³£Á¿ºÍ³éÏó·½·¨¡£ÓÉÓÚ½Ó¿ÚµÄÌØÊâÐÔ£¬ÔÚ¶¨ÒåʱÐèҪʹÓÃinterface¹Ø¼ü×Ö¡£
Áù¡¢±à³ÌÌâ
1¡¢²Î¿¼´ð°¸
class Student { }
class UnderGraduate extends Student{ }
public class Test01{
public static void main(String[] args) { }
Student student = new Student(\student.show();
UnderGraduate underGraduate = new UnderGraduate(%underGraduate.show(); public String degree;
public UnderGraduate(String name,int age,String degree){ }
public void show(){ }
System.out.println(\super(name, age); this.degree=degree; public String name; public int age;
public Student(String name,int age){ }
public void show(){ }
System.out.println(\this.name=name; this.age=age;
}
2¡¢²Î¿¼´ð°¸
interface Shape { }
class Square implements Shape{ }
class Circle implements Shape{ }
public class Test02 { }
public static void main(String[] args) { }
Shape square = new Square(); Shape circle = new Circle();
System.out.println(square.area(2)); System.out.println(circle.area(3)); public double area(double r) { }
return Math.PI*r*r;
public double area(double sideLength) { }
return sideLength*sideLength;
double area(double givenValue);
3¡¢²Î¿¼´ð°¸
class NoThisSongException extends Exception{ }
class Player{ }
public class Test03 {
public static void main(String[] args) {
Player player = new Player(); try {
public void play(int index)throws NoThisSongException{ }
if(index>10){ }
System.out.println(\ÕýÔÚ²¥·Å¸èÇú\
throw new NoThisSongException(\Äú²¥·ÅµÄ¸èÇú²»´æÔÚ\
public NoThisSongException(){ }
public NoThisSongException(String message){ }
super(message); super();
}
}
player.play(13);
} catch (NoThisSongException e) { }
System.out.println(\Òì³£ÐÅϢΪ£º \
µÚ5Õ ¶àÏß³Ì
Ò»¡¢Ìî¿ÕÌâ
1¡¢ Ï̡߳¢Í¨ÐÅ
2¡¢ Thread¡¢Runnable 3¡¢ ¾ÍÐ÷
4¡¢ synchronized¡¢¶ÔÏó¡¢this 5¡¢ ½ø³Ì 6¡¢ н¨×´Ì¬(New)¡¢¾ÍÐ÷״̬(Runnable)¡¢ÔËÐÐ״̬(Running)¡¢×èÈû״̬(Blocked)¡¢ËÀÍö״̬(Terminated) 7¡¢ 10¡¢1
8¡¢ ¿ªÆôÒ»¸öÐÂÏ̡߳¢run()·½·¨ 9¡¢ wait()¡¢notify()¡¢notifyAll() 10¡¢setDaemon(true)¡¢start() ¶þ¡¢ÅжÏÌâ
1¡¢´í 2¡¢¶Ô 3¡¢¶Ô 4¡¢´í 5¡¢´í Èý¡¢Ñ¡ÔñÌâ
1¡¢B 2¡¢AC 3¡¢ABC 4¡¢BC 5¡¢ABD 6¡¢ABC 7¡¢C 8¡¢D 9¡¢AB 10¡¢ABCD ËÄ¡¢³ÌÐò·ÖÎöÌâ
1¡¢³ÌÐò²»ÄܱàÒëͨ¹ý£¬ÒòΪRunHandlerÀàûÓÐʵÏÖRunnable½Ó¿Ú£¬Òò´ËRunHandlerµÄʵÀý¶ÔÏó²»ÄÜ×÷Ϊ²ÎÊý´«µÝ¸øThreadµÄ¹¹Ôì·½·¨¡£
2¡¢³ÌÐò²»ÄܱàÒëͨ¹ý£¬ÒòΪThreadµÄ×ÓÀàAÖØÐ´µÄrun()·½·¨µÄ·ÃÎʼ¶±ð²»ÄܵÍÓÚ¸¸Ààrun()·½·¨µÄ¡£·ÃÎʼ¶±ð
3¡¢³ÌÐò²»ÄܱàÒëͨ¹ý£¬ÒòΪͬ²½·½·¨Öе÷ÓÃwait()·½·¨µÄ¶ÔÏó±ØÐëΪͬ²½Ëø¶ÔÏó¡£ 4¡¢t.start(); Îå¡¢¼ò´ðÌâ
1¡¢Ò»ÖÖÊǼ̳Ðjava.lang°üϵÄThreadÀ࣬¸²Ð´ThreadÀàµÄrun()·½·¨£¬ÔÚrun()·½·¨ÖÐʵÏÖÔËÐÐÔÚÏß³ÌÉϵĴúÂë¡£
new Thread() {
public void run(){} }.start();
ÁíÒ»ÖÖ¾ÍÊÇʵÏÖjava.lang.Runnable½Ó¿Ú£¬Í¬ÑùÊÇÔÚrun()·½·¨ÖÐʵÏÖÔËÐÐÔÚÏß³ÌÉϵĴúÂë¡£
new Thread(new Runnable(){ public void run(){} }).start()
2¡¢µ÷ÓÃsleep()·½·¨£¬ÕýÔÚÖ´ÐеÄÏß³ÌÖ÷¶¯ÈóöCPUÈ¥Ö´ÐÐÆäËûỊ̈߳¬ÔÚsleep()·½·¨Ö¸¶¨µÄʱ¼ä¹ýºó£¬
CPU²Å»á»Øµ½Õâ¸öÏß³ÌÉϼÌÐøÍùÏÂÖ´ÐУ¬Èç¹ûµ±Ç°Ï߳̽øÈëÁËͬ²½Ëø£¬sleep()·½·¨²¢²»»áÊÍ·ÅËø£¬¼´Ê¹µ±Ç°Ïß³ÌʹÓÃsleep()·½·¨ÈóöÁËCPU£¬µ«ÆäËü±»Í¬²½Ëøµ²×¡Á˵ÄÏß³ÌÒ²ÎÞ·¨µÃµ½Ö´ÐС£wait()
ÔÚÒ»¸öÒѾ½øÈëÁËͬ²½ËøµÄÏß³ÌÄÚ½øÐе÷Óã¬Èõ±Ç°Ïß³ÌÔÝʱÈóöͬ²½Ëø£¬ÒÔ±ãÆäËüÕýÔڵȴý´ËËøµÄÏ߳̿ÉÒԵõ½Í¬²½Ëø²¢ÔËÐС£µ±ÆäËüÏ̵߳÷ÓÃÁËnotify()·½·¨ºó£¬µ÷ÓÃwait()·½·¨µÄÏ߳̾ͻá½â³ýwait״̬£¬µ±ÔٴλñµÃͬ²½Ëøºó£¬³ÌÐò¿ÉÒÔ¼ÌÐøÏòÏÂÖ´ÐС£
Áù¡¢±à³ÌÌâ
1¡¢²Î¿¼´ð°¸
public class MyThread extends Thread{
public MyThread(String name) { }
public void run() { }
public static void main(String[] args) { }
new MyThread(\new MyThread(\
super(name);
System.out.println(this.getName());
}
2¡¢²Î¿¼´ð°¸
public class MyRunnable implements Runnable { }
public void run() { }
public static void main(String[] args) { }
new Thread(new MyRunnable()).start(); for (int i = 0; i < 100; i++) { }
System.out.println(\for (int i = 0; i < 50; i++) { }
System.out.println(\
3¡¢²Î¿¼´ð°¸
public class Test01 { }
class Teacher implements Runnable {
private int notes = 80; public void run() {
while (true) {
public static void main(String[] args) { }
Teacher t = new Teacher(); new Thread(t, \³ÂÀÏʦ\new Thread(t, \¸ßÀÏʦ\new Thread(t, \ÀîÀÏʦ\
}
}
}
dispatchNotes(); // µ÷ÓÃÊÛÆ±·½·¨ if (notes <= 0) { }
break;
private synchronized void dispatchNotes() { }
if (notes > 0) { }
try { }
System.out.println(Thread.currentThread().getName() + \·¢³öµÄ±Ê¼Ç\
+ notes--);
Thread.sleep(10); // ¾¹ýµÄÏß³ÌÐÝÃß10ºÁÃë e.printStackTrace();
} catch (InterruptedException e) {
4¡¢²Î¿¼´ð°¸
public class Accumulator extends Thread {
private int stratNum; public static int sum;
public Accumulator(int startNum) { }
public static synchronized void add(int num) { }
public void run() { }
public static void main(String[] args) throws Exception {
Thread[] threadList = new Thread[10]; for (int i = 0; i < 10; i++) { }
for (int i = 0; i < 10; i++) { }
threadList[i].join();
threadList[i] = new Accumulator(10 * i + 1); threadList[i].start(); int sum = 0;
for (int i = 0; i < 10; i++) { }
add(sum);
sum += stratNum + i; sum += num;
this.stratNum = startNum;