Java2实用教程(第二版)程序代码——第四章 类、对象、和接口

  1//例子1
  2class XiyoujiRenwu   
  3{   float height,weight;
  4    String head, ear,hand,foot, mouth;
  5    void speak(String s) 
  6    {  System.out.println(s);
  7    }

  8}

  9class A
 10{   public static void main(String args[])
 11   { XiyoujiRenwu  zhubajie;       //声明对象。
 12     zhubajie=new  XiyoujiRenwu(); //为对象分配内存,使用new 运算符和默认的构造方法。
 13   }

 14}

 15
 16//例子2
 17class Point 
 18int x,y;
 19  Point(int a,int b)
 20  { x=a;
 21   y=b;
 22  }

 23}

 24public class A 
 25public static void main(String args[])
 26  { Point p1,p2;                 //声明对象p1和p2。
 27    p1=new Point(10,10);   //为对象分配内存,使用 new 和类中的构造方法。
 28    p2=new Point(23,35);   //为对象分配内存,使用 new 和类中的构造方法。
 29  }

 30}

 31
 32//例子3
 33class XiyoujiRenwu
 34float height,weight;
 35       String head, ear,hand,foot, mouth;
 36       void speak(String s)
 37       { head="歪着头";
 38         System.out.println(s);
 39       }

 40    }

 41   class Example4_3
 42{  public static void main(String args[]) 
 43   { XiyoujiRenwu  zhubajie,sunwukong;//声明对象。
 44         zhubajie=new  XiyoujiRenwu();  //为对象分配内存,使用new 运算符和默认的构造方法。
 45         sunwukong=new  XiyoujiRenwu();
 46         zhubajie.height=1.80f;     //对象给自己的变量赋值。
 47         zhubajie.weight=160f;      zhubajie.hand="两只黑手";
 48         zhubajie.foot="两只大脚";  zhubajie.head="大头"
 49         zhubajie.ear="一双大耳朵"; zhubajie.mouth="一只大嘴"
 50         sunwukong.height=1.62f;    //对象给自己的变量赋值。
 51         sunwukong.weight=1000f;    sunwukong.hand="白嫩小手";
 52         sunwukong.foot="两只绣脚"; sunwukong.head="绣发飘飘"
 53         sunwukong.ear="一对小耳";  sunwukong.mouth="樱头小嘴";
 54         System.out.println("zhubajie的身高:"+zhubajie.height);
 55         System.out.println("zhubajie的头:"+zhubajie.head);
 56         System.out.println("sunwukong的重量:"+sunwukong.weight);
 57         System.out.println("sunwukong的头:"+sunwukong.head);
 58         zhubajie.speak("俺老猪我想娶媳妇");      //对象调用方法。
 59         System.out.println("zhubajie现在的头:"+zhubajie.head);
 60        sunwukong.speak("老孙我重1000斤,我想骗八戒背我"); //对象调用方法。
 61        System.out.println("sunwukong现在的头:"+sunwukong.head);
 62       }

 63    }

 64
 65//例子4
 66import java.applet.*;
 67import java.awt.*;
 68class Student 
 69{  float math,english,sum;
 70   float f(float k1,float k2)
 71   { sum=k1*math+k2*english;
 72     return sum;
 73   }
 
 74}

 75public class Example4_4 extends Applet 
 76{  Student wanghong,lihong;
 77   public void init() 
 78   { wanghong=new Student();  lihong  =new Student();
 79     wanghong.math=60.0f;     wanghong.english=80f;
 80     lihong.math=70.0f;       lihong.english=90.0f;
 81     wanghong.sum=wanghong.f(2.0f,2.0f);
 82     lihong.sum=lihong.f(2.0f,2.0f); 
 83   }

 84   public void paint(Graphics g)
 85   { g.drawString("lihong sum= "+lihong.sum,12,45);
 86     g.drawString("wanghong sum="+wanghong.sum,12,60);
 87   }

 88}
  
 89
 90//例子5
 91class 梯形 
 92{  float 上底,下底,高;
 93    梯形()
 94    { 上底=60;
 95     下底=40;
 96      高=20;
 97    }

 98    梯形(float x,float y,float h) 
 99    { 上底=x;
100     下底=y;
101       高=h;
102    }

103    float 计算面积() 
104    float 面积;
105      面积=(上底+下底)*/2.0f;
106      return 面积;
107    }

