【Core Java】【Volume I ~Fundamentals】Chapter 6 INTERFACES AND INNER CLASSES

本文深入探讨Java中接口与内部类的概念和技术细节,包括接口如何定义类的行为规范,内部类的特点及其应用场景,如实现GUI事件处理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chapter  6

INTERFACES AND  INNER CLASSES

▼ INTERFACES
▼ OBJECT CLONING
▼ INTERFACES AND CALLBACKS
▼ INNER CLASSES
▼ PROXIES

You have now seen all the basic tools for object-oriented programming in Java.
This chapter shows you several advanced techniques that are commonly used. Despite
their less obvious nature, you will need to master them to complete your Java tool chest.
The first, called an interface, is a way of describing what classes should do, without specifying
how they should do it. A class can implement one or more interfaces. You can then
use objects of these implementing classes anytime that conformance to the interface is
required. After we cover interfaces, we take up cloning an object (or deep copying, as it
is sometimes called). A clone of an object is a new object that has the same state as the
original. In particular, you can modify the clone without affecting the original.
Next, we move on to the mechanism of inner classes. Inner classes are technically somewhat
complex—they are defined inside other classes, and their methods can access the
fields of the surrounding class. Inner classes are useful when you design collections of
cooperating classes. In particular, inner classes enable you to write concise, professional-looking
code to handle GUI events.
This chapter concludes with a discussion of proxies, objects that implement arbitrary
interfaces. A proxy is a very specialized construct that is useful for building systemlevel
tools. You can safely skip that section on first reading.

Interfaces

  In the Java programming language, an interface is not a class but a set of requirements for
classes that want to conform to the interface.
Typically, the supplier of some service states: “If your class conforms to a particular
interface, then I’ll perform the service.” Let’s look at a concrete example. The sort
method of the Arrays class promises to sort an array of objects, but under one condition:
The objects must belong to classes that implement the Comparable interface.
Here is what the Comparable interface looks like:
public interface Comparable
{
  int compareTo(Object other);
}
This means that any class that implements the Comparable interface is required to have a
compareTo method, and the method must take an Object parameter and return an integer.

转载于:https://www.cnblogs.com/yqskj/articles/2053754.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值