6.接口与内部类

6.1 接口

接口不是类,而是对类的一组需求描述。


接口中所有方法自动属于public 。因此在声明时,不必提供关键字public

在接口中,可以定义常量。


浮点数比较大小时,可能因舍入产生 错误。



在接口中, 似乎能定义静态方法?

接口中的常量被自动设为public static final。  此时的静态方法就没有意义。


6.2 对象克隆


Object 类中的clone() 是 浅拷贝


Cloneable接口


所有数组都有一个clone()方法



6.4 内部类

内部类对象有一个隐式引用,它引用了实例化该内部对象的外围类对象。


内部类-局部内部类- 匿名内部类-静态内部类(只为隐藏,不需引用外部类对象)


public void start(int interval, final boolean beep)//<span style="font-family: Arial, Helvetica, sans-serif;">final</span>
<span style="white-space:pre">	</span>class TimePrinter implements ActionListener
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>public void actionPerformed(ActionEvent event)
<span style="white-space:pre">		</span>{ <span style="white-space:pre">	</span>Date now = new Date();
<span style="white-space:pre">			</span>System.out.println("At the tone, the time is " + now);
<span style="white-space:pre">			</span>if (beep) Toolkit.getDefaultToolkit().beep();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>ActionListener listener = new TimePrinter();
<span style="white-space:pre">	</span>Timer t = new Timer(interval, listener);
<span style="white-space:pre">	</span>t.start();
}

class TalkingClock$1TimePrinter
{
<span style="white-space:pre">	</span>TalkingClock$1TimePrinter(TalkingClock, boolean);
<span style="white-space:pre">	</span>public void actionPerformed(java.awt.event.ActionEvent);
<span style="white-space:pre">	</span>final boolean val$beep;
<span style="white-space:pre">	</span>final TalkingClock this$0;
}



ArrayList<String> friends = new ArrayList<>();
favorites.add("Harry");
favorites.add("Tony");
invite(friends);

invite(new ArrayList<String>() {{ add("Harry"); add("Tony"); }})


获得Object匿名子类的外部类 。可得到静态方法所在类
new Object(){}.getClass().getEnclosingClass() // gets class of static method



声明在 接口中的内部类自动成为static和public类



6.5 代理

在运行时创建一个实现了一组给定接口的新类

class TraceHandler implements InvocationHandler
{
<span style="white-space:pre">	</span>private Object target;
<span style="white-space:pre">	</span>public TraceHandler(Object t)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>target = t;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public Object invoke(Object proxy, Method m, Object[] args)
<span style="white-space:pre">	</span>throws Throwable
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>// print method name and parameters
<span style="white-space:pre">		</span>// invoke actual method
<span style="white-space:pre">		</span>return m.invoke(target, args);
<span style="white-space:pre">	</span>}
}

Object value = . . .;
// construct wrapper
InvocationHandler handler = new TraceHandler(value);
// construct proxy for one or more interfaces
Class[] interfaces = new Class[] { Comparable.class};
Object proxy = Proxy.newProxyInstance(null, interfaces, handler);


代理类覆盖了Object类中的方法toString,equals和hashCode,如同代理方法一样,这些方法仅仅调用了调用处理器的invoke



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值