108    void 修改高(float height) 
109    {  高=height;
110    }

111    float 获取高() 
112    return 高;
113    }

114}

115class Example4_5  
116{    public static void main(String args[]) 
117    { 梯形 laderOne=new 梯形(),laderTwo=new 梯形(2.0f,3.0f,10);
118      System.out.println("laderOne的高是:"+laderOne.获取高());
119      System.out.println("laderTwo的高是:"+laderTwo.获取高());
120      System.out.println("laderOne的面积是:"+laderOne.计算面积());
121      System.out.println("laderTwo的面积是:"+laderTwo.计算面积());
122      laderOne.修改高(10);
123      float h=laderOne.获取高();
124      laderTwo.修改高(h*2);
125      System.out.println("laderOne现在的高是:"+laderOne.获取高());
126      System.out.println("laderTwo现在的高是:"+laderTwo.获取高());
127      System.out.println("laderOne现在的面积是:"+laderOne.计算面积());
128      System.out.println("laderTwo现在的面积是:"+laderTwo.计算面积());
129    }
 
130}
 
131
132//例子6
133class 圆 
134{  double 半径;
135   圆(double r)
136   { 半径=r;
137   }

138  double 计算面积() 
139  {  return 3.14*半径*半径;
140  }

141  void 修改半径(double 新半径)
142  {  半径=新半径;
143  }

144  double 获取半径() 
145  return 半径;
146  }

147}

148class 圆锥 
149{  圆 底圆;
150  double 高;
151  圆锥(圆 circle,double h) 
152  {  this.底圆=circle;
153    this.高=h;
154  }

155  double 计算体积() 
156  {  double volume;
157    volume=底圆.计算面积()*/3.0;
158    return  volume;
159  }

160  void 修改底圆半径(double r) 
161  {  底圆.修改半径(r);
162  }

163  double 获取底圆半径() 
164  {  return 底圆.获取半径();
165  }

166}

167class Example4_6 
168{  public static void main(String args[])
169   { 圆 circle=new 圆(10);
170     圆锥 circular=new 圆锥(circle,20);
171     System.out.println("圆锥底圆半径:"+circular.获取底圆半径()); 
172     System.out.println("圆锥的体积:"+circular.计算体积());
173     circular.修改底圆半径(100);
174     System.out.println("圆锥底圆半径:"+circular.获取底圆半径());
175     System.out.println("圆锥的体积:"+circular.计算体积());  
176   }

177}

178
179//例子7
180class 梯形 
181{    float 上底,高;
182    static float 下底;
183    梯形(float x,float y,float h) 
184    { 上底=x; 下底=y; 高=h;
185    }

186    float 获取下底()
187    {   return 下底;
188    }

189    void 修改下底(float b)
190    {  下底=b; 
191    }

192}

193class Example4_7 
194{    public static void main(String args[]) 
195{ 梯形 laderOne=new 梯形(3.0f,10.0f,20),laderTwo=new 梯形(2.0f,3.0f,10);
196       System.out.println("laderOne的下底:"+laderOne.获取下底());
197       System.out.println("laderTwo的下底:"+laderTwo.获取下底());
198       laderTwo.修改下底(60);
199       System.out.println("laderOne的下底:"+laderOne.获取下底());
200       System.out.println("laderTwo的下底:"+laderTwo.获取下底());
201    }
 
202}
 
203
204//例子8
205import java.applet.*;
206import java.awt.*;
207class Family 
208{  static String familyname;
209       String name; 
210  int age;     
211}

212public class Example4_8 extends Applet 
213{  Family father,son1,son2;
214   public void init() 
215   { father=new Family();
216     son1=new Family();son2=new Family();
217     Family.familyname=""; father.name="鬼子";
218     son1.name="汉奸"; son2.name="恶霸";
219   }

220   public void paint(Graphics g) 
221   {  g.drawString("father: "+father.familyname+father.name,5,10);
222      g.drawString("son1: "+son1.familyname+son1.name,5,20);
223      g.drawString("son2: "+son2.familyname+son2.name,5,30);
224      Family.familyname="";
225      g.drawString("father:"+father.familyname+father.name,5,40);
226      g.drawString("son1: "+son1.familyname+son1.name,5,50);
227      g.drawString("son2: "+son2.familyname+son2.name,5,65);
228   }

229}

