302班

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

          interface實例

          Posted on 2007-06-15 19:26 停留的風 閱讀(284) 評論(0)  編輯  收藏
          抽象類就是說的一個概念,他不能實例化,而子類則必須將抽象類的方法重新定義,否則,子類本身也就成為一個抽象類。
          interface內(nèi)所有的方法都是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);

          }

          }


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 浙江省| 承德县| 榆林市| 荔波县| 喀什市| 仪陇县| 祁阳县| 石家庄市| 龙州县| 任丘市| 蒙城县| 华阴市| 鄂伦春自治旗| 宝应县| 于都县| 孝昌县| 盐源县| 裕民县| 衡水市| 石家庄市| 碌曲县| 台安县| 新郑市| 郑州市| 安吉县| 郯城县| 固始县| 常山县| 罗甸县| 都昌县| 图片| 赤水市| 仪征市| 自治县| 茶陵县| 南江县| 玉龙| 东平县| 阳春市| 班玛县| 古交市|