Tool version compatibility problem
In designing LOL When I was , There are two kinds of offensive heroes , One is to attack the Physics Department , One is to attack the magic department
Correct
At this time , You can use Interface To achieve this effect . An interface is like a convention , We agreed that some heroes were physics heroes , Then they must be able to make physical attacks .
This video is interpretive , So I hope you have read the content of this knowledge point , And after writing the corresponding code , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning
![]() 8 branch 47 second This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers Step 1 : Physical attack interface Step 2 : Design a kind of hero , Able to use physical attacks Step 3 : Magic attack interface Step 4 : Design a kind of hero , You can only use magic attacks Step 5 : Design a kind of hero , Both physical attacks , Can make magic attacks again Step 6 : Under what circumstances should the interface be used ? Step 7 : practice - Interface Step 8 : answer - Interface
Create an interface File->New->Interface
AD , Declare a method physicAttack Physical attack , But there is no method body , It's a “ empty ” method
package charactor; public interface AD { // Physical damage public void physicAttack(); }
package charactor; public interface AD { // Physical damage public void physicAttack(); }
Design a kind of hero , Able to use physical attacks , Such heroes are LOL It is called AD
class :ADHero Inherited Hero class , So I inherited name,hp,armor And so on Implement an interface , It's equivalent to committing to some kind of agreement So , realization Yes AD This interface , Just must Provide AD Methods declared in the interface physicAttack() realization Use keywords in Syntax implements
package charactor; public class ADHero extends Hero implements AD{ @Override public void physicAttack() { System.out.println(" Make a physical attack "); } }
package charactor; public class ADHero extends Hero implements AD{ @Override public void physicAttack() { System.out.println(" Make a physical attack "); } }
Create an interface File->New->Interface
AP , Declare a method magicAttack Magic attack , But there is no method body , It's a “ empty ” method
package charactor; public interface AP { public void magicAttack(); }
package charactor; public interface AP { public void magicAttack(); }
Design a kind of hero , You can only use magic attacks , Such heroes are LOL It is called AP
class :APHero Inherited Hero class , So I inherited name,hp,armor And so on At the same time , Realized AP This interface , Just must Provide AP Methods declared in the interface magicAttack() realization Use keywords in Syntax implements
package charactor; public class APHero extends Hero implements AP{ @Override public void magicAttack() { System.out.println(" Make a magic attack "); } }
package charactor; public class APHero extends Hero implements AP{ @Override public void magicAttack() { System.out.println(" Make a magic attack "); } }
A hero , Capable of both physical and magic attacks
Like ezrell , Picturesque policewoman Caitlin
package charactor; // Heroes who can do both physical and magic damage public class ADAPHero extends Hero implements AD,AP{ @Override public void magicAttack() { System.out.println(" Make a magic attack "); } @Override public void physicAttack() { System.out.println(" Make a physical attack "); } }
package charactor; // Heroes who can do both physical and magic damage public class ADAPHero extends Hero implements AD,AP{ @Override public void magicAttack() { System.out.println(" Make a magic attack "); } @Override public void physicAttack() { System.out.println(" Make a physical attack "); } }
The above example , It seems that the interface , Don't interface , All the same , So what is the meaning of the interface
Learn a knowledge point , It is carried out from shallow to deep . Here , It just introduces the concept of interface , To really understand the benefits of interfaces , More practice is needed , And after a large number of applications in more complex systems , To really understand , For example, I'm studying Polymorphism Then we can further deepen our understanding . Just touched on a concept , I hope to achieve the perfect learning effect , Such a learning goal is unscientific .
Design a healer interface :Healer
The interface declares a method : heal() Design a Support class , On behalf of auxiliary Heroes , Inherit Hero class , At the same time Healer This interface
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Viewing this answer will cost 2 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Base total 0 One answer ( Total required 0 Integral )
Viewing this answer will cost 2 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Base total 0 One answer ( Total required 0 Integral )
Account not activated
Account not activated , Limited functionality . Please click activate
This video is interpretive , So I hope you have read the content of this answer , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning
![]() 44 second This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers
public interface Healer { public void heal(); // Add blood }
public interface Healer { public void heal(); // Add blood }
public class Support extends Hero implements Healer{ @Override public void heal() { System.out.println(name+" Add a mouthful of blood "); } }
public class Support extends Hero implements Healer{ @Override public void heal() { System.out.println(name+" Add a mouthful of blood "); } }
The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .
![]()
Q & A area
2021-07-30
@override What do you mean? ?
1 One answer
bird Jump to the problem location Answer time :2021-07-30
When a method has @override Indicates that the method is a method that overrides the parent class
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-07-09
2021-7-9-PM-2-30
3 One answer
wxd123 Jump to the problem location Answer time :2021-07-22 Li Ge qaq Jump to the problem location Answer time :2021-07-13 Daydream home Jump to the problem location Answer time :2021-07-09
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-04-20
answer
2021-03-23
Describe the function of the interface by comparing the class with the interface
2021-02-01
hero After a class of single cases adhero If you inherit, you will report an error
Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 48 Previous questions , please Click to view
Please... Before asking questions land
The question has been submitted successfully , Auditing . Please
My question Check the question record at , thank you
|