302班

          java突擊隊
          posts - 151, comments - 74, trackbacks - 0, articles - 14
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          interface實例

          Posted on 2007-06-15 19:26 停留的風 閱讀(281) 評論(0)  編輯  收藏
          抽象類就是說的一個概念,他不能實例化,而子類則必須將抽象類的方法重新定義,否則,子類本身也就成為一個抽象類。
          interface內所有的方法都是public,所有的成員變量都是public static final,即使你沒有申明。interface里的值必須是在編譯的時候確定。

          例一.

          public interface Shape {

          void draw() {}

          void erase() {}

          }

          class Circle extends Shape {

          void draw() {

          System.out.println("Circle.draw()");

          }

          void erase() {

          System.out.println("Circle.erase()");

          }

          }

          class Square extends Shape {

          void draw() {

          System.out.println("Square.draw()");

          }

          void erase() {

          System.out.println("Square.erase()");

          }

          }

          class Triangle extends Shape {

          void draw() {

          System.out.println("Triangle.draw()");

          }

          void erase() {

          System.out.println("Triangle.erase()");

          }

          }

          public class Shapes {

          public static Shape randShape() {

          switch((int)(Math.random() * 3)) {

          default: // To quiet the compiler

          case 0: return new Circle();

          case 1: return new Square();

          case 2: return new Triangle();

          }

          }

          public static void main(String[] args) {

          Shape[] s = new Shape[9];

          // Fill up the array with shapes:

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

          s[i] = randShape();

          // Make polymorphic method calls:

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

          s[i].draw();

          }

          }

          例二.

          public interface print {

          public void println();

          }

          //--------------------------------------------------------------

          package com.exaple;

          /**

          * @author liu yu

          *

          * To change the template for this generated type comment go to

          * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

          */

          class A implements print{

          int a = 3;

          public void println(){

          System.out.println("The value of Class A : "+a);

          }

          }

           

          class B implements print{

          int b = 4;

          public void println(){

          System.out.println("The value of Class B : "+b);

          }

          }

           

          public class MyInterfaceDemo {

           

          public static void dosomething(print c){

          c.println();

          }

          public static void main(String [] args){

          A a1 = new A();

          B b1 = new B();

          MyInterfaceDemo.dosomething(a1);

          MyInterfaceDemo.dosomething(b1);

          }

          }


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


          網站導航:
           
          主站蜘蛛池模板: 临汾市| 类乌齐县| 镇远县| 炉霍县| 阜阳市| 神农架林区| 长海县| 措勤县| 天全县| 渝中区| 开阳县| 金溪县| 九台市| 沂水县| 麻阳| 余庆县| 日照市| 太保市| 西平县| 湾仔区| 南陵县| 东丽区| 宜州市| 永定县| 若尔盖县| 达拉特旗| 荆州市| 鲁甸县| 改则县| 湟源县| 合作市| 张北县| 青田县| 商城县| 云浮市| 芦溪县| 南皮县| 格尔木市| 平顶山市| 商城县| 策勒县|