302班

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

          interface實例

          Posted on 2007-06-15 19:26 停留的風 閱讀(284) 評論(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);

          }

          }


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


          網站導航:
           
          主站蜘蛛池模板: 南和县| 平陆县| 洛宁县| 绿春县| 黄陵县| 会东县| 鹤岗市| 华蓥市| 桂林市| 中宁县| 大洼县| 开平市| 华容县| 正宁县| 轮台县| 延长县| 玉屏| 和静县| 阿合奇县| 潞城市| 元江| 文昌市| 长宁区| 和顺县| 当雄县| 逊克县| 昭苏县| 安阳县| 沧州市| 仙桃市| 满洲里市| 大同市| 古丈县| 应用必备| 孟津县| 浪卡子县| 高碑店市| 永修县| 武穴市| 建始县| 潼南县|