听同学说,盗梦空间的情节就像类中的继承关系,于是我仔细想了想,果然不错,于是写下了这些代码,分享一下:
//LayerOne
package com.xysnake.test;
public class LayerOne {
public LayerOne(){
DreamLayer();
}
private void DreamLayer()
{
System.out.println("This is the first Layer!");
}
}
//LayerTwo
package com.xysnake.test;
public class LayerTwo extends LayerOne {
public LayerTwo() {
super();
DreamLayer();
}
private void DreamLayer()
{
System.out.println("This is the Second Layer!");
}
}
//LayerThird
package com.xysnake.test;
public class LayerThird extends LayerTwo{
public LayerThird() {
super();
DreamLayer();
}
private void DreamLayer()
{
System.out.println("This is the Third Layer!");
}
}
//LayerForth
package com.xysnake.test;
public class LayerForth extends LayerThird{
public LayerForth() {
super();
DreamLayer();
}
private void DreamLayer()
{
System.out.print("This is the Forth Layer!");
}
}
//test
package com.xysnake.test;
public class test {
public static void main(String[] args) {
// LayerOne myDreamOne=new LayerOne();
//etc ...Two Three Forth
// LayerTwo myDreamTwo=new LayerTwo();
// LayerThird myDreamLayerThird=new LayerThird();
LayerForth myDreamForth=new LayerForth();
}
}
运行结果: