接口,数组,方法调用关系
2018-05-24 21:51:42 0 举报
接口,数组,方法调用关系
作者其他创作
大纲/内容
3
验证
1
满足条件继续执行setLight(ls[1])
false
结束
进入for循环
package _2018年5月17日_8;interface Light {\tvoid shine();}class RedLight implements Light {\tpublic void shine() {\t\tSystem.out.println(\"Red Light shine in Red\");\t}}class YellowLight implements Light {\tpublic void shine() {\t\tSystem.out.println(\"Yellow Light shine in Yellow\");\t}}class GreenLight implements Light {\tpublic void shine() {\t\tSystem.out.println(\"Green Light shine in Green\");\t}}class Lamp {\tprivate Light light;\tpublic void setLight(Light light) {\t\tthis.light = light;\t}\tpublic void on() {\t\tlight.shine();\t}}public class TestLamp {\tpublic static void main(String args[]) {\t\tLight[] ls = new Light[3];\t\tls[0] = new RedLight();\t\tls[1] = new YellowLight();\t\tls[2] = new GreenLight();\t\tLamp lamp = new Lamp();\t\tfor (int i = 0; i ls.length; i++) {\t\t\tlamp.setLight(ls[i]);\t\t\tlamp.on();\t\t}\t}}
i ls.length;
开始
true
之后i ls.length;判定失效退出循环.程序结束
lamp.setLight(ls[i]);
int i = 0;
2
满足条件继续执行setLight(ls[2])
4
lamp.on();
6
i++;
5
数组也是对象
退出for循环
最终输出
0 条评论
下一页