class jiaotongdeng{}
class Red extends jiaotongdeng{}
class Yellow extends jiaotongdeng{}
class Green extends jiaotongdeng{}
public class lianxi{
public static void draw(jiaotongdeng G)
{
if(G instanceof Red)
{
System.out.println("红灯亮45秒");
}
if(G instanceof Yellow)
{
System.out.println("黄亮5秒");
}
if(G instanceof Green)
{
System.out.println("绿亮30秒");
}
}
public static void main(String[] args)
{
draw(new Red());
draw(new Yellow());
draw(new Green());
}
}
该Java程序定义了三个类Red、Yellow和Green,它们都继承自基类jiaotongdeng。在lianxi类的draw方法中,根据传入的对象类型执行不同的灯亮时长逻辑。在main方法中,分别调用draw方法展示红、黄、绿灯的亮灯时间。
1150

被折叠的 条评论
为什么被折叠?



