// Atomic attack Interface Interface NuclearAttacked { // Method for handling atomic bomb attacks. The parameter is the x and y coordinates of the serving point. Public function NuclearAttacked ($ x, $ y ); } // The base of the person family to implement the interface attacked by the atomic bomb. Other content is not considered for the moment. Class CommandCenter implements NuclearAttacked { // Method for handling atomic bomb attacks. The parameter is the x and y coordinates of the serving point. Public function NuclearAttacked ($ x, $ y) { // According to the distance from the Atomic Bomb Center, the reduced blood is defined. If the remaining blood is exceeded, it will blow up. } } // The cruiser (also known as Dahe) implements an interface attacked by an atomic bomb. Other content is not considered for the moment. Class Battlecruiser implements NuclearAttacked { // Method for handling atomic bomb attacks. The parameter is the x and y coordinates of the serving point. Public function NuclearAttacked ($ x, $ y) { // According to the distance from the Atomic Bomb Center, the reduced blood is defined. If the remaining blood is exceeded, it will blow up. } } // Atomic bomb Class Nuclear { // Targets attacked by atomic bombs Public $ attackedThings; // Add the object attacked by the atomic bomb Public function addAttackedThings ($ thing) { // Add the object attacked by the atomic bomb $ This-> attackedThings [] = $ thing; } // Atomic bomb explosion method. The parameter is the x and y coordinates of the serving point. Public function blast ($ x, $ y) { // Hand over the explosion to all involved objects and let them handle it by themselves Foreach ($ this-> attackedThings as $ thing) { // Hand over the explosion to all involved objects and let them handle it by themselves $ Thing-> NuclearAttacked ($ x, $ y ); } } } // Create a Base Object $ CommandCenter = new CommandCenter (); // Create a new cruiser object $ Battlecruiser = new Battlecruiser (); // Creates an atomic bomb. $ Nuclear2 = new Nuclear (); // Assume that the launch is successful. At that moment, a base object and a cruiser object are in the killing range. $ Nuclear2-> addAttackedThings ($ CommandCenter ); $ Nuclear2-> addAttackedThings ($ Battlecruiser ); // Atomic bomb explosion. In this way, the event is handed over to the Processing Methods of the objects involved. Assume that the x and y coordinates of the serving point are 2353,368. $ Nuclear2-> blast (2353,368 ); ?> |