Home > Back-end >  In the main method, respectively of parent and child classes object is stored in a Vehicle types of
In the main method, respectively of parent and child classes object is stored in a Vehicle types of

Time:09-29

Topic is such a parent in the custom, custom a shine in the parent class method, then defines three subclasses inherit and rewrite the shine of the parent class method, in the main method, respectively of parent and child classes object is stored in a Vehicle types in the array, use an array of each object call shine method, the output traffic lights shine, yellow light red light, red light green light, red glowing red glowing now that I've rewritten good just don't know how in the main method, respectively of parent and child classes object is stored in a Vehicle types in the array, use an array of each object call shine method, output

The class TrafficLights
{
Void shine ()
{
System. The out. Println (" traffic lights shine ");
}
}
The class YellowTrafficLight extends TrafficLights
{
Void shine ()
{
System. The out. Println (" yellow light glows red ");
}
}
The class GreenTrafficLight extends TrafficLights
{
Void shine ()
{
System. The out. Println (" green light glows red ");
}
}
The class RedTrafficLight extends TrafficLights
{
Void shine ()
{
System. The out. Println (" the red light glows red ");
}
}

CodePudding user response:

The class TrafficLights
{
Void shine ()
{
System. The out. Println (" traffic lights shine ");
}
}
The class YellowTrafficLight extends TrafficLights
{
Void shine ()
{
System. The out. Println (" yellow light glows red ");
}
}
The class GreenTrafficLight extends TrafficLights
{
Void shine ()
{
System. The out. Println (" green light glows red ");
}
}
The class RedTrafficLight extends TrafficLights
{
Void shine ()
{
System. The out. Println (" the red light glows red ");
}
Public static void main (String [] args)
{
TrafficLights traffic=new TrafficLights ();
YellowTrafficLight Yellow=new YellowTrafficLight ();
GreenTrafficLight Green=new GreenTrafficLight ();
RedTrafficLight Red=new RedTrafficLight ();
String Vehicle []={" traffic ", "Yellow", "Green", "Red"};
System. The out. Println (" traffic lights: ");
Traffic. Shine ();
System. The out. Println (" traffic lights: ");
Yellow. Shine ();
System. The out. Println (" traffic lights: ");
Red. Shine ();
System. The out. Println (" traffic lights: ");
Green. Shine ();
}
}
Thanks to this topic later to the bosses can see optimize code yes thank you

CodePudding user response:

TrafficLights [] array={new TrafficLights (), new YellowTrafficLight (), new GreenTrafficLight (), new RedTrafficLight ()}

CodePudding user response:

Ok thank you
  • Related