posts - 0,  comments - 0,  trackbacks - 0

           

          //數組申明的幾種方式,我寫的這幾個破類 為什么數組只能在語句塊中申明?

          public class Array

          {

              static int[] array1;

             

              {

                  array1=new int[]{1,1,1,1,1};

                 

                  int[] array2={2,2,2,2,2};

                 

                  int[] array3=new int[5];

                  for(int n=0;n<array3.length;n++)

                  {

                      array3[n]=3;

                      System.out.println("array3:"+array3[n]+" ");

                  }

                 

                  int[][] array4 ;

                  array4=new int[][]{{1,1,1},{2,2},{3}};

                 

                  int[][]array5={{1,1,1},{2,2},{3}};

                 

                  int[][] array6=new int[3][3] ;

                  for(int i=0;i<array6.length;i++)

                      for(int j=0;j<array6[i].length;j++)

                      {

                          array6[i][j]=(i+j)*2;

                          System.out.println("array6: "+array6[i][j]);

                      }

                 

              }

             

           

          }

          //變量

          class Variable

          {

               

              {

              byte variable1=1;      //一個字節

              short variable2=2;     //二個字節

              int variable3=4;       //四個字節

              long variable4=8L;     //八個字節

             

              char variable5=2;      //二個字節 無符號

              char variable6='a';   

              float variable7=4.4F//四個字節 小數常量默認為double類型,如果要賦浮點型就要加f

              double variable8=8.8;

              variable1+=6;//八個字節

              //variable1=variable1+1;//byte類型參與運算后自動專為int類型不能賦給byte

              System.out.println("variable1="+variable1);

              }

             

          }

           

          class Test

          {

              public static void main(String[] args)

              {

                  Array ay=new Array();

                  //ay.toString();

                  Variable va=new Variable();

                  System.out.println("array1="+Array.array1[4]);

                 

                  System.out.print("va equal ay ? "+va.equals(ay));

              }

          }

          /*結果:array3:3

          array3:3

          array3:3

          array3:3

          array3:3

          array6: 0

          array6: 2

          array6: 4

          array6: 2

          array6: 4

          array6: 6

          array6: 4

          array6: 6

          array6: 8

          variable1=7

          array1=1

          va equal ay ? false

          */

           

          ***********************************************************************

          接口:

           

          interface Lunzi//缺省接口只能在同一個包中進行訪問

          {

              int heavy=20;//接口中的數據成語都是public static final

              public abstract void run();//共有的抽象

              void daqi();//接口中所有的方法默認都是共有的抽象的

          }

           

          interface Cheshen

          {

              int longs=5;

              void touguang();

              void touqi();

             

          }

          class Dazhong implements Cheshen,Lunzi //大眾廠商生產車身和輪子

          {

              public void output()

              {

                  System.out.println("輪子重量"+heavy+"車身長度"+longs );

              }

              public void run()

              {

                  System.out.println("輪子轉動正常");

              }

              public void daqi()

              {

                  System.out.println("輪子充氣良好");

              }

              public void touguang()

              {

                  System.out.println("車身透光良好");

              }

              public void touqi()

              {

                  System.out.println("車身透氣性好");

              }

          }

          class Cheban//車板安裝車身和輪子

          {

              Lunzi lz;

              Cheshen cs;

              void setLunzi(Lunzi lz)//使用接口Lunzi安裝輪子。

               //這里傳的參數看似Lunzi接口的一個實例,其實是大眾的實例,

               //因為最后將把大眾的實例的引用傳給LZ,接口是不能被實例化的。和任何抽象類一樣。

              {

                  this.lz=lz;

              }

              void setCheshen(Cheshen cs)

              {

                  this.cs=cs;

              }

             

              void runcar()

              {

                 

                  lz.daqi();

                  lz.run();

                  cs.touguang();

                  cs.touqi();

                  System.out.println("嗚嗚~~~~~~~~");

              }

              public static void main(String[] args)

              {

                  Dazhong dz=new Dazhong();

                  //Dazhong dz1=new Dazhong();

                  dz.output();

                  System.out.println("輪子重量="+dz.heavy+" or"+Lunzi.heavy+

                   "; 車身長度="+dz.longs+" or "+Cheshen.longs);//因為接口中的數據成員都是共有靜態的

                  Cheban cb=new Cheban();

                  cb.setCheshen(dz);

                  cb.setLunzi(dz);//大眾的實例的引用傳給LZ

                  cb.runcar();

              }

          }

          //運行結果:

          /*輪子重量20車身長度5

          輪子重量=20 or20; 車身長度=5 or 5

          輪子充氣良好

          輪子轉動正常

          車身透光良好

          車身透氣性好

          嗚嗚~~~~~~~~

          */

           

          /**JAVA不允許類的多繼承 但允許接口的多繼承 類可以實現多個接口 也可以繼承一個類同時實現多個接口;*/

           

          /**************************內部類***********************/  

          /********************文件一:類的嵌套*********************/

          /*class Outer

          {

              private static int index=100;//內部類可以訪問外部類的私有變量

              void text()

              {

                  System.out.println("內部類調用外部類的方法");

              }

              void fn(final int a)//內部類的嵌套

              {

              final int b=0;//方法中的局部變量或參數如果需要被 方法中的內部類訪問就必須聲

          //明為常量;

                  if(true)

                  {

                      class Middle

                      {

                          private int index=60;

                          class Inner

                          {

                              private int index=50;

                              void print()

                              {

                                  text();

                                  int index=30;

                                  System.out.println("方法中的index : "+index);

                                  System.out.println("Inner中成員變量index : "+this.index);

                                  System.out.println("Middle.this.index : "+Middle.this.index);

                                  System.out.println("Outer.this.index : "+Outer.this.index);

                                  //a=5;

                                  //b=6;

                                  System.out.println("常量a="+a);

                                  System.out.println("常量b="+b);

                              }

                          }

                      }

          Middle md=new Middle();        //在語句塊中定義的內部類其作用域只在塊內。在語句塊外// 不能實例化;

                      Middle.Inner in=md.new Inner();

                      in.print();

                  }

              }

             

             

          }

           

          class Test

          {

              public static void main(String[] args)

              {

                 

                 

                  Outer outer=new Outer();

                  outer.fn(1111);//通過方法調用方法中的內部類

              }

          }

          /*運行結果

          內部類調用外部類的方法

          方法中的index : 30

          Inner中成員變量index : 50

          Middle.this.index : 60

          Outer.this.index : 100

          常量a=1111

          常量b=0

          */

           

           

          /**********************內部類文件二***********************/

           

          class Outer

          {

              private static int index=100;//內部類可以訪問外部類的私有變量還可以用abstract final

              void text()

              {

                  System.out.println("內部類調用外部類的方法");

              }

              private static class Inner//內部類可以有和成員方法一樣的訪問控制符當其被聲明為靜態的時候 就切斷了與外部類的對象的聯系,不能再訪問外部類中的非靜態成員,

              {

                  private int index=50;

                   void print()

                  {

                      int index=30;

                      System.out.println(index);

                      System.out.println(this.index);

                      System.out.println(Outer.index);

                      //Outer.this.text();//text為非靜態方法所以不能被靜態內部類訪問

                     

                  }

                   static void print2()

                   {

                       System.out.println("只有頂層類和靜態內部類才可以有靜態方法");

                       //System.out.println(this.index);不能在靜態環境中使用this 因為靜態成員屬于類 與類的對象無關;

                      System.out.println(Outer.index);

                   }

              }

             

              void print()                      

              {

                  Inner inner=new Inner();//在外部類的方法中實例化內部類

                  inner.print();

              }

              Inner getInner()

              {

                  return new Inner();//外部類的方法中實例化并返回一個內部類對象

              }

             

              public static void main(String[] args)

              {

                  Outer outer=new Outer();

                  outer.print();

                 

                  Inner inner1=outer.getInner();//mainOuter中此時Inner可見所以不用Outer.Inner

                  inner1.print();

                  Inner.print2();

                 

                  //Inner inner2=outer.new Inner();//此時Inner可見,但還是不能直接new Inner(); 如果內部類被聲明為靜態的 此語句錯誤,因為內部類已經失去了與外部類對象的聯系 即沒有了 Outer.this

                  //inner2.print();

              }

          }

          class Test

          {

              public static void main(String[] args)

              {

                  //三種方法調用Inner類的print();

                  Outer outer=new Outer();//第一種

                  outer.print();

                 

                  Outer.Inner inner1=outer.getInner();//第二種

                  inner1.print();

                 

                 

                  Outer.Inner inner2=outer.new Inner();//第三種 直接實例化,特殊語法:out.new 因為在創建內部類的時候需要外部類對象的一個引用因為外部類可能有多個對象,

                  inner2.print();

                 

                 

              }

          }

           

          /*運行結果:

          30

          50

          100

          內部類調用外部類的方法

          30

          50

          100

          內部類調用外部類的方法

          30

          50

          100

          */

           

          /****繼承內部類**/

          class Car

          {

              class Wheel

              {

              }

          }

           

          class PlaneWheel extends Car.Wheel

          {

              PlaneWheel(Car car)

              {

                  car.super();//特殊語法 建立內部類與指定外部類對象的聯系;

              }

              public static void main(String[] args)

              {

                  Car car=new Car();

                  //Car.Wheel wheel=car.new Wheel();

                  PlaneWheel pw=new PlaneWheel(car);

              }

          }

           

          /*****用匿名的內部類 實現繼承同時實現抽象類**/

           

          class A

          {

              void fn1()

              {

                  System.out.println("實現A的方法");

              }

          }

           

          abstract class B

          {

              abstract void fn2();

          }

           

          class C extends A //用匿名的內部類 實現繼承同時實現抽象類(相當于多繼承)

          {

              B getB()

              {

                  return new B()

                  {                         //括號內為一個匿名的內部類用以在return語句結束之前實現抽象類

                      public void fn2()

                      {

                          System.out.println("實現了抽象類B的方法");

                      }

                  };//分號不能省 標志return的結束

              }

          }

           

          class Test

          {

              static void method1(A a)//靜態方法傳遞A實例的一個引用 以調用fn1()

              {

                  a.fn1();

              }

              static void method2(B b)//靜態方法傳遞B實例的一個引用 以調用fn2() B不能被實例化

              {

                  b.fn2();

              }

              public static void main(String[] args)

              {

                  C c=new C();//創建C的一個實例

                  A a=new A();

                  method1(c);//因為C繼承自A所以C的實例是A的實例;可以在任何需要超類對象的地方用子類對象

                  method2(c.getB());

                  System.out.println("(c isInstanceOf A)"+(c instanceof A));//true

                  System.out.println("(a isInstanceOf C)"+(a instanceof C));//false

              }

          }

           

           

          /****內部類 和匿名內部類實現接口*****/

          interface Animal

          {

              void eat();

              void sleep();

          }

           

          class Zoo

          {

              private class Tiger implements Animal

              {

                  public void eat()

                  {

                      System.out.println("tiger eat");

                  }

                  public void sleep()

                  {

                      System.out.println("tiger sleep");

                  }

              }

              Animal getAnimal()

              {

                  return new Tiger();

              }

              /*Animal getAnimal()

              {

                  return new Animal()//匿名內部類實現接口。必須在返回對象之前即return語句的封號前;

                  {

                      public void eat()

                      {

                          System.out.println("animal eat");

                      }

                      public void sleep()

                      {

                          System.out.println("animal sleep");

                      }

                  };

              }*/

          }

           

          class Test

          {

              public static void main(String[] args)

              {

                  Zoo z=new Zoo();

                  Animal an=z.getAnimal();

                  an.eat();

                  an.sleep();

              }

          }

           

          //一個類可以在繼承的同時取實現接口,但如果他要繼承的類和要實現的接口

          //有同名的方法時 如果直接實現不好,所以用內部類實現接口;

          interface Machine

          {

              void run();

          }

           

          class Person

          {

              void run()

              {

                  System.out.println("run");

              }

          }

           

          class Robot extends Person

          {

              private class MachineHeart implements Machine

              {

                  public void run()

                  {

                      System.out.println("heart run");

                  }

              }

              void run()

              {

                  super.run();

                  MachineHeart mheart=new MachineHeart();

                  mheart.run();

              }

              Machine getMachine()

              {

                  return new MachineHeart();

              }

          }

           

          class Test1

          {

              public static void main(String[] args)

              {

                  Robot robot=new Robot();

                  robot.run();

                 

                  Machine m=robot.getMachine();

                  robot.run();

                 

              }

          }

           

          posted on 2007-05-07 21:09 唐佚 閱讀(116) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 贡嘎县| 徐闻县| 丽水市| 潜江市| 牙克石市| 格尔木市| 彭泽县| 新丰县| 玛曲县| 永登县| 赣榆县| 四平市| 克什克腾旗| 洪雅县| 铜鼓县| 曲沃县| 西城区| 东兴市| 盐亭县| 启东市| 门头沟区| 罗平县| 阜宁县| 北京市| 来宾市| 油尖旺区| 黑河市| 新化县| 寻甸| 安远县| 营山县| 哈巴河县| 浦城县| 正阳县| 威信县| 灵武市| 兴和县| 克什克腾旗| 五大连池市| 德保县| 大悟县|