¡¶Java³ÌÐòÉè¼Æ¡·Á·Ï°Ìâ ÏÂÔØ±¾ÎÄ

plainButton = new JButton( \ container.put( plainButton );

fancyButton = new JButton( \ container.put( fancyButton );

ButtonHandler handler = new ButtonHandler(); fancyButton.addItemListener( handler ); plainButton.addItemListener( handler ); setSize( 275, 100 ); setVisible( true ); }

public static void main( String[] args ) {

ButtonTest application = new ButtonTest();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); }

private class ButtonHandler implements ActionListener {

public void itemStateChanged( ActionEvent event ) {

JOptionPane.showMessageDialog( null, \

event.getActionCommand() );

} } }

13¡¢Ð´³öÏÂÁгÌÐò´úÂëµÄÔËÐнá¹û£º

1) class MinusException extends Exception { 2) int number;

3) public MinusException (String msg, int i) { 4) super(msg);

5) this.number = i; 6) } 7) } 8)

9) class Salary {

10) private String name; 11) private int salary;

12) public Salary(String n, int s ) throws MinusException { 13) this.name=n;

14) if (s<0) throw new MinusException(\¹¤×ʲ»ÄÜΪ¸º£¡\15) this.salary=s; 16) }

17) public void print() {

18) System.out.println(name+\µÄ¹¤×ÊΪ\Ôª¡£\19) } 20) } 21)

µÚ 21 Ò³ ¹² 48 Ò³

22) class TestSalary {

23) public static void main(String [] args) { 24) try {

25) Salary s1=new Salary(¡°ÕÅÈý¡±,1000); 26) s1.print( );

27) Salary s2=new Salary(¡°ÀîËÄ¡±,-10); 28) s2.print( ); 29) }

30) catch(MinusException e) {

31) System.out.println(\Òì³£:\32) } 33) } 34) }

14¡¢×ÐϸÔĶÁÏÂÃæµÄ³ÌÐò´úÂ룬Èô¾­±àÒëºÍÔËÐкó£¬Çëд³ö´òÓ¡½á¹û¡£ 1. public class Outer {

2. private int size = 1; 3.

4. public class Inner{ 5. private int size=2;

6. public void doStuff(int size){ 7. size++;

8. this.size++;

9. Outer.this.size++;

10. System.out.println(\

11. System.out.println(\12. System.out.println(\of the Outer class: \13. } 14. } 15.

16. Inner i=new Inner(); 17.

18. public void increaseSize(int s){ 19. i.doStuff(s); 20. } 21. 22.

23. public static void main(String[] a){ 24. Outer outer=new Outer(); 25. outer.increaseSize(10); 26. } 27. }

15¡¢ÔĶÁÏÂÃæµÄ³ÌÐò´úÂ룬д³ö³ÌÐòÔËÐеĴòÓ¡½á¹û¡£

µÚ 22 Ò³ ¹² 48 Ò³

1. class ParentClass{ 2. int x=0;

3. int sum(int a,int b, int c){ 4. return a+b+c; 5. }

6. int sum(int a,int b){ 7. return a+b; 8. } 9. }

10. class ChildClass extends ParentClass{ 11. public ChildClass( ) {x=10;} 12. int sum(int a,int b){ 13. return a+b+1; 14. } 15. }

16. class Test{

17. public static void main(String args[]){ 18. ParentClass p=new ChildClass();

19. System.out.println(p.sum(5,5,5)); 20. System.out.println(p.sum(5,5)); 21. System.out.println(p.x); 22. } 23. }

16¡¢ÔĶÁÏÂÃæµÄ³ÌÐò£¬Ð޸ijÌÐòÖдíÎóµÄµØ·½£º

1) public class Test implements Runnable { 2) String str[]=new String[10]; 3) static {

4) for (int i = 0; i < str.length; i++) { 5) str[i]=i+¡±-¡±; 6) } 7) }

8) public void run() { 9) try {

10) for (int i = 0; i < str.length; i++) { 11) sleep(1000);

12) System.out.print(str[i]); 13) }

14) } catch (InterruptedException e) { 15) e.printStackTrace(); 16) } 17) }

18) public static void main(String args[]) { 19) Test t = new Test();

µÚ 23 Ò³ ¹² 48 Ò³

20) t.run(); 21) } 22) }

17¡¢×ÐϸÔĶÁÏÂÃæµÄ³ÌÐò´úÂ룬Èô¾­±àÒëºÍÔËÐкó£¬Çëд³ö´òÓ¡½á¹û¡£

1) class TestString{

2) public void stringReplace (String text) { 3) text = text.replace('j' , 'i'); 4) text=text+\5) }

6) public void bufferReplace (StringBuffer text) { 7) text.setCharAt(0,'i'); 8) text = text.append(\9) }

10) public void change(char ch[]){ 11) ch[0]= 'Y'; 12) }

13) public static void main(String args[]){ 14) String str1=\

15) StringBuffer str2=new StringBuffer(\16) char ch[]={'j','a','v','a'}; 17) TestString t=new TestString(); 18) t.change(ch);

19) t.stringReplace (str1); 20) t.bufferReplace (str2); 21) System.out.println(str1);

22) System.out.println(str2.toString()); 23) System.out.println (new String(ch)); 24) } 25) }

18¡¢ÔĶÁÏÂÃæµÄ³ÌÐò´úÂ룬ÅжÏ18¡«27ÐУ¨´ø»®Ïß²¿·Ö£©¸÷Óï¾ä±àÒëÊÇ·ñͨ¹ý£¬Èç¹û±àÒëͨ¹ý£¬Ö±½Óд³ö¸ÃÐеĴòÓ¡½á¹û¡£

1. class TestString{

2. public static String stringReplace (String text) { 3. text = text.replace('j' , 'i'); 4. text=text+\5. return text; 6. }

7. public static StringBuffer bufferReplace (StringBuffer text) { 8. text.setCharAt(0,'i'); 9. text = text.append(\10. return text; 11. }

µÚ 24 Ò³ ¹² 48 Ò³