java学习Day3

继承

package day6;


class Item{
    String name;
    int pirce;
}
public class Armor extends Item{
    int ac;
    int exp;
    void shuchu(){
        System.out.println("name:"+this.name);
        System.out.println("price:"+this.pirce);
        System.out.println("护甲:"+this.ac);
        System.out.println("等级:"+this.exp);
    }
    public static void main(String[] args) {
        Armor wujin = new Armor();
        Armor yasuo = new Armor();
        wujin.name = "无尽之刃";
        wujin.pirce = 1980;
        wujin.ac = 100;
        wujin.exp = 5;
        yasuo.name = "亚索";
        yasuo.pirce = 18888;
        yasuo.ac = 250;
        yasuo.exp = 0;
        yasuo.shuchu();
        wujin.shuchu();
    }
}

重载

package day6;


class Hero {
    String neam;
    float hp;
}
public class Support extends Hero{
    void heal(){
        System.out.println("对全体英雄用了治疗");
    }
    void heal(Hero y){
        System.out.println("对英雄"+y.neam+"使用了治疗");
    }
    void heal(Hero x,int hp){
        x.hp = x.hp + hp;
        System.out.println("对英雄"+x.neam+hp+"点hp,当前hp为:"+x.hp);
    }

    public static void main(String[] args) {
        Support gailun = new Support();
        gailun.neam = "盖伦";
        gailun.hp = 250;
        gailun.heal();
        gailun.heal(gailun,123);
        gailun.heal(gailun);
    }
}

构造方法重载

package day6;

class Hero2{
    String name;
    float hp;
    int hujia;
    public Hero2(String Heroname){
        name = Heroname;
    }
    public Hero2(String Heroname,float hp,int hujia){
        name = Heroname;
        this.hp = hp;
        this.hujia = hujia;
    }
    void xianshi(Hero2 x){
        System.out.println(x.name+"   护甲:"+x.hujia+"   hp:"+x.hp);
    }

}
public class Gouzaofangfachongzai {
    public static void main(String[] args) {
        Hero2 gailun = new Hero2("盖伦",250,100);
        gailun.xianshi(gailun);
        Hero2 timo = new Hero2("提莫");
        timo.xianshi(timo);
    }
}

对于传递参数的理解

package day6;


class Hero3{
    String name;
    float hp;
    public Hero3(){

    }
    public Hero3(String name, int hp){
        this.name = name;
        this.hp = hp;
    }
    public void diaoxue(Hero3 x,int xp){
        x.hp = x.hp - xp;
    }
    public void fuhuo(Hero3 y){
           y = new Hero3("酒桶",500);//在这里传递的参数是传不回去的,他是新new的一块空间跟主函数里引用jiutong 不同
//         y.hp = 500;
//         y.name = "酒桶";

    }
}
public class Chuancan {
    public static void main(String[] args) {
        Hero3 jiutong = new Hero3("酒桶",500);
        System.out.println("Heroname:"+jiutong.name+"   Herohp:"+jiutong.hp);
        jiutong.diaoxue(jiutong,500);
        System.out.println("Heroname:"+jiutong.name+"   Herohp:"+jiutong.hp);
        jiutong.fuhuo(jiutong);
        System.out.println("Heroname:"+jiutong.name+"   Herohp:"+jiutong.hp);
    }
}

static 静态属性

package day7;

class day{
    static int a = 1234;
}
class day2 extends day{

}
public class test1{
    public static void main(String[] args) {
        day2 s = new day2();
        s.a= 123;
        System.out.println(s.a);
        day2 s2 = new day2();
        System.out.println(s2.a);
    }
}

类方法

package day7;

class day2{
    static int a = 1234;
    static void show(){
        System.out.println("quancunzuishuai");
    }
}

public class test1{
    public static void main(String[] args) {
        day2.show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值