230
231//例子9
232class Fibi 
233{  public long  fibinacii(int n)
234   {        long c=0;
235           if(n==1||n==2)
236                c=1;
237           else
238                c=fibinacii(n-1)+fibinacii(n-2); 
239           return c;
240    }

241}

242public class Example4_9 
243{  public static void main(String args[])
244   { Fibi a=new Fibi(); 
245     for(int i=1;i<=10;i++
246       { System.out.print(" "+a.fibinacii(i));
247       }

248   }

249}

250
251//例子10
252class 三角形 
253{  double a,b,c;
254   三角形(double a,double b,double c) 
255   { setABC(this,a,b,c);
256   }

257  void setABC(三角形 trangle,double a,double b,double c) 
258   { trangle.a=a;
259     trangle.b=b;
260     trangle.c=c;
261   }

262}

263class Example4_10 
264{  public static void main(String args[])
265    {三角形 tra=new 三角形(3,4,5);
266     System.out.print("三角形型的三边是:"+tra.a+","+tra.b+","+tra.c+","); 
267     }

268}

269
270//例子11
271package tom.jiafei;
272public class  PrimNumber 
273{    public static void main(String args[])
274     {  int sum=0,i,j;
275        for( i=1;i<=10;i++)  //找出10以内的素数。
276         {  for(j=2;j<=i/2;j++)
277             {   if(i%j==0)
278                   break;
279             }

280           if(j>i/2) System.out.print(" 素数:"+i);
281         }

282    }

283}

284
285//例子12
286import java.applet.Applet;import java.awt.*;
287public class Example4_12 extends Applet 
288{  Button redbutton;
289    public void init()
290     {   redbutton=new Button("我是一个红色的按钮"); 
291         redbutton.setBackground(Color.red);
292         add(redbutton);
293     }

294    public void  paint(Graphics g) 
295     {  g.drawString("it is a button",30,50);
296     }

297}

298
299//例子13
300import tom.jiafei.*//引入包tom.jiafei中的类。
301public class Example4_13
302{    public static void main(String args[]) 
303     {  PrimNumber num=new PrimNumber();//用包tom.jiafei中的类创建对象。
304        String a[]={"ok"};
305        System.out.println(a[0]);
306        num.main(a);
307     }

308}

309
310//例子14
311public class Example4_14 
312{  public static void main(String args[]) 
313   { PrimNumber num=new PrimNumber();//要保证PrimNuber类和Example4_14类在同一目录中
314     String a[]={"ok"};
315     System.out.println(a[0]);
316     num.main(a);
317  }

318     }

319
320//例子15
321Trangel.java:
322package tom.jiafei;
323public class Trangle  
324{  double sideA,sideB,sideC;
325   boolean boo;
326   public  Trangle(double a,double b,double c) 
327   { sideA=a;sideB=b;sideC=c;
328    if(a+b>c&&a+c>b&&c+b>a)
329     {  System.out.println("我是一个三角形");
330       boo=true;
331     }
    
332    else
333     { System.out.println("我不是一个三角形");
334       boo=false;
335     }

336   }

337 public void  计算面积() 
338   {    if(boo)
339        double p=(sideA+sideB+sideC)/2.0;
340          double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ;
341          System.out.println("面积是:"+area);
342        }

343      else
344        { System.out.println("不是一个三角形,不能计算面积");
345        }

346   }
 
347 public void 修改三边(double a,double b,double c)
348   { sideA=a;sideB=b;sideC=c;
349     if(a+b>c&&a+c>b&&c+b>a)
350     { boo=true;
351     }
    
352    else
353     { boo=false;
354     }

355   }

356}

357
358Example4_15.java:
359import tom.jiafei.*;
360class Example4_15 
361{  public static void main(String args[])
362  {  Trangle trangle=new Trangle(12,3,1);
363            trangle.计算面积();
364            trangle.修改三边(3,4,5);
365            trangle.计算面积();
366  }

367}

