1、新建一个java类,
package reusing;<span style="color:#808080;">/*
<span style="color:#808080;"> Created by caoxiaohong on 16/11/20.
<span style="color:#808080;"> 测试idea可以手动选择生成代理
<span style="color:#808080;">
<span style="color:#808080;"> */
<span style="color:#000080;">public class SpaceShipDelegation {}
2、在要被代理的类,如下:
class SpaceShipControls{ void up(int i){} void down(int i){} void left(int i){} void right(int i){} void forward(int i){} void back(int i){} }
3、在类
public class SpaceShipDelegation { private SpaceShipControls controls=new SpaceShipControls(); }
显示如下:
选择Generate,后显示如下:
原文链接:https://www.f2er.com/idea/67288.html选择:Delegate Methods,然后显示如下:
选择ok,显示如下:
选择ok,显示如下:
这样就自动生成了java类的代理,也完成了idea自动生成java代理的过程。
sum:整体代码如下:
<pre style="font-family:Menlo;font-size:9pt;"><span style="color:#000080;">package reusing;
<span style="color:#808080;">/*
<span style="color:#808080;"> Created by caoxiaohong on 16/11/20.
<span style="color:#808080;"> 测试idea可以手动选择生成代理
<span style="color:#808080;">
<span style="color:#808080;"> */
<span style="color:#808080;">
<span style="color:#000080;">class SpaceShipControls{
<span style="color:#000080;">void up(<span style="color:#000080;">int i){}
<span style="color:#000080;">void down(<span style="color:#000080;">int i){}
<span style="color:#000080;">void left(<span style="color:#000080;">int i){}
<span style="color:#000080;">void right(<span style="color:#000080;">int i){}
<span style="color:#000080;">void forward(<span style="color:#000080;">int i){}
<span style="color:#000080;">void back(<span style="color:#000080;">int i){}
}
<span style="color:#000080;">public class SpaceShipDelegation {<span style="color:#000080;"><strong>private </strong></span>SpaceShipControls <span style="color:#660e7a;"><strong>controls</strong></span>=<span style="color:#000080;"><strong>new </strong></span>SpaceShipControls(); <span style="color:#000080;"><strong>public void </strong></span>up(<span style="color:#000080;"><strong>int </strong></span>i) { <span style="color:#660e7a;"><strong>controls</strong></span>.up(i); } <span style="color:#000080;"><strong>public void </strong></span>right(<span style="color:#000080;"><strong>int </strong></span>i) { <span style="color:#660e7a;"><strong>controls</strong></span>.right(i); } <span style="color:#000080;"><strong>public void </strong></span>back(<span style="color:#000080;"><strong>int </strong></span>i) { <span style="color:#660e7a;"><strong>controls</strong></span>.back(i); } <span style="color:#000080;"><strong>public void </strong></span>down(<span style="color:#000080;"><strong>int </strong></span>i) { <span style="color:#660e7a;"><strong>controls</strong></span>.down(i); } <span style="color:#000080;"><strong>public void </strong></span>forward(<span style="color:#000080;"><strong>int </strong></span>i) { <span style="color:#660e7a;"><strong>controls</strong></span>.forward(i); } <span style="color:#000080;"><strong>public void </strong></span>left(<span style="color:#000080;"><strong>int </strong></span>i) { <span style="color:#660e7a;"><strong>controls</strong></span>.left(i); }
<span style="color:#808080;">/public static void main(String[] args){
<span style="color:#808080;">
<span style="color:#808080;"> }/
}