inotgo.com


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

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 .
Correct

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
 Physical attack interface
package charactor; public interface AD { // Physical damage public void physicAttack(); }
package charactor;

public interface AD {
        // Physical damage 
	public void physicAttack();
}
Step 2 :

Design a kind of hero , Able to use physical attacks

edit top fracture
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();
}
Step 4 :

Design a kind of hero , You can only use magic attacks

edit top fracture
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 ");
	}

}
Step 5 :

Design a kind of hero , Both physical attacks , Can make magic attacks again

edit top fracture
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 ");
    }
 
}
Step 6 :

Under what circumstances should the interface be used ?

edit top fracture
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 .
Step 7 :

practice - Interface

edit top fracture   Wrong posture , get half the results with double the effort ! Click to see the correct posture for the exercise
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? ?
Lin Hai

@override What do you mean? ?




1 One answer

bird
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
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2021-07-09 2021-7-9-PM-2-30
Daydream home




2021-7-9-PM-2-30
package charactor;

public class Support extends Hero implements Healer {
    public void heal(Hero hero){
        System.out.println(hero.name + " Start healing teammates ");
    }

    public static void main(String[] args) {
        Support support = new Support();
        support.name = " Cai Wenji ";
        support.heal(support);
    }
}

package charactor;

public class Hero {
    String name;
    float hp;
    float armor;
    int movespeed;
}

package charactor;

public interface Healer {
    public void heal(Hero hero);
}
 
               


3 One answer

wxd123
Answer time :2021-07-22
Li Ge qaq
Answer time :2021-07-13
Daydream home
Answer time :2021-07-09



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





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
about JAVA Basics - Interface and inheritance - Interface Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot