Java basic review-interface and inheritance

interface

When designing LOL, there are two types of offensive heroes, one is for physical attacks, and the other is for magic attacks.

At this time, you can use the interface to achieve this effect.

The interface is like a convention. We promise that certain heroes are physics heroes, so they must be able to carry out physical attacks (provided that they inherit the physical attack interface)

Code example:

public class Hero {//基类                                         
    public String name;
    protected float hp;
}
public interface AD {
        //物理伤害
    public void physicAttack();
}
public class ADHero extends Hero implements AD{
 	//继承了  Hero类,且实现了AD接口
    @Override
    public void physicAttack() {
        System.out.println("进行物理攻击");
    }
 
}

Implementing an interface is equivalent to committing to a certain convention

Therefore, to implement the AD interface, you must provide the method physicalAttack() declared in the AD interface to
implement the keyword implements in the syntax.

public interface AP {
 
    public void magicAttack();
}

Of course, it can also implement two interfaces, giving him two attack capabilities

//同时能进行物理和魔法伤害的英雄
public class ADAPHero extends Hero implements AD,AP{
  
    @Override
    public void magicAttack() {
        System.out.println("进行魔法攻击");
    }
  
    @Override
    public void physicAttack() {
        System.out.println("进行物理攻击");
    }
  
}

Object transformation

Generally speaking, those who don't need to be forced to transfer will definitely be able to transfer.
For forced conversion, if the method in the converted type is not in the original type, the forced conversion will fail.

Rewrite

Subclasses can inherit the object methods of the parent class

After inheritance, repeatedly providing the method is called method rewriting

Override

Polymorphism

The polymorphic
"+" of the operator can be used as an arithmetic operation or as a string concatenation

Polymorphic class
parent class to sub-class object reference (in fact, on the object of rewriting method implemented polymorphism )

hide

Similar to rewriting, method rewriting is that the subclass overrides the object method of the parent class

Hidden , that is, the subclass overrides the parent class's class method

super keyword

Instantiate an ADHero(), its construction method will be called,
its parent's construction method will also be called,
and the parent 's construction method will be called first. The
child's construction method will call the parent's no-parameter construction method by default.
You can use super explicit Invoke the construction method of the parent class with parameters

public class ADHero extends Hero implements AD{
  
    @Override
    public void physicAttack() {
        System.out.println("进行物理攻击");
    }
     
    public ADHero(String name){
        super(name);
        System.out.println("AD Hero的构造方法");
    }
     
    public static void main(String[] args) {
        new ADHero("德莱文");
    }
  
}

You can also call the moveSpeed ​​property of the parent class through super

public class ADHero extends Hero implements AD{
 
    int moveSpeed=400; //移动速度
 
    @Override
    public void physicAttack() {
        System.out.println("进行物理攻击");
    }
     
    public int getMoveSpeed(){
        return this.moveSpeed;
    }
     
    public int getMoveSpeed2(){
        return super.moveSpeed;
    }
     
    public static void main(String[] args) {
        ADHero h= new ADHero();
         
        System.out.println(h.getMoveSpeed());
        System.out.println(h.getMoveSpeed2());
         
    }
  
}

ADHero rewrites the useItem method, and calls the useItem method of the parent class through super in useItem

public class ADHero extends Hero implements AD {
 
    int moveSpeed = 400; // 移动速度
 
    @Override
    public void physicAttack() {
        System.out.println("进行物理攻击");
    }
 
    public int getMoveSpeed() {
        return this.moveSpeed;
    }
 
    public int getMoveSpeed2() {
        return super.moveSpeed;
    }
 
    // 重写useItem,并在其中调用父类的userItem方法
    public void useItem(Item i) {
        System.out.println("adhero use item");
        super.useItem(i);
    }
 
    public static void main(String[] args) {
        ADHero h = new ADHero();
 
        LifePotion lp = new LifePotion();
 
    }
 
}

Object class

Object class is the parent class of all classes

When declaring a class, the default is to inherit Object

The Object class provides a toString method, so all classes have a toString method
toString() means to return the string expression of the current object

When an object does not have any reference to it, it satisfies the conditions for garbage collection.
When it is garbage collected, its finalize() method is called.
finalize() is not a method that developers actively call, but is called by the virtual machine JVM.

equals() is used to determine whether the contents of two objects are the same

Suppose, when two heroes have the same hp, we think that the two heroes are the same

public class Hero {
    public String name;
    protected float hp;
      
    public boolean equals(Object o){
        if(o instanceof Hero){
            Hero h = (Hero) o;
            return this.hp == h.hp;
        }
        return false;
    }
      
    public static void main(String[] args) {
        Hero h1= new Hero();
        h1.hp = 300;
        Hero h2= new Hero();
        h2.hp = 400;
        Hero h3= new Hero();
        h3.hp = 300;
         
        System.out.println(h1.equals(h2));
        System.out.println(h1.equals(h3));
    }
}

"=="This is not an Object method, but it is used to determine whether two objects are the same.
More accurately, it is used to determine whether two references point to the same object.

final

When Hero is modified to final, it means that Hero cannot be inherited and
its subclasses will have compilation errors.

Hero's useItem method is modified to final, then this method cannot be overridden in ADHero

Final modifies the basic type variable, which means that the variable has only one chance of assignment

The final modified reference
h reference is modified to final, which means that the reference has only one chance to point to the object

Used to define constants:
constants refer to values ​​that can be public, directly accessed, and will not change

public static final int itemTotalNumber = 6;

Abstract class

Declare a method in the class, this method has no implementation body, it is an "empty" method

Such methods are called abstract methods, using the modifier "abstract"

After being inherited, the abstract method must be implemented.

When a class has abstract methods, the class must be declared as an abstract class

An abstract class can have no abstract methods.
Once a class is declared as an abstract class, it cannot be instantiated directly

The difference between an abstract class and an interface:
Difference 1:
A subclass can only inherit one abstract class, not multiple
subclasses can implement multiple interfaces
Difference 2: An
abstract class can define
public, protected, package, private
static and non-static
final properties And non-final attributes,
but the attributes declared in the interface can only be
public and
static
final,
even if there is no explicit declaration

Guess you like

Origin blog.csdn.net/xiaohaigary/article/details/89360912