Java练习之方法重载

Java练习之方法重载

方法的重载指的是方法名一样,但是参数类型不一样。

0x1 方法重载

有一种英雄,叫做物理攻击英雄 ADHero
为ADHero 提供三种方法

public void attack()
public void attack(Hero h1)
public void attack(Hero h1, Hero h2)

方法名是一样的,但是参数类型不一样
在调用方法attack的时候,会根据传递的参数类型以及数量,自动调用对应的方法

public class ADHero extends Hero {
    public void attack() {
        System.out.println(name + " 进行了一次攻击 ,但是不确定打中谁了");
    }

    public void attack(Hero h1) {
        System.out.println(name + "对" + h1.name + "进行了一次攻击 ");
    }

    public void attack(Hero h1, Hero h2) {
        System.out.println(name + "同时对" + h1.name + "和" + h2.name + "进行了攻击 ");
    }

    public static void main(String[] args) {
        ADHero bh = new ADHero();
        bh.name = "赏金猎人";

        Hero h1 = new Hero();
        h1.name = "盖伦";
        Hero h2 = new Hero();
        h2.name = "提莫";

        bh.attack(h1);
        bh.attack(h1, h2);
    }

0x2 可变数量的参数

如果要攻击更多的英雄,就需要设计更多的方法,这样类会显得很累赘,像这样:

public void attack(Hero h1)
public void attack(Hero h1,Hero h2)
public void attack(Hero h1,Hero h2,Hero 3)

这时,可以采用可变数量的参数
只需要设计一个方法
public void attack(Hero ...heros)
即可代表上述所有的方法了
在方法里,使用操作数组的方式处理参数heros即可

public class ADHero extends Hero {

    public void attack() {
        System.out.println(name + " 进行了一次攻击 ,但是不确定打中谁了");
    }

    // 可变数量的参数
    public void attack(Hero... heros) {
        for (int i = 0; i < heros.length; i++) {
            System.out.println(name + " 攻击了 " + heros[i].name);

        }
    }

    public static void main(String[] args) {
        ADHero bh = new ADHero();
        bh.name = "赏金猎人";

        Hero h1 = new Hero();
        h1.name = "盖伦";
        Hero h2 = new Hero();
        h2.name = "提莫";

        bh.attack(h1);
        bh.attack(h1, h2);

    }

}

0x3 练习

设计一个类Support (辅助英雄)继承Hero,提供一个heal(治疗)方法
对Support的heal方法进行重载

heal() 
heal(Hero h) //为指定的英雄加血
heal(Hero h, int hp) //为指定的英雄加了hp的血

Demo1

public class ADhero extends Hero{
    public void heal(){
        System.out.println(name+"给自己加了血");
    }
     
    public void heal(Hero...heros){
        for (Hero hero_name : heros){
            System.out.println(name+"给"+hero_name.name+"加了血");
        }      
    }
     
    public void heal(int hp,Hero...heros){
        for (Hero hero_name : heros){
            System.out.println(name+"给"+hero_name.name+"加了"+hp+"点血");
        }
    }
     
    public static void main(String[] args) {
        ADhero bh = new ADhero();
        bh.name = "赏金猎人";
         
        Hero h1 = new Hero();
        h1.name = "盖伦";
         
        Hero h2 = new Hero();
        h2.name = "提莫";
         
        Hero h3 = new Hero();
        h3.name = "关羽";
         
        bh.heal();
        bh.heal(h1,h2,h3);
        bh.heal(100,h1,h2,h3);
    }
}

Demo2

public class Support extends Hero{
    public void heal(){
        System.out.println(name+"正在给自己加血");
    }
    public void heal(Hero... h){
        for(Hero hero_name : h){
        System.out.println(name+"正在给"+hero_name.name+"加血");
        }
    } //为指定的英雄加血
    public void heal(int hp,Hero... h){
        for(Hero hero_name : h){
        System.out.println(name+"给"+hero_name.name+"加了"+hp+"hp血");
        }
    } //为指定的英雄加血
     
    public static void main(String[] args){
        Support hero1=new Support();
        hero1.name="赏金猎人";
         
        Hero hero2=new Hero();
        hero2.name="盖伦";
         
        Hero hero3=new Hero();
        hero3.name="提莫";
         
        hero1.heal();
        hero1.heal(hero2);
        hero1.heal(100,hero3,hero2);
         
    }
}

大乔施放了治疗技能
大乔对李元芳施放了治疗技能
大乔对后羿施放了治疗技能
大乔对李元芳施放了治疗技能,并回复了100点血
大乔对后羿施放了治疗技能,并回复了100点血

package j2se;
 
public class Overload {
    public static void main(String[] args) {
        SupportHero s = new SupportHero();
        s.name = "大乔";
         
        Hero h1 = new Hero();
        h1.name = "李元芳";
//      int hp = 100;
         
        Hero h2 = new Hero();
        h2.name = "后羿";
         
        s.Treatment();
        s.Treatment(h1,h2);
        s.Treatment(100,h1,h2);
    }
 
}
 
class SupportHero extends Hero{
     
    //第一种方法:
    /*
    public void Treatment() {
        System.out.println(name+"施放了治疗技能");
    }
     
    public void Treatment(Hero h) {
        System.out.println(name+"对"+h.name+"施放了治疗技能");
    }
     
    public void Treatment(Hero h,int hp) {
        System.out.println(name+"对"+h.name+"施放了治疗技能,"+"并回复了"+hp+"点血");
    }
    */
     
     
    //第二种方法:
    public void Treatment() {
        System.out.println(name+"施放了治疗技能");
    }
     
    public void Treatment(/*int hp,*/Hero...heros) {
        for(int i=0;iout.println(name+"对"+heros[i].name+"施放了治疗技能");
        }
    }
     
    public void Treatment(int hp,Hero...heros) {
        for(int i=0;iout.println(name+"对"+heros[i].name+"施放了治疗技能,"+"并回复了"+hp+"点血");
        }
    }
}

你可能感兴趣的:(Java学习)