Java-继承、重载、重写、多态

继承

  • 在java中使用extends关键字来表示继承关系,例如:
/*父类*/
class Engineer {
    protected int no; // 编号
    protected String name;// 姓名
    protected String enterTime;// 入职时间

    public Engineer() {
        super();
    }

    public Engineer(int no, String name, String enterTime) {
        super();
        this.no = no;
        this.name = name;
        this.enterTime = enterTime;
    }

    public void showInfo() {
        System.out.println("[编号:" + no + ",姓名:" + name + ",入职时间:" + enterTime + "]");
    }
    //get和set方法省略
}
class SoftEngineer extends Engineer {
    private String type;// 语言类型

    public SoftEngineer() {
        super();
    }
    public SoftEngineer(int no, String name, String enterTime, String type) {
        super(no, name, enterTime);
        this.type = type;
    }
    @Override
    public void showInfo() {
        System.out.println("[编号:" + no + ",姓名:" + name + ",入职时间:" + enterTime + ",语言类型:" + type + "]");
    }

    public void coding() {
        System.out.println(no + "号软件工程师" + name + ",正在使用" + type + "语言编写项目。");
    }
}
class TestEngineer extends Engineer {
    private String level;// 等级

    public TestEngineer() {
        super();
    }
    public TestEngineer(int no, String name, String enterTime, String level) {
        super(no, name, enterTime);
        this.level = level;
    }
    @Override
    public void showInfo() {
        System.out.println("[编号:" + no + ",姓名:" + name + ",入职时间:" + enterTime + ",等级:" + level + "]");
    }
    public void test() {
        System.out.println(no + "号" + level + "测试工程师" + name + ",正在测试项目");
    }
}
  • 继承后的初始化顺序:父类属性->父类构造方法->子类属性->子类构造方法
  • 有些父类成员不能继承
    • 不能继承父类private成员
    • 不能继承子类与父类不在同包时,默认访问权限的成员
    • 不能继承构造方法
访问修饰符
访问修饰符本类同包子类其他
private
默认(friendly)
protected
public

方法重载

  • 在同一个类中定义的多个同名方法,参数列表不同(数据类型,数量,顺序)——两同一不同
  • 调用重载方法时,Java编译器能通过检查调用的方法的参数类型和个数选择一个恰当的方法。
  • 方法重载通常用于创建完成一组任务相似但参数的类型或参数的个数不同的方法。
  • 如,上例中构造方法的重载。

方法重写

  • 方法重写的规则
    • 在继承关系的子类中
    • 重写的方法名、参数、返回值类型必须和父类相同
    • 不能比父类更严格
    • 私有的方法不能继承因而也无法重写
    • 如,上例两个子类中的showInfo()方法
位置方法名参数列表返回值访问修饰符
方法重写子类相同相同相同不能比父类更严格
方法重载同类相同不同无关无关

向上转型(多态)

  • 向上转型,JAVA中的一种调用方式。向上转型是对父类的对象的方法的扩充,即父类的对象可访问子类从父类中继承来的和子类复写父类的方法。
  • 例子:
//例子1
public class Demo01 {
    public static void main(String[] args) {
        Artist a = new Artist();
        Piano p = new Piano();
        a.make(p);
    }
}

class Artist{
    //Piano传入时向上转型
    public void make(Instrument instrument) {
        System.out.print("音乐家正在");
        instrument.play();
    }
}

abstract class Instrument {
    protected String brand;
    protected double weight;

    public Instrument() {
        super();
    }
    public Instrument(String brand, double weight) {
        super();
        this.brand = brand;
        this.weight = weight;
    }
    //父类的抽象方法
    public abstract void play();
}

class Piano extends Instrument {
    protected double size;

    public Piano() {
        super();
    }

    public Piano(String brand, double weight, double size) {
        super(brand, weight);
        this.size = size;
    }
    //子类重写
    @Override
    public void play(){
        System.out.println("演奏钢琴");
    }
}

class Violin extends Instrument {
    protected double length;

    public Violin() {
        super();
    }
    public Violin(String brand, double weight, double length) {
        super(brand, weight);
        this.length = length;
    }
    //子类重写
    @Override
    public void play(){
        System.out.println("演奏小提琴");
    }
}
//例子2
public class Demo02 {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        Print print = null;
        while(true){
            System.out.println("请选择打印机:1、黑白打印机,2、彩色打印机");
            switch (reader.next()) {
                case "1":
                    //向上转型
                    print = new hbPrint();
                    break;
                case "2":
                    //向上转型
                    print = new csPrint();
                    break;
            }
            if(print != null){
                break;
            } else {
                System.out.println("输入有误");
            }
        }
        System.out.println("请输入打印内容:");
        String str = reader.next();
        System.out.println("打印结果:");
        print.printText(str);
    }
}

abstract class Print{
    abstract void printText(String str);
}

class hbPrint extends Print{
    @Override
    void printText(String str) {
        System.out.println(str);
    }
}

class csPrint extends Print{
    @Override
    void printText(String str) {
        System.err.println(str);
    }
}

执行效果测试

  • 代码:
public class Demo04 {
    public static void main(String[] args) {
        Base base = new Child();
        base.showName();
    }
}

class Base{
    public String name;

    public Base() {
        super();
        System.out.println(1);
        this.name = "张三";
    }

    public Base(String name) {
        super();
        System.out.println(2);
        this.name = name;
    }

    public void showName(){
        System.out.println(name);
    }


}

class Child extends Base{
    public Child(){
        super("李四");
        System.out.println(3);
    }
}
  • 猜测可能的输出结果:
    • 1 张三
    • 2 3 李四
  • 预测结果:name="李四"
  • 运行结果:
    运行结果
  • 结论:
    • 使用向上转型(父类引用指向子类对象)时,子类重写的方法会覆盖父类中的方法
    • 子类中的无参构造方法可以调用父类中重载的有参构造方法
    • ……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值