368
369//例子16
370class Example4_16 
371{    private int money;
372    Example4_16() 
373    {  money=2000;
374    }
  
375    private int getMoney() 
376    {  return money;
377    }
 
378    public static void main(String args[]) 
379    { Example4_16 exa=new Example4_16();
380      exa.money=3000;int m=exa.getMoney();
381      System.out.println("money="+m);
382    }

383}

384
385//例子17
386import java.applet.*;
387import java.awt.*;
388class Father 
389{  private int money;
390   float weight,height;
391   String head;
392   String speak(String s) 
393   return s ;
394   }

395}

396class Son extends Father 
397{   String hand ,foot;
398}
 
399public class Example4_17 extends Applet 
400{   Son boy;
401    public void init() 
402   { boy=new Son();
403    boy.weight=1.80f;  boy.height=120f;
404    boy.head="一个头"; boy.hand="两只手 ";
405    boy.foot="两只脚";
406   }

407   public void paint(Graphics g) 
408   {  g.drawString(boy.speak("我是儿子"),5,20);
409     g.drawString(boy.hand+boy.foot+boy.head+boy.weight+boy.height,5,40);
410   }

411}
    
412
413//例子18
414Father.java:
415package tom.jiafei;
416public class  Father 
417{   int  height;
418   protected int money;
419   public   int weight;
420   public Father(int m) {
421   {  money=m;
422   }

423   protected int getMoney() 
424   {  return money;
425   }

426    void setMoney(int newMoney) 
427   {  money=newMoney;
428   }
 
429}

430Jerry.java:
431package sun.com;
432import tom.jiafei.Father;
433public class Jerry extends Father   //Jerry和Father在不同的包中.
434{  public Jerry() 
435   {   super(20);
436   }

437   public static void main(String args[]) 
438   {  Jerry  jerry=new Jerry();
439      jerry.height=12;         //非法,因为Jerry没有继承友好的height。
440      jerry.weight=200;        //合法。
441      jerry.money=800;         //合法。
442      int m=jerry.getMoney(); //合法。.
443      jerry.setMoney(300);    //非法,因为Jerry没有继承友好的方法setMoney。
444      System.out.println("m="+m); 
445   }

446}

447
448//例子19
449import java.applet.*;
450import java.awt.*;
451class Chengji 
452{  float f(float x,float y) 
453   {  return x*y;
454   }

455}

456class Xiangjia  extends Chengji 
457{  float f(float x,float y) 
458   return x+y ;
459   }

460}

461public class Example4_19 extends Applet 
462{  Xiangjia sum;
463   public void init() 
464   {  sum=new Xiangjia();
465   }

466   public void paint(Graphics g) 
467   { g.drawString("sum="+sum.f(4,6),100,40);
468   }

469}
 
470
471//例子20
472import java.applet.*;
473import java.awt.*;
474class Area 
475{  float f(float r ) 
476   {  return 3.14159f*r*r;
477   }

478   float g(float x,float y) 
479   {  return x+y;
480   }

481}

482class Circle extends Area  
483{   float f(float r)
484   {  return 3.14159f*2.0f*r;
485   }
  
486}

487public class Example4_20 extends Applet 
488{  Circle yuan;
489   public void init() 
490   {  yuan=new Circle();
491   }
 
492   public void paint(Graphics g) 
493   { g.drawString("yuan= "+yuan.f(5.0f),5,20);  //调用子类重写的方法f。
494    g.drawString(" "+yuan.g(2.0f,8.0f),5,40);  //调用继承父类的方法g。
495   }

496}
  
497
498//例子21
499class  类人猿 
500{  private int n=100;
501   void crySpeak(String s) 
502   {  System.out.println(s); 
503   }
  
504}

505class People extends 类人猿 
506{  void computer(int a,int b)
507   {  int c=a*b;
508      System.out.println(c); 
509   }

510 void crySpeak(String s) 
511   { System.out.println("**"+s+"**"); 
512   }
  
513}

514class Example4_21 
515{  public static void main(String args[]) 
516   {  类人猿 monkey=new People();   //monkey是People对象的上转型对象。
517      monkey.crySpeak("I love this game");
518      People people=(People)monkey; //把上转型对象强制转化为子类的对象。
519      people.computer(10,10);
520   }

521}

