类的继承

继承

案例一:
通过继承继承父类成员

package com.company.继承;

public class demo1 {
    public static void main(String[] args) {
        teacher t1=new teacher();
        t1.method();

        Assintant assintant=new Assintant();
        assintant.method();
    }
}

package com.company.继承;

public class Assintant extends Employe{
}

package com.company.继承;

public class teacher extends Employe{

}
package com.company.继承;

public class Employe {
    public void method(){
        System.out.println("方法执行");
    }
}

运行结果
案例二:在父类的继承关系中,如果成员变量重名,则创建子类对象时访问有两种方式

  1. 直接通过子类对象访问成员变量
    等号左边是谁,就优先使用谁,没有则向上找
  2. 间接通过成员方法访问成员变量
    该方法属于谁,就优先用谁,没有则向上找

案例二:

package com.company.继承.demo2;

public class demo2 {
    public static void main(String[] args) {
        Dad dad=new Dad();
        System.out.println(dad.num);
        System.out.println(dad.numDad);
        System.out.println("==============");
        son son=new son();
        System.out.println(son.num);
        System.out.println(son.numSon);
        System.out.println("===============");
        System.out.println(son.numDad);
        son.methodSon();
        son.methodDad();
    }
}

运行结果

案例三:

package com.company.继承.dmeo3;

public abstract class Animal {
    public abstract void eat();
    public abstract void sleep();
}

package com.company.继承.dmeo3;

public class demo3 {
    public static void main(String[] args) {
        Dog2Ha dog2Ha=new Dog2Ha();
        dog2Ha.eat();
        dog2Ha.sleep();
        System.out.println("===========");
        DogGolden dogGolden=new DogGolden();
        dogGolden.eat();
        dogGolden.sleep();
    }

}

package com.company.继承.dmeo3;

public abstract class dog extends Animal{
    @Override
    public void eat() {
        System.out.println("狗在吃骨头");
    }
}

package com.company.继承.dmeo3;

public class Dog2Ha extends dog{

    @Override
    public void sleep() {
        System.out.println("我是重写的睡觉方法");
    }
}

package com.company.继承.dmeo3;

public class DogGolden extends dog{
    @Override
    public void sleep() {
        System.out.println("我是golddog重写的sleep方法");
    }
}

运行结果

案例四:群主发红包程序

package com.company.继承.demo4;

import java.util.ArrayList;

public class demo4 {
    public static void main(String[] args) {


        manager manager=new manager("群主",100);
        member member1=new member("成员A",0);
        member member2=new member("成员B",0);
        member member3=new member("成员3",0);

        manager.shw();
        member1.shw();
        member2.shw();
        member3.shw();
        System.out.println("=======我是分割线======");
        //群主总共法20块钱,分成三个红包
        ArrayList<Integer> redList=manager.send(20,3);
        //三个成员收红包
        member1.receiver(redList);
        member2.receiver(redList);
        member3.receiver(redList);

        manager.shw();
        member1.shw();
        member2.shw();
        member3.shw();
    }
}

package com.company.继承.demo4;

import java.util.ArrayList;

public class manager extends user{
    public manager() {
    }

    public manager(String name, int money) {
        super(name, money);
    }

    public ArrayList<Integer> send(int totalMoney,int count){
        //首先需要一个集合,用来存储若干个红包金额
        ArrayList<Integer> redList = new ArrayList<>();
        //先看一下群主自己有多少钱
        int leftMoey=super.getMoney();
        if(totalMoney>leftMoey){
            System.out.println("余额不足");
            return redList;
        }
        //扣钱其次是重新设置余额
        super.setMoney(leftMoey-totalMoney);
        //发红包需要平均拆分为count份
        int avg=totalMoney/count;
        int mod =totalMoney%count;//余额也就是剩下的零头
        //除不开的零头包在最后一个红包中
        //下面把红包一个一个放到集合中
        for(int i=0;i<count-1;i++){
            redList.add(avg);
        }
        //最后一个红包
        int last=avg+mod;
        redList.add(last);
        return redList;
    }
}

package com.company.继承.demo4;

import java.util.ArrayList;
import java.util.Random;

public class member extends user{
    public member() {
    }

    public member(String name, int money) {
        super(name, money);
    }
    public void receiver(ArrayList<Integer> list){
        //从多个红包里面随便抽取一个,给我自己
        //随机获取一个集合当中的索引编号
        int index=new Random().nextInt(list.size());
        //根据索引,从集合里面删除,并且得到被删除的红包,给我自己
        int delete=list.remove(index);
        //当前成员本来有多少钱
        int money=super.getMoney();
        //加法,并且重新设置回去
        super.setMoney(money+delete);
    }
}

package com.company.继承.demo4;

public class user {
    private String name;
    private int money;

    public user() {
    }

    public user(String name, int money) {
        this.name = name;
        this.money = money;
    }
    public void shw(){
        System.out.println("我叫"+this.name+"我有钱"+this.money);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值