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);

          }

          }


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


          網站導航:
           
          主站蜘蛛池模板: 贵溪市| 山东省| 平武县| 鹤岗市| 新源县| 潜山县| 亚东县| 电白县| 长子县| 莎车县| 宝鸡市| 石门县| 河北省| 东城区| 周口市| 陈巴尔虎旗| 高邮市| 通江县| 南涧| 莲花县| 旬阳县| 承德市| 新干县| 水富县| 沙湾县| 威海市| 万盛区| 纳雍县| 德保县| 博湖县| 卓尼县| 南和县| 高清| 疏勒县| 新源县| 宜州市| 图们市| 九台市| 拉孜县| 大田县| 于都县|