4.15方法的重写

在这里插入图片描述
1.子类重写父类方法,只是重写编写方法体的代码
2.如果父类方法是public,那么子重写时候不能使用缺省以下

父类Person1

package day07;
public class Person1 {
    int age;
    String name;
    int sex;

    public void showInfo(){
        System.out.println("我是父类");
        System.out.println(this.age);
        System.out.println(this.name);
        System.out.println(this.sex);
    }
    public void setInfo(int age,String name,int sex){
        System.out.println("我是父类");
        this.age=age;
        this.name=name;
        this.sex=sex;
    }
}

子类Student1

package day07;

public class Student1 extends Person1{
    String school;

    /**
     * 在此处eclipse使用快捷键alt+/可以快速重写
     * idea使用ctrl+O可以快速重写
     * 得到第12行的重写
     */
    @Override
    public void showInfo() {
//        super.showInfo();  重写过后会得到这条代码,将他随便改即可,比如我改成下面的14行的样子
        System.out.println("hello我是重写过的showInfo\n");
    }

    @Override
    public void setInfo(int age, String name, int sex) {
        System.out.println("hello我是重写过的setInfo");
        System.out.println(age);
        System.out.println(name);
        System.out.println(sex);
    }

    public static void main(String[] args) {
        Student1 stu=new Student1();
        stu.showInfo();
        stu.setInfo(12,"abc",2);
    }

}

运行截图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值