Java-重载与覆写

本文探讨了Java中的函数重载(通过函数签名)和方法覆写(保持返回值类型一致),展示了如何在`Aclass`和`Student`类中实现,并通过`Studyf003`到`Studyf005`包中的示例代码进行实例解析。
摘要由CSDN通过智能技术生成
package cn.ALAN_CF.studyf003;

/**
 * @author 15328
 * 函数重载:
 * 函数签名=函数名和参数列表
 * 以函数签名来判断是否是同一个函数
 * 注意:函数签名不包括返回值类型
 */
public class Main {
    public static void main(String[] args) {
        Aclass obj = new Aclass();
        obj.foobar(15);
        obj.foobar("Alan");
    }
}

class Aclass{
    public void foobar(int x) {
        System.out.println("foobar (int x) : " + x);
    }
    public void foobar(String s){
        System.out.println("foobar (String s) :" + s);
    }
}

在这里插入图片描述

package cn.ALAN_CF.studyf004;

/**
 * @author 15328
 * 方法覆写,子类覆写方法的返回值类型要和父类的方法一样
 * "@Override"标志我们进行了覆写
 * 覆写:方法名、返回值类型以及参数表都一样
 */
public class Main {
    public static void main(String[] args) {
        Person stu0 = new Person();
        stu0.foobar();
        Student stu1 = new Student();
        stu1.foobar();
        Person p = new Student();
        p.foobar();
    }
}
class Person{
    public void foobar(){
        System.out.println("Person: foobar");
    }
}
class Student extends Person{
    @Override
    public void foobar(){
        System.out.println("Student: foobar");
    }
}

在这里插入图片描述

package cn.ALAN_CF.studyf005;

/**
 * @author 15328
 */
public class Student {
    protected double regularGrade;
    protected double finalExamGrade;
    public Student(double regularGrade,double finalExamGrade){
        this.regularGrade = regularGrade;
        this.finalExamGrade=finalExamGrade;
    }
    public double totalMarks(){
        return this.finalExamGrade;
    }
    public final void foobar(){
        //给方法加上final,则派生类中不能对他进行覆写
        //在初始化之后不允许修改
        //给字段用final的初始化方式:
        // 1、直接=初始化 2、构造方法初始化
    }
    @Override
    public String toString() {
        return "Regular: " + this.regularGrade + " FinalExam: " +
                this.finalExamGrade + " Total: " + this.totalMarks();
                //后面Student的对totalMarks()方法进行了覆写,
                //this如果是该子类的实例化对象,
                //则此处调用覆写后的totalMarks()
    }
}
final class Person{
    //不允许其他任何类继承此类
    //final:继承树的最后一个节点,不允许再有节点继承它
}
package cn.ALAN_CF.studyf005;

/**
 * @author 15328
 */
public class StudentOfAi extends Student{
    public StudentOfAi(double regularGrade,double finalExamGrade){
        super(regularGrade,finalExamGrade);
    }
    @Override
    public double totalMarks(){
        return 0.3 * this.regularGrade + 0.7 * this.finalExamGrade;
    }
}
package cn.ALAN_CF.studyf005;

/**
 * @author 15328
 */
public class StudentOfEd extends Student{
    public StudentOfEd(double regularGrade,double finalExamGrade){
        super(regularGrade,finalExamGrade);
    }
    @Override
    public double totalMarks(){
        return 0.5 * this.regularGrade + 0.5 * this.finalExamGrade;
    }
    public double originMarks(){//用super调用父类被覆写的方法的父类版本
        return super.totalMarks();
    }
}
package cn.ALAN_CF.studyf005;

/**
 * @author 15328
 */
public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[]{
                new StudentOfAi(80,60),
                new StudentOfEd(60,80)
        };
        for(Student s: students) {
            System.out.println(s);
            //实例对象放在输出上,则执行toString()方法
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值