This chapter introduces the basic concept of reference, inheritance and method overloading. By learning the basic concepts of reference, inheritance and method overloading, you can finally complete the simple code design of physically attacking heroes and healing heroes in the LOL hero game.
1. References
The concept of reference is defined as if the type of a variable is a class type rather than a basic type, then the variable is also called a reference.
- Reference and pointing

Represents a Hero object created.
But it just created an object, there is no way to access it.
In order to access this object, a reference will be used to represent this object.

h this variable is of type Hero, also called reference.
= Means h This reference represents the object created on the right.
“Representation” is also called “pointing” in object-oriented.
2. Inherit
In LOL, a weapon is a kind of item with a name and price.
So, when designing the class, you can let the weapon inherit the item, thereby inheriting the name and price attributes.
- Item
The Item class has name and price attributes.
The specific code is as follows:
public class Item {
String name;
int price;
}

- Weapon (not inherited)
Weapon: Weapon does not inherit the wording of Item.
Independent design of name and price attributes.
At the same time there is an additional attribute damage.
The specific code is as follows:
public class Weapon{
String name;
int price;
int damage; //Attack power
}

Weapon class Weapon (inherited class Item)
This time Weapon inherits Item.
Although Weapon did not design the name and price, it also has the name and price attributes by inheriting the Item class.
The specific code is as follows:
package property;
public class Weapon extends ltem{
int damage; //Attack power
public static void main(String[] args) {
Weapon infinityEdge = new Weapon();
infinityEdge.damage = 50; //The damage attribute is newly designed in the Weapon class
infinityEdge.name = “Endless Blade”;//The name attribute is inherited from Item, so there is no need to repeat the design
infinityEdge.price = 2600;
}
}

- Exercise 1-Armor
Design a class Armor
Inherit the Item class, and provide an additional attribute ac: armor level is int type
Instantiate two pieces of armor
Name Price Armor Level
Cloth armor 200 10
Chain mail 400 35
The specific code is as follows:
package property;
public class Armor extends ltem{
int ac; //armor level
public static void main(String[] args) {
Armor cloth = new Armor();
cloth.name=”Cloth armor”;
cloth.price=200;
cloth.ac = 10;
Armor chainMail = new Armor();
chainMail.name=”Chain mail”;
chainMail.price=400;
chainMail.ac = 35;
}
}

3. Method Overloading
Method overloading means that the method name is the same, but the parameter types are different.
- Overload of attack method
There is a kind of hero called the physical attack hero AD Hero, AD Hero is provided with three methods.
The specific code is as follows:
public void attack ()
public void attack (Hero h1)
public void attack (Hero h1, Hero h2)
It can be seen that the method name is the same, but the parameter types are different.
When calling the method attack, the corresponding method will be automatically called according to the type and number of parameters passed.
- Exercise 2-attack
Through programming, the final output of the console is that Timo attacked Anne and Timo attacked Annie and Ashe at the same time.
The specific code is as follows:
package property;
public class ADHero extends Hero {
public void attack() {
System.out.println(name + ” made an attack, but not sure who was hit”);
}
public void attack(Hero h1) {
System.out.println(name + ” made an attack on “+h1.name);
}
public void attack(Hero h1, Hero h2) {
System.out.println(name + ” made an attack on ” + h1.name + “and” + h2.name);
}
public static void main(String[] args) {
ADHero tm = new ADHero();
tm.name = “timo”;
Hero h1 = new Hero();
h1.name = “Annie”;
Hero h2 = new Hero();
h2.name = “Ashe”;
tm.attack(h1);
tm.attack(h1, h2);
}
}

The final console output:

Variable number of parameters
If you want to attack more heroes, you need to devise more methods.
The previously designed attack method is too cumbersome, now I introduce a relatively simple attack method design.
Only need to design a method:
public void attack (Hero …heroes)
This method can represent all the above methods.
In the method, use an array to process the parameter heroes.
The specific code is as follows:
package property;
public class ADHero extends Hero {
public void attack() {
System.out.println(name + ” made an attack, but not sure who was hit”);
}
//Variable number of parameters
public void attack(Hero… heroes) {
for (int i = 0; i < heroes.length; i++) {
System.out.println(name + ” made an attack on ” + heroes[i].name);
}
}
public static void main(String[] args) {
ADHero tm = new ADHero();
tm.name = “timo”;
Hero h1 = new Hero();
h1.name = “Annie”;
Hero h2 = new Hero();
h2.name = “Ashe”;
tm.attack(h1);
tm.attack(h1, h2);
}
}

The final console output:

- Exercise 3-treatment
Design a class Support (auxiliary hero) to inherit Hero and provide a “heal” (treatment) method
Overload the heal method of Support heal ()
heal (Hero h) //Add blood to the specified hero
heal (Hero h, int hp) //Add hp to the specified hero
The specific code is as follows:
package property;
public class Support extends Hero {
public void heal() {
System.out.println(“treat myself”);
}
public void heal(Hero hero) {
System.out.println(name+”add blood to the hero:”+hero.name);
}
public void heal(Hero hero,int hp) {
System.out.println(name+”add blood to the hero:”+hero.name+” and add the hp:”+hero.hp);
}
public static void main(String[] args) {
Support h1=new Support();
h1.name=”Soraka”;
h1.heal();
Hero h2=new Hero();
h2.name=”Annie”;
h1.heal(h2);
Hero h3=new Hero();
h3.name=”Ashe”;
h3.hp=100;
h1.heal(h3,h3.hp);
}
}

The final console output:

Today’s tutorial is over, I hope people who watch this tutorial can do it by themselves and type the code line by line. By studying the concepts of reference, inheritance, and method overloading,
You can design physical attack heroes and healing heroes by yourself.
Best view i have ever seen !