A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 棉/mg花/x糖 中级黑马   /  2013-5-22 11:48  /  1062 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 棉/mg花/x糖 于 2013-5-22 12:56 编辑

关于多重继承接口总结

多重继承是指一个子类可以有多个直接父类。而Java出于安全性、简化程序结构的考虑只支持单继承。
那么,在Java中怎么实现多重继承机制呢?---->答案是:通过接口来实现!


1、接口的声明(定义)
      
         [修饰符] interface 接口名 [extends 父接口名列表] {
                常量数据成员声明;
                抽象方法声明;
        }

(1)接口定义用关键字interface,而不是用class,可以把接口看成一个特殊类,或者“纯”的抽象类。
(2)修饰符有两种:和普通类的修饰符一样,只有public和默认两种。
        ①public:表示该接口可以被所有的类和接口使用;
        ②默认:表示该接口只能被同一包中的类和接口使用。
(3)父接口列表:接口也具有继承性。
        ①可以用extends声明该接口是某个已经存在的父接口的派生接口。
        ②对于类继承,extends后面只能允许有一个父类。
        ③接口与类继承不同,一个接口可以有一个以上的父接口即extends后边可以有多个接口,之间用逗号隔开
(4)常量数据成员声明:常量数据成员前可以有也可以没有修饰符,且默认全是final static,即常量
        即常量数据成员默认为:public final static或final static。
(5)抽象方法声明:接口中默认的都是public abstract修饰的抽象方法。
        ①public可写可不写,不写也认为是public abstract方法;
        ②接口中的方法体可以由Java语言书写,也可以由其他语言书写。由其他语言书写时,接口方法由native修饰符修
饰。
(6)接口中没有自身的构造方法,所有的成员方法都是抽象方法,且均默认为是public abstract方法。


2、接口的实现

在Java中,通常对接口功能的“继承”称为“实现”。
(1)在类中,用implements关键字就可以调用接口。要调用多个接口时,接口之间用逗号隔开。
(2)如果实现某接口的类不是abstract类,则在类的定义部分必须实现指定接口所有抽象方法
(3)如果实现某接口的类是abstract类,则它可以不实现该接口所有抽象方法
(4)接口的抽象方法访问限制符均指定为public,所以类在实现方法时,必须显式地使用public修饰符,否则将
会被系统警告为
        缩小了接口中定义的方法的访问控制范围。

=====================================================================================

补充:多重继承和多层继承的区别。

很多人可能会将多重继承和多层继承混淆,这里我必须强调一下:多重继承不是多层继承!!
(1)多重继承:是指extends/implements后面可以有多个接口,是横向结构。例如:
        ①接口D同时继承接口A、B、C:
            interface D extends A,B,C {}
        ②类D同时实现接口A、B、C:
            class D implements A,B,C {}
        这样,D与A、B、C之间就形成一个横向的实现关系,称之为多重继承。
(2)多层继承:是指类的层状继承关系,是纵向结构。例如:
        类D继承类C,类C继承类B,类B又继承类A。
        这样,A、B、C、D之间就形成一个纵向的继承关系,称之为多层继承。
=====================================================================================

下面是一个关于接口的示例:Test7.java
  1. package com.yb.Test;

  2. import java.applet.Applet;
  3. import java.awt.Graphics;

  4. //import com.yb.DrawPolygon.Circle;
  5. //import com.yb.DrawPolygon.Shapes;
  6. //import com.yb.DrawPolygon.Square;
  7. //import com.yb.DrawPolygon.Triangle;

  8. interface Shapes {
  9.     abstract double getArea();
  10.     abstract double getPerimeter();
  11. }

  12. class Coordinates {
  13.     int x,y;
  14.     public Coordinates(int x,int y) {
  15.         this.x = x;
  16.         this.y = y;
  17.     }
  18. }

  19. //Square Coordinates Shapes
  20. class Square extends Coordinates implements Shapes {
  21.     public int width,height;
  22.     public double getArea() {
  23.         return(width*height);
  24.     }
  25.     public double getPerimeter() {
  26.         return(2*width+2*height);
  27.     }
  28.     public Square(int x,int y,int width,int height) {
  29.         super(x,y);
  30.         this.width = width;
  31.         this.height = height;
  32.     }
  33. }

  34. //Triangle Coordinates Shapes
  35. class Triangle extends Coordinates implements Shapes {
  36.     public int width,height;
  37.     public double c;
  38.     public double getArea() {
  39.         return(0.5*width*height);
  40.     }
  41.     public double getPerimeter() {
  42.         return(width+height+c);
  43.     }
  44.     public Triangle(int x,int y,int base,int height) {
  45.         super(x,y);
  46.         width = base;
  47.         this.height = height;
  48.         c = Math.sqrt(width*width+height*height);
  49.     }
  50. }

  51. //Circle Coordinates Shapes
  52. class Circle extends Coordinates implements Shapes {
  53.     public int width,height;
  54.     public double r;
  55.     public double getArea() {
  56.         return(r*r*Math.PI);
  57.     }
  58.     public double getPerimeter() {
  59.         return(2*Math.PI*r);
  60.     }
  61.     public Circle(int x,int y,int width,int height) {
  62.         super(x,y);
  63.         this.width = width;
  64.         this.height = height;
  65.         r = (double)width/2.0;
  66.     }
  67. }

  68. @SuppressWarnings("serial")
  69. public class Test7 extends Applet {
  70.     Square box = new Square(5,15,25,25);
  71.     Triangle tri = new Triangle(5,50,8,4);
  72.     Circle oval = new Circle(5,90,25,25);
  73.     public void paint(Graphics g) {
  74.         //Draw a square, then output of its area and perimeter.
  75.         g.drawRect(box.x,box.y,box.width,box.height);
  76.         g.drawString("Box Area:"+box.getArea(),50,35);
  77.         g.drawString("Box Perimeter:"+box.getPerimeter(),50,55);

  78.         //Note: only know the bevel edge, bottom edge and high, we are unable to draw a triangle.
  79.         //Here only the output of the triangle area and perimeter.
  80.         g.drawString("Tri Area:"+box.getArea(),50,75);
  81.         g.drawString("Tri Perimeter:"+box.getPerimeter(),50,95);

  82.         //Draw a circle, then output of its area and perimeter.
  83.         g.drawOval(oval.x,oval.y,oval.width,oval.height);
  84.         g.drawString("Oavl Area:"+oval.getArea(),50,115);
  85.         g.drawString("oval Area:"+oval.getPerimeter(),50,135);
  86.     }
  87. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马