522
523//例子22
524class  动物 
525{  void cry() 
526   {
527   }
  
528}

529class 狗 extends 动物 
530{  void cry() 
531   {  System.out.println("汪汪.."); 
532   }
  
533}

534class 猫 extends 动物 
535{  void cry() 
536   {  System.out.println("喵喵.."); 
537   }
  
538}

539class Example4_22 
540{  public static void main(String args[]) 
541   {  动物 dongwu;
542      if(Math.random()>=0.5)     
543         {
544           dongwu=new 狗();
545           dongwu.cry(); 
546         }

547       else
548         {
549           dongwu=new 猫();
550           ongwu.cry();
551         }

552   }

553}

554
555//例子23
556abstract class 图形 
557{  public abstract double 求面积();
558}

559class 梯形 extends 图形 
560{  double a,b,h;
561   梯形(double a,double b,double h)
562   {  this.a=a;this.b=b;this.h=h;
563   }

564   public double 求面积() 
565   {  return((1/2.0)*(a+b)*h);
566   }

567}

568class 圆形 extends 图形 
569{  double r;
570   圆形(double r)
571   {  his.r=r;
572   }

573   public double 求面积()
574   {  return(3.14*r*r);
575   }

576}

577class 堆 
578   {  图形 底;
579      double 高;
580    堆(图形 底,double 高) 
581   {  this.底=底;
582      this.高=高;
583   }

584    void 换底(图形 底) 
585   {  this.底=底;
586   }

587    public double 求体积() 
588   {   return (底.求面积()*高)/3.0;
589   }

590}

591public class Example4_23
592{  public static void main(String args[]) 
593   {  堆 zui;
594      图形 tuxing;
595      tuxing=new 梯形(2.0,7.0,10.7);
596      System.out.println("梯形的面积"+tuxing.求面积());
597      zui=new  堆(tuxing,30);
598      System.out.println("梯形底的堆的体积"+zui.求体积());
599      tuxing=new 圆形(10);
600      System.out.println("半径是10的圆的面积"+tuxing.求面积());
601      zui.换底(tuxing);
602      System.out.println("圆形底的堆的体积"+zui.求体积());
603   }

604}

605
606//例子24
607class Student 
608{  int number;String name;
609   Student(int number,String name) 
610   {  this.number=number;this.name=name;
611      System.out.println("I am "+name+ "my number is "+number);
612   }

613 }

614class Univer_Student extends Student 
615{  boolean 婚否;
616   Univer_Student(int number,String name,boolean b) 
617   {  super(number,name);
618      婚否=b;
619      System.out.println("婚否="+婚否);
620   }

621 }

622public class Example4_24 
623{  public static void main(String args[]) 
624   {  Univer_Student zhang=new Univer_Student(9901,"和晓林",false);
625   }

626}

627
628//例子25
629class Sum 
630{  int n;
631   float f() 
632   {  float sum=0;
633      for(int i=1;i<=n;i++)
634          sum=sum+i;
635     return sum;  
636   }

637 }

638class Average extends Sum 
639{  int n;  
640   float f() 
641   {  float c;
642      super.n=n;
643      c=super.f();
644      return c/n; 
645   }

646   float g() 
647   {  float c;
648     c=super.f();
649      return c/2
650   }

651 }

652public class Example4_25 
653{  public static void main(String args[]) 
654   {  Average aver=new Average();
655      aver.n=100;
656      float result_1=aver.f();
657      float result_2=aver.g();
658      System.out.println("result_1="+result_1);
659      System.out.println("result_2="+result_2);
660   }

661}

662
663//例子26
664import java.applet.*;import java.awt.*;
665interface Computable 
666{  final int MAX=100;
667   void speak(String s);
668   int f(int x);
669    float g(float x,float y);
670}

671class China implements Computable 
672{  int xuehao;
673   public  int f(int x)   //不要忘记public关键字。
674   {  int sum=0;
675      for(int i=1;i<=x;i++)
676         { sum=sum+i;
677         }

678       return sum;
679   }

680   public float g(float x,float y)
681   {  return 6;                   //至少有return语句。  
682   }

683   public void speak(String s) 
684   
685   }

686}

