331929879

          2007年10月12日 #

          繼承類


          class Father
          {
           int age;
           String name;
           void eat()
           {
            System.out.println("吃饃!");
           }
          }
          class Son extends Father
          {
           int height;
           void eat()
           {
            super.eat();
            System.out.println("喝酒");
           }
          }
          class Ex
          {
           public static void main(String args[])
           {
            Son p=new Son();
            Father p1=new Son();
            p1.eat();
            p.eat();
           }
          }

          posted @ 2007-10-16 14:47 小飛 閱讀(162) | 評論 (0)編輯 收藏

          復數運算

          class Complex{
           double real;
              double image;

              public Complex add(Complex another){
            Complex res = new Complex();
            res.real = real + another.real;
            res.image = image + another.image;
            return res;
              }
              
           public Complex minus(Complex another){// 當前復數與方法參數another相減;
            Complex r = new Complex();
            r.real = real - another.real;
            r.image = image - another.image;
            return r;
           }
           
           public Complex multi(Complex another){ //當前復數與方法參數another相乘(注意復數相乘); 
            Complex r = new Complex();
            r.real = real * another.real - image * another.image;
            r.image = real * another.image + image * another.real ;
            return r;
           }
           
           public void disp(){
            if( image == 0 ){
             System.out.print(real);
            }
            else
            {
             System.out.print(real + " + " + image + "i");
            }
           }
           
           public Complex(double real , double image){
            this.real = real;
            this.image = image ;
           }
           public Complex(){
            this(0 , 0);
           }
           
           public static void main (String[] args) {
            Complex c1 = new Complex(2 , 3);
            Complex c2 = new Complex(5 , 4);
            Complex res;
            
            c1.disp();
            System.out.print("  +   ");
            c2.disp();
            System.out.print("  =   ");
            c1.add(c2).disp();
            System.out.println();
            
            c1.disp();
            System.out.print("  -   ");
            c2.disp();
            System.out.print("  =   ");
            c1.minus(c2).disp();
            System.out.println();
            
            
            c1.disp();
            System.out.print("  *   ");
            c2.disp();
            System.out.print("  =   ");
            c1.multi(c2).disp();
            System.out.println();
              }
          }

          posted @ 2007-10-12 09:13 小飛 閱讀(279) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 隆化县| 固始县| 乐都县| 榆社县| 翼城县| 广饶县| 孟州市| 晋江市| 九江市| 抚远县| 芮城县| 绥德县| 宁津县| 固安县| 威信县| 宜春市| 隆德县| 东阳市| 赫章县| 吴忠市| 黑山县| 钟山县| 洛川县| 康保县| 龙南县| 双牌县| 西充县| 德钦县| 和田市| 阆中市| 仁布县| 林芝县| 旺苍县| 南溪县| 清远市| 泗阳县| 林口县| 合阳县| 婺源县| 饶阳县| 蓬安县|