- 浏览: 285981 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
-
u011563440:
<div class="quote_title ...
Java接口中的内部接口和接口中的内部类 -
u011563440:
用反射A.class.toGenericString()--- ...
Java接口中的内部接口和接口中的内部类 -
narutolby:
因为接口本身不能实例化,所以在new 一个 接口的内部类时默认 ...
Java接口中的内部接口和接口中的内部类 -
hekuilove:
请问楼主,第四部如何做?在eclipse哪个位置?
eclipse中使用maven插件的问题:Updating index central|http://repo1.maven.org/maven2 -
小小生:
请问下,如果修改了数据库里面的订单任务的时间,那么不是要重启? ...
利用Spring动态对Quartz定时任务管理
我们常用的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关键字的。
即:接口中的内部类是在编译期自动加上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关键字的。
即:接口中的内部类是在编译期自动加上static关键字的。
1 楼
narutolby
2013-06-24
因为接口本身不能实例化,所以在new 一个 接口的内部类时默认是静态的,直接用就可以,但是类是能实例化的,所以内部类需要说明是static,还是非static才能使用,我是这样理解
发表评论
-
POJO与DTO的区别
2015-01-08 15:34 1678ational Mapping(对象关系映射)的缩写。通俗点 ... -
Java Security
2013-12-09 11:04 0Key http://blog.csdn.net/turu ... -
c#与java对照
2012-04-16 17:12 1333c#基本类型转换:System.c ... -
java动态代理
2012-02-21 14:31 1218转:http://blog.csdn.net/driver ... -
Java代码查询网站
2012-01-03 23:39 1256这些是经常使用的一些Java代码查询网站. 1. java ... -
Java泛型
2011-11-29 10:58 2436Java泛型由来的动机 理解Java泛型最简单的方法是把它看 ... -
Java synchronized用法
2011-11-28 17:20 1455在Java中,synchronized关键 ... -
java成员变量和方法的public等访问限定符的访问权限
2011-08-22 10:04 1639在学习java的过程中,我写类经常会用到public,priv ... -
Java中堆、栈、常量池等概念解析
2011-08-19 12:01 1796程序运行时,我们最好对数据保存到什么地方做到心中有数。特别 ... -
java中String类详解(享元模式思考)
2011-08-19 11:48 1564转:http://blog.csdn.net/huangbia ... -
面向对对象思维方法
2011-08-18 12:34 10411,首先确定谁来做,其次确定怎么做. 2.首先考虑整体,其次 ... -
JAVA基础-栈与堆,static、final修饰符、内部类和Java内存分配
2011-08-18 12:09 5062Java栈与堆 堆:顺序随意 栈:后进先出(Last-i ... -
软件开发的好网址
2010-05-20 14:08 1324Sun公司的J2EE中文API: http://do ... -
serialVersionUID的作用
2010-05-20 14:02 1338本文来自CSDN博客,转载 ... -
深入探索 高效的Java异常处理框架-转
2010-05-20 12:19 1286版权声明:本作品已刊登在《IT实验室周报》第04-05期第04 ... -
设计异常管理系统
2010-05-20 12:18 15202009-06-30 作者:Jean-Pierre Norgu ...
相关推荐
### Java接口和内部类教程 #### 3.1 接口 ##### 3.1.1 接口概念的引入 在计算机编程中,接口扮演着至关重要的角色,尤其是在Java这样的面向对象编程语言中。接口可以被视为一种规范或合同,规定了类必须遵循的...
Java接口和内部类是Java语言中两种重要的抽象机制,它们在软件设计中起着关键作用。接口主要用于定义对象之间的交互规范,而内部类则提供了一种封装和隐藏类的机制,使得类的设计更加灵活。 首先,接口是一个合约,...
### Java接口与内部类知识点详解 #### 接口概述 - **接口定义**: 在Java中,接口(Interface)是一种抽象类型,它定义了一组方法签名(即方法的名称和参数列表),但没有具体实现(即没有方法体)。接口提供了一种...
1.接口-简介 2.接口-作用 3.接口-定义方式 4.接口-实现 5.接口-与抽象类之区别 6.接口-应用示例(设计模式...内部类-局部内部类 12.内部类-匿名内部类 13.内部类-静态内部类 14.综述
"Java接口、内部类和Java_API基础" Java接口是Java语言中的一个抽象概念,它定义了一组方法的集合,但不提供实现。Java接口的主要特点是它不能被实例化,必须由其他类来实现。接口的声明格式为: ``` public ...
java接口和内部类.pdf
接口(interface)是一组常量和抽象方法的集合。接口是一种引用数据类型。 抽象方法的具体实现由实现接口的类完成,实现接口的类必须覆盖接口中的所有抽象方法。
总结来说,接口和内部类在Java中分别扮演着定义行为规范和增强封装性的角色。接口提供了多继承的解决方案,让代码更加模块化和可扩展;内部类则提供了更灵活的类结构,便于处理复杂的设计需求。理解并熟练运用这些...
复习时,可以详细阅读`Java抽象类.docx`来理解抽象类的定义、用途和实例化规则,通过`Java接口.docx`学习接口的定义、实现方式以及多继承的特点,`Java内部类.docx`将帮助你掌握不同类型的内部类及其应用场景。...
总结一下,内部类、匿名内部类和内部接口在Java编程中提供了一种强大的工具,它们可以帮助我们更好地组织代码,实现封装和模块化。在Android开发中,它们常用于事件监听、线程管理、回调函数等场景,增强了代码的...
Java接口在Java编程语言中扮演着至关重要的角色,它是实现多态性和抽象化的关键机制。接口定义了一组方法签名,但不提供具体实现,这使得类可以实现多个接口,从而表现出多种行为。以下是对Java接口的详细解释: 一...
在编程世界中,接口、内部类和抽象类是面向对象设计中的重要概念,它们各自扮演着不同的角色,帮助我们构建可扩展和灵活的代码结构。接下来,我们将详细探讨这三个概念及其区别。 首先,我们来理解**接口...
在Java编程语言中,抽象类、接口和内部类是三个重要的概念,它们分别用于不同的设计需求。下面将详细解释这三个概念及其使用。 1. 抽象类(Abstract Class) 抽象类是不能直接实例化的类,它包含至少一个抽象方法。...
集合框架包括接口、类和算法,这些都集中在`java.util`包中。主要的集合接口有List、Set、Queue和Map,它们各自代表了不同的数据组织方式。 List接口是有序的集合,允许重复元素,如ArrayList和LinkedList。...
在这个Java学习资料中,我们重点探讨抽象类、接口和内部类。 首先,抽象类是一种不能实例化的类,它主要用于被其他类继承。在Java中,使用`abstract`关键字来定义一个抽象类。抽象类可以包含抽象方法(无方法体的...
"2022年接口内部类和JavaAPI基础" 一、接口 1.1 声明接口 ...本节课我们学习了Java中的接口、内部类和内部接口,了解了它们的声明、实现和使用方法,以及Java API基础的包中的基础类库和工具类库。
5. **内部接口及接口中的内部类** - 接口也可以包含内部类,但这并不常见,因为接口主要用于定义行为,而不是封装数据或逻辑。但是,如果需要在接口中定义一个相关的辅助类,这是可行的。 6. **内部类的类文件** ...
总之,Java接口是软件设计的重要工具,它定义了类的行为规范,支持多继承,提供了功能强大的默认方法和函数式接口。理解并熟练运用接口,能让你的Java代码更加优雅、可扩展和易于维护。在实际编程中,根据需求选择...
Java中的抽象类(abstract class)和接口(interface)都是用于创建抽象化模型的重要工具,它们在面向对象编程中扮演着核心角色。这两种机制都允许我们定义一组方法的签名,但不提供具体实现,从而实现“设计契约”...
创建对象的过程称为实例化,使用关键字`new`和类的构造器完成。对象间的交互通过方法调用实现,这是面向对象编程中的消息传递。 4. 图形界面(GUI): Java提供了丰富的图形用户界面(GUI)工具包,如Java Swing和...