687class Japan implements Computable 
688{  int xuehao;
689   public int f(int x) 
690   {  return 68
691   }

692    public  float g(float x,float y) 
693   {  return x+y;
694   }

695 public void speak(String s) 
696   {                            //必须有方法体,但体内可以没有任何语句。
697   }

698}

699public class Example4_26 extends Applet 
700{  China Li; Japan Henlu;
701   public void init() 
702   {  Li=new China();   Henlu=new Japan();  
703      Li.xuehao=991898; Henlu.xuehao=941448;
704   }

705   public void paint(Graphics g) 
706   {  g.drawString("xuehao:"+Li.MAX+Li.xuehao+"从1到100求和"+Li.f(100),10,20);
707      g.drawString("xuehao:"+Henlu.MAX+Henlu.xuehao+"加法"+Henlu.g(2.0f,3.0f),10,40);
708   }

709}
   
710
711//例子27
712interface  收费 
713public void  收取费用();
714}

715class 公共汽车 implements 收费 
716{  public  void 收取费用() 
717   {  System.out.println("公共汽车:一元/张,不计算公里数");
718   }

719 }
 
720class 出租车 implements 收费 
721{  public void 收取费用() 
722   {  System.out.println("出租车:1.60元/公里,起价3公里");
723   }

724 }

725class 电影院 implements 收费 
726{  public void 收取费用()
727   {  System.out.println("电影院:门票,十元/张");
728   }

729}

730class Example4_27 
731{  public static void main(String args[])
732   {  公共汽车 七路=new 公共汽车();
733      出租车   天宇=new 出租车();
734      电影院   红星=new 电影院();
735      七路.收取费用();天宇.收取费用();
736      红星.收取费用();
737   }

738}

739
740//例子28
741interface  ShowMessage 
742{   void 显示商标(String s);
743}

744class TV implements ShowMessage 
745{  public void 显示商标(String s) 
746   {  System.out.println(s);
747   }

748}

749class PC implements ShowMessage 
750{  public void 显示商标(String s) 
751   {  System.out.println(s);
752   }

753}

754public class Example4_28 
755{  public static void main(String args[])
756   {  ShowMessage sm;                  //声明接口变量。
757      sm=new TV();                     //接口变量中存放对象的引用。
758      sm.显示商标("长城牌电视机");      //接口回调。
759      sm=new PC();                     //接口变量中存放对象的引用。
760      sm.显示商标("联想奔月5008PC机"); //接口回调。
761   }
 
762}

763
764//例子29
765interface  Computerable
766{  public  double 求面积();
767}

768class 梯形 implements Computerable 
769{  double a,b,h;
770    梯形(double a,double b,double h) 
771   {  this.a=a;this.b=b;this.h=h;
772   }

773    public double 求面积() 
774   {  return((1/2.0)*(a+b)*h);
775   }

776}

777class 圆形 implements Computerable 
778{  double r;
779   圆形(double r) 
780   {  this.r=r;
781   }

782   public double 求面积() 
783   { eturn(3.14*r*r);
784   }

785}

786class 堆 
787{  Computerable 底;           //声明一个接口变量,可以回调"求面积"方法。
788   double 高;
789   堆(Computerable 底,double 高) 
790   {  this.底=底;
791      this.高=高;
792   }

793    void 换底(Computerable 底) 
794   {  this.底=底;
795   }

796    public double 求体积()
797   {  return (底.求面积()*高)/3.0;
798   }

799}

800public class Example4_29 
801{  public static void main(String args[])
802   {  堆 zui;
803      Computerable bottom;
804      bottom=new 梯形(2.0,7.0,10.7); //接口变量中存放对象的引用。
805      System.out.println("梯形的面积"+bottom.求面积()); //bottom接口回调,求面积。
806      zui=new  堆(bottom,30);
807      System.out.println("梯形底的堆的体积"+zui.求体积());
808      bottom=new 圆形(10);  //接口变量中存放对象的引用。
809  System.out.println("半径是10的圆的面积"+bottom.求面积());
810  zui.换底(bottom);
811      System.out.println("圆形底的堆的体积"+zui.求体积());
812   }

813}

814
posted @ 2005-05-27 08:43  Rookie.Zhang  阅读(1339)  评论(0编辑  收藏  举报