关闭

快速掌握接口和抽象类的定义方式

发表于:2008-2-18 13:53

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

#
java

接口定义

  关于java的接口定义方式,以下三种情况下可以采用接口定义方式:

◆接口中声明的变量全部为final 和static类型的,并且这个接口的作用在于定义一些值不能改变的变量。

举个例子:


public interface ObjectConstants{
public static final String SPACE = new String(" ");
public static final char FORMFEED = '\f';
}
 

 

◆接口中只定义可供实现的抽象方法


EventListener.java
    public interface EventListener {
    public void handleEvent(Event evt);
    }
Runnable.java
package java.lang;
    public interface Runnable {  
    public abstract void run();
    }
 

◆还有一种方式是上述两种方式的组合,如非必要一般会将这样一个接口定义拆分成两个接口定义

抽象类的定义

1.如果一个类包含一个接口但是不完全实现接口定义的方法,那么该类必须定义成abstract型

例如nputStream.java类的定义方式:


package java.io;
public abstract class InputStream implements Closeable {
    // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
    private static final int SKIP_BUFFER_SIZE = 2048;
    // skipBuffer is initialized in skip(long), if needed.
    private static byte[] skipBuffer;  
    public abstract int read() throws IOException;

  
    public int read(byte b[]) throws IOException {
    return read(b, 0, b.length);
    }  
    public int read(byte b[], int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if ((off < 0) || (off > b.length) || (len < 0) ||
           ((off + len) > b.length) || ((off + len) < 0)) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }
    int c = read();
    if (c == -1) {
        return -1;
    }
    b[off] = (byte)c;
    int i = 1;
    try {
        for (; i < len ; i++) {
        c = read();
        if (c == -1) {
            break;
        }
        if (b != null) {
            b[off + i] = (byte)c;
        }
        }
    } catch (IOException ee) {
    }
    return i;
    }
 public long skip(long n) throws IOException {
    long remaining = n;
    int nr;
    if (skipBuffer == null)
        skipBuffer = new byte[SKIP_BUFFER_SIZE];
    byte[] localSkipBuffer = skipBuffer;       
    if (n <= 0) {
        return 0;
    }
    while (remaining > 0) {
        nr = read(localSkipBuffer, 0,
              (int) Math.min(SKIP_BUFFER_SIZE, remaining));
        if (nr < 0) {
        break;
        }
   remaining -= nr;
    }   
    return n - remaining;
    }
    public int available() throws IOException {
    return 0;
    }  
    public void close() throws IOException {} 
    public synchronized void mark(int readlimit) {}   
    public synchronized void reset() throws IOException {
    throw new IOException("mark/reset not supported");
    }
    public boolean markSupported() {
    return false;
    }
}
 

2.抽象类的方法体中只定义抽象的方法,例如AbstractMethodError.java


package java.lang;
    public class AbstractMethodError extends IncompatibleClassChangeError {
    public AbstractMethodError() {
    super();}
    public AbstractMethodError(String s) {
    super(s); }
}
 

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号