`
zy77612
  • 浏览: 278243 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java接口中的内部接口和接口中的内部类

阅读更多

我们常用的interface用法,定义单独的接口,或者extends另外的interface.很少会在接口中再定义接口或是在接口中义类.我们看下面定义的接口:

代码1:

package test;
public interface A {
	public void getA()throws AException;
	public static interface B{
		public void getB();
	}
	public class AException extends java.lang.Exception{
		public AException(String msg){
			super(msg);			
		}
	}
}

 下面是接口的两种实现:

 代码2:

package test;
public class ImplA implements A {
	@Override
	public void getA() throws A.AException {
		System.out.println("Implements A interface");
		A.AException e = new A.AException("This is AException");
		throw e;
	}
	public static class ImplB implements A.B {
		@Override
		public void getB() {
			System.out.println("Implements B interface");
		}
	}
	public static void main(String[] arg) {
		ImplA testA = new ImplA();
		try {			
			testA.getA();
		} catch (A.AException e) {
			e.printStackTrace();
		}
		A.B testB = new ImplA.ImplB();
		testB.getB();
	}
}

  代码3:

package test;
public class ImplA implements A,A.B {
	@Override
	public void getA() throws A.AException {
		System.out.println("Implements A interface");
		A.AException e = new A.AException("This is AException");
		throw e;
	}
	@Override
	public void getB() {
		System.out.println("Implements B interface");
	}
	public static void main(String[] arg) {
		ImplA testA = new ImplA();
		try {			
			testA.getA();
		} catch (A.AException e) {
			e.printStackTrace();
		}		
		testA.getB();
	}
}

 

  

我们可以理解为上面定义的A接口和内部接口B,是两个接口A和B。可以用单独类ImplA实现A接口,单独类ImplB实现B接口,然后客户端调用相应的接口方法。也可以像代码3中实现父接口和内部接口。

我理解这种接口一般是定义一种层次结构或者是包含关系。接口的内部类用法与类里面的成员类用法有所不同,如上面A.AException类的定义前面没有static,我们客户端的可以这样实例化:A.AException ex = new A.AException("This is Exception");但是成员类就不能这样实例化,如代码2中:public static class ImplB implements A.B ,如果没有在前面用static 装饰,我们客户端就不能实例化:ImplA.ImplB b = new ImplA.ImplB();(编绎出错)。这个不同让我不解,看以后能搞懂吗?

 

 

 

可以参考java.util.Map 和HashMap

 

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.util;


/**
 * A {@code Map} is a data structure consisting of a set of keys and values
 * in which each key is mapped to a single value.  The class of the objects
 * used as keys is declared when the {@code Map} is declared, as is the 
 * class of the corresponding values.
 * <p>
 * A {@code Map} provides helper methods to iterate through all of the
 * keys contained in it, as well as various methods to access and update 
 * the key/value pairs.  
 */
public interface Map<K,V> {

    /**
     * {@code Map.Entry} is a key/value mapping contained in a {@code Map}.
     */
    public static interface Entry<K,V> {
        /**
         * Compares the specified object to this {@code Map.Entry} and returns if they
         * are equal. To be equal, the object must be an instance of {@code Map.Entry} and have the
         * same key and value.
         * 
         * @param object
         *            the {@code Object} to compare with this {@code Object}.
         * @return {@code true} if the specified {@code Object} is equal to this
         *         {@code Map.Entry}, {@code false} otherwise.
         * @see #hashCode()
         */
        public boolean equals(Object object);

        /**
         * Returns the key.
         * 
         * @return the key
         */
        public K getKey();

        /**
         * Returns the value.
         * 
         * @return the value
         */
        public V getValue();

        /**
         * Returns an integer hash code for the receiver. {@code Object} which are
         * equal return the same value for this method.
         * 
         * @return the receiver's hash code.
         * @see #equals(Object)
         */
        public int hashCode();

        /**
         * Sets the value of this entry to the specified value, replacing any
         * existing value.
         * 
         * @param object
         *            the new value to set.
         * @return object the replaced value of this entry.
         */
        public V setValue(V object);
    };

    /**
     * Removes all elements from this {@code Map}, leaving it empty.
     * 
     * @throws UnsupportedOperationException
     *                if removing elements from this {@code Map} is not supported.
     * @see #isEmpty()
     * @see #size()
     */
    public void clear();

    /**
     * Returns whether this {@code Map} contains the specified key.
     * 
     * @param key
     *            the key to search for.
     * @return {@code true} if this map contains the specified key,
     *         {@code false} otherwise.
     */
    public boolean containsKey(Object key);

    /**
     * Returns whether this {@code Map} contains the specified value.
     * 
     * @param value
     *            the value to search for.
     * @return {@code true} if this map contains the specified value,
     *         {@code false} otherwise.
     */
    public boolean containsValue(Object value);

    /**
     * Returns a {@code Set} containing all of the mappings in this {@code Map}. Each mapping is
     * an instance of {@link Map.Entry}. As the {@code Set} is backed by this {@code Map},
     * changes in one will be reflected in the other.
     * 
     * @return a set of the mappings
     */
    public Set<Map.Entry<K,V>> entrySet();

    /**
     * Compares the argument to the receiver, and returns {@code true} if the
     * specified object is a {@code Map} and both {@code Map}s contain the same mappings.
     * 
     * @param object
     *            the {@code Object} to compare with this {@code Object}.
     * @return boolean {@code true} if the {@code Object} is the same as this {@code Object}
     *         {@code false} if it is different from this {@code Object}.
     * @see #hashCode()
     * @see #entrySet()
     */
    public boolean equals(Object object);

    /**
     * Returns the value of the mapping with the specified key.
     * 
     * @param key
     *            the key.
     * @return the value of the mapping with the specified key, or {@code null}
     *         if no mapping for the specified key is found.
     */
    public V get(Object key);

    /**
     * Returns an integer hash code for the receiver. {@code Object}s which are equal
     * return the same value for this method.
     * 
     * @return the receiver's hash.
     * @see #equals(Object)
     */
    public int hashCode();

    /**
     * Returns whether this map is empty.
     * 
     * @return {@code true} if this map has no elements, {@code false}
     *         otherwise.
     * @see #size()
     */
    public boolean isEmpty();

    /**
     * Returns a set of the keys contained in this {@code Map}. The {@code Set} is backed by
     * this {@code Map} so changes to one are reflected by the other. The {@code Set} does not
     * support adding.
     * 
     * @return a set of the keys.
     */
    public Set<K> keySet();

    /**
     * Maps the specified key to the specified value.
     * 
     * @param key
     *            the key.
     * @param value
     *            the value.
     * @return the value of any previous mapping with the specified key or
     *         {@code null} if there was no mapping.
     * @throws UnsupportedOperationException
     *                if adding to this {@code Map} is not supported.
     * @throws ClassCastException
     *                if the class of the key or value is inappropriate for
     *                this {@code Map}.
     * @throws IllegalArgumentException
     *                if the key or value cannot be added to this {@code Map}.
     * @throws NullPointerException
     *                if the key or value is {@code null} and this {@code Map} does
     *                not support {@code null} keys or values.
     */
    public V put(K key, V value);

    /**
     * Copies every mapping in the specified {@code Map} to this {@code Map}.
     * 
     * @param map
     *            the {@code Map} to copy mappings from.
     * @throws UnsupportedOperationException
     *                if adding to this {@code Map} is not supported.
     * @throws ClassCastException
     *                if the class of a key or a value of the specified {@code Map} is
     *                inappropriate for this {@code Map}.
     * @throws IllegalArgumentException
     *                if a key or value cannot be added to this {@code Map}.
     * @throws NullPointerException
     *                if a key or value is {@code null} and this {@code Map} does not
     *                support {@code null} keys or values.
     */
    public void putAll(Map<? extends K,? extends V> map);

    /**
     * Removes a mapping with the specified key from this {@code Map}.
     * 
     * @param key
     *            the key of the mapping to remove.
     * @return the value of the removed mapping or {@code null} if no mapping
     *         for the specified key was found.
     * @throws UnsupportedOperationException
     *                if removing from this {@code Map} is not supported.
     */
    public V remove(Object key);

    /**
     * Returns the number of mappings in this {@code Map}.
     * 
     * @return the number of mappings in this {@code Map}.
     */
    public int size();

    /**
     * Returns a {@code Collection} of the values contained in this {@code Map}. The {@code Collection}
     * is backed by this {@code Map} so changes to one are reflected by the other. The
     * {@code Collection} supports {@link Collection#remove}, {@link Collection#removeAll}, 
     * {@link Collection#retainAll}, and {@link Collection#clear} operations,
     * and it does not support {@link Collection#add} or {@link Collection#addAll} operations.
     * <p>
     * This method returns a {@code Collection} which is the subclass of
     * {@link AbstractCollection}. The {@link AbstractCollection#iterator} method of this subclass returns a
     * "wrapper object" over the iterator of this {@code Map}'s {@link #entrySet()}. The {@link AbstractCollection#size} method
     * wraps this {@code Map}'s {@link #size} method and the {@link AbstractCollection#contains} method wraps this {@code Map}'s
     * {@link #containsValue} method.
     * <p>
     * The collection is created when this method is called at first time and
     * returned in response to all subsequent calls. This method may return
     * different Collection when multiple calls to this method, since it has no
     * synchronization performed.
     * 
     * @return a collection of the values contained in this map.
     */
    public Collection<V> values();
}

 

分享到:
评论
3 楼 u011563440 2015-02-03  
u011563440 写道
用反射A.class.toGenericString()-------->public abstract static interface com.zcloud.test.utils.I$J

即:接口中的内部类是在编译期自动加上static关键字的。



-------------------

写错了,不好意思,应该是

[color=green]
public abstract static interface test.A$B
public static class test.A$AException
[/color]
2 楼 u011563440 2015-02-03  
用反射A.class.toGenericString()-------->public abstract static interface com.zcloud.test.utils.I$J

即:接口中的内部类是在编译期自动加上static关键字的。
1 楼 narutolby 2013-06-24  
因为接口本身不能实例化,所以在new 一个 接口的内部类时默认是静态的,直接用就可以,但是类是能实例化的,所以内部类需要说明是static,还是非static才能使用,我是这样理解

相关推荐

    Java 接口 内部类

    Java 接口 内部类Java 接口 内部类Java 接口 内部类

    java接口与内部类

    java接口与内部类:简单讲解了java接口与内部的类的介绍!

    java接口与内部类教学PPT

    1.接口-简介 2.接口-作用 3.接口-定义方式 4.接口-实现 5.接口-与抽象类之区别 6.接口-应用示例(设计模式...内部类-局部内部类 12.内部类-匿名内部类 13.内部类-静态内部类 14.综述

    Java接口和内部类教程

    Java接口和内部类的介绍与代码实例 public interface school { void setschoolname(); void setclassname(); void setstudentname(); void setstudentcode(); void setstudentsexy(); void setstudentbirthday(); ...

    Java__接口、内部类和Java_API基础

    Java__接口、内部类和Java_API基础 Java__接口、内部类和Java_API基础

    java接口和内部类.pdf

    java接口和内部类.pdf

    接口、内部类和Java API基础

    接口(interface)是一组常量和抽象方法的集合。接口是一种引用数据类型。 抽象方法的具体实现由实现接口的类完成,实现接口的类必须覆盖接口中的所有抽象方法。

    Java内部类(DOC)

    Java内部类Java内部类Java内部类Java内部类Java内部类Java内部类Java内部类

    java 编程思想 (多态,接口和内部类)

    java对象的引用,控制程序流,初始化和清除,封装,复用类,多态,接口和内部类,异常处理try——catch,类的检查,对象的集合,i/o系统,并发,分析与设计,对象的传递和返回

    java中的抽象类,接口和内部类 PPT

    关于java中的抽象类,接口和内部类的幻灯片,自己去试讲的哦,不错的,欢迎下载

    java中的内部类内部接口详解

    一般来说,我们创建类和接口的时候都是一个类一个文件,一个接口一个文件,但有时候为了方便或者某些特殊的原因,java并不介意在一个文件中写多个类和多个接口,这就有了我们今天要讲的内部类和内部接口。...

    java期末复习抽象类与接口,内部类,知识点.zip

    java期末复习抽象类与接口,内部类,知识点,期末考试用的,

    Java的接口、内部类、常用类

    JAVA接口、内部类、常用类 一、JAVA接口 1.1 什么是接口?: 接口相当于特殊的抽象类,定义方式、组成部分于抽象类类似。 微观概念:接口是一种能力和约定 接口的定义:代表了某种能力 方法的定义:能力的具体要求 经验:...

    03-java接口与内部类1

    第 3 章 接口与内部类接口是什么?接口有什么作用?如何使用接口?这些都是本章需要解决的问题。本章将详细地讲述接口的概念,并以实战结合的方式,学习这些抽象概念。

    java学习资料抽象类,接口,内部类

    学习java技术的基础方面的知识,有利于更加好的理解 ,抽象类,接口,内部类

    编程语言-Java语言-关于内部类的分类和认识

    Java语言中的内部类根据定义的位置和访问权限可以分为四类:成员内部类、局部内部类、匿名内部类和静态内部类。成员内部类是定义在类的内部且与类的实例相关联的类;局部内部类是定义在方法或作用域内的类;匿名内部...

    JAVA抽象类、接口和内部类(下).mp4

    JAVA抽象类、接口和内部类(下).mp4

    JAVA抽象类、接口和内部类(上).mp4

    JAVA抽象类、接口和内部类(上).mp4

    Java中的内部类

    是个很麻烦,很纠结的问题 内部类基本语法 内部类继承 子类与父类的内部类同名 匿名类 内部接口及接口中的内部类 内部类的类文件 为什么需要内部类--内部类用途

Global site tag (gtag.js) - Google Analytics