java期末复习(构造方法和成员方法,重写和重载)

一、构造方法(Constructor)

1 什么是构造方法

构造方法是用于初始化新创建对象的一种特殊方法,它在创建对象时自动调用。

**构造方法就是"新生儿出生证明"**​ - 告诉计算机如何"生"出一个对象宝宝,并给它起名字、设置基本信息。

java 复制代码
public class Person {
    private String name;
    private int age;
    
    // 构造方法
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

2 构造方法的特点

  • 与类同名

  • 没有返回类型(连void也没有)

  • 不能被static、final、synchronized、abstract、native修饰

  • 不能被显式调用 ,只能通过new关键字调用

  • 一个类可以有多个构造方法(重载)

3 构造方法的类型

java 复制代码
public class Student {
    private String name;
    private int id;
    
    // 1. 无参构造方法(默认构造方法)
    public Student() {
        this.name = "Unknown";
        this.id = 0;
    }
    
    // 2. 有参构造方法
    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }
    
    // 3. 拷贝构造方法
    public Student(Student other) {
        this.name = other.name;
        this.id = other.id;
    }
    
    // 4. 构造方法重载
    public Student(String name) {
        this(name, 0);  // 调用另一个构造方法
    }
}

4 构造方法链

java 复制代码
class Animal {
    public Animal() {
        System.out.println("Animal构造方法");
    }
}

class Dog extends Animal {
    public Dog() {
        super();  // 隐式调用父类构造方法
        System.out.println("Dog构造方法");
    }
}

二、成员方法(Instance Method)

1 什么是成员方法

成员方法是定义在类中,用于执行特定操作或计算的代码块。

成员方法就是对象能做的"事情"或"技能",就像人会说话、狗会叫、计算机会计算一样。

java 复制代码
public class Calculator {
    // 成员方法
    public int add(int a, int b) {
        return a + b;
    }
    
    public double multiply(double a, double b) {
        return a * b;
    }
}

2 成员方法的特点

  • 必须有返回类型(可以是void)

  • 可以被各种访问修饰符修饰

  • 可以被static、final等修饰

  • 需要通过对象实例调用(非static方法)

三、方法重载(Overloading)

1 什么是方法重载

在同一个类中,多个方法名称相同但参数列表不同

java 复制代码
public class MathOperations {
    
    // 重载示例1:参数类型不同
    public int add(int a, int b) {
        return a + b;
    }
    
    public double add(double a, double b) {
        return a + b;
    }
    
    // 重载示例2:参数个数不同
    public int add(int a, int b, int c) {
        return a + b + c;
    }
    
    // 重载示例3:参数顺序不同
    public void printInfo(String name, int age) {
        System.out.println("Name: " + name + ", Age: " + age);
    }
    
    public void printInfo(int age, String name) {
        System.out.println("Age: " + age + ", Name: " + name);
    }
}

2 重载的规则

  1. 方法名必须相同

  2. 参数列表必须不同(类型、个数、顺序)

  3. 返回类型可以相同也可以不同

  4. 访问修饰符可以不同

  5. 可以抛出不同的异常

3 构造方法重载

java 复制代码
public class Rectangle {
    private double length;
    private double width;
    
    // 无参构造
    public Rectangle() {
        this.length = 1.0;
        this.width = 1.0;
    }
    
    // 一个参数构造
    public Rectangle(double side) {
        this.length = side;
        this.width = side;
    }
    
    // 两个参数构造
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
}

四、方法重写(Overriding)

1 什么是方法重写

子类重新定义父类中已有的方法,提供特定实现。

java 复制代码
class Animal {
    public void makeSound() {
        System.out.println("动物发出声音");
    }
    
    public void eat() {
        System.out.println("动物在吃东西");
    }
}

class Dog extends Animal {
    // 重写父类方法
    @Override
    public void makeSound() {
        System.out.println("汪汪汪");
    }
    
    // 重写并扩展父类方法
    @Override
    public void eat() {
        super.eat();  // 调用父类方法
        System.out.println("狗在吃骨头");
    }
}

2 重写的规则

  1. 方法名必须相同

  2. 参数列表必须完全相同

  3. 返回类型必须相同或是其子类(协变返回类型)

  4. 访问权限不能比父类更严格

  5. 不能抛出比父类更宽泛的检查异常

3 @Override注解

  • 不是必须的,但强烈推荐使用

  • 帮助编译器检查是否正确重写

  • 提高代码可读性

java 复制代码
class Parent {
    public Number getNumber() {
        return 10;
    }
}

class Child extends Parent {
    @Override
    public Integer getNumber() {  // 协变返回类型
        return 20;
    }
}

五、重载 vs 重写对比

特性 重载 (Overloading) 重写 (Overriding)
发生位置 同一个类中 父子类之间
方法名 必须相同 必须相同
参数列表 必须不同 必须相同
返回类型 可以不同 必须相同或是子类
访问修饰符 可以不同 不能更严格
异常 可以不同 不能更宽泛
调用时机 编译时确定 运行时确定
目的 增加方法灵活性 实现多态性

六、综合示例

java 复制代码
// 父类
class Vehicle {
    private String brand;
    
    // 构造方法
    public Vehicle(String brand) {
        this.brand = brand;
    }
    
    // 构造方法重载
    public Vehicle() {
        this("Unknown");
    }
    
    // 成员方法
    public void start() {
        System.out.println(brand + " 车辆启动");
    }
    
    // 可以被重写的方法
    public void displayInfo() {
        System.out.println("品牌: " + brand);
    }
    
    // 重载的方法
    public void refuel() {
        System.out.println("加油");
    }
    
    public void refuel(String fuelType) {
        System.out.println("加" + fuelType);
    }
    
    public void refuel(double amount) {
        System.out.println("加油" + amount + "升");
    }
}

// 子类
class ElectricCar extends Vehicle {
    private int batteryCapacity;
    
    // 构造方法
    public ElectricCar(String brand, int batteryCapacity) {
        super(brand);  // 调用父类构造方法
        this.batteryCapacity = batteryCapacity;
    }
    
    // 方法重写
    @Override
    public void start() {
        System.out.println("电动汽车静音启动");
    }
    
    // 方法重写
    @Override
    public void displayInfo() {
        super.displayInfo();  // 调用父类方法
        System.out.println("电池容量: " + batteryCapacity + "kWh");
    }
    
    // 重写父类的重载方法
    @Override
    public void refuel() {
        System.out.println("充电中...");
    }
}

// 测试类
public class Main {
    public static void main(String[] args) {
        // 构造方法使用
        Vehicle vehicle1 = new Vehicle();
        Vehicle vehicle2 = new Vehicle("Toyota");
        ElectricCar car = new ElectricCar("Tesla", 100);
        
        // 方法调用
        vehicle1.start();  // 父类方法
        car.start();       // 子类重写的方法
        
        // 多态
        Vehicle v = new ElectricCar("BYD", 80);
        v.start();         // 实际调用子类方法
        
        // 方法重载演示
        car.refuel();
        car.refuel("快充");
        car.refuel(50.5);
        
        // 方法重写演示
        car.displayInfo();
    }
}

七、重要注意事项

1 构造方法特殊规则

  1. 如果没有显式定义构造方法,编译器会自动提供无参构造

  2. 如果定义了有参构造,无参构造不会自动生成

  3. 构造方法第一行可以是this()super(),但不能同时存在

  4. 如果没有显式调用super(),编译器会自动插入对父类无参构造的调用

2 静态方法不能重写

java 复制代码
class Parent {
    public static void staticMethod() {
        System.out.println("Parent static method");
    }
}

class Child extends Parent {
    // 这是隐藏(hiding),不是重写
    public static void staticMethod() {
        System.out.println("Child static method");
    }
}

3 private/final方法不能重写

  • private方法对子类不可见,不能重写

  • final方法禁止重写

4 抽象方法必须重写

java 复制代码
abstract class AbstractClass {
    public abstract void abstractMethod();
}

class ConcreteClass extends AbstractClass {
    @Override
    public void abstractMethod() {
        // 必须实现
    }
}

八.总结

  1. 构造方法 用于对象初始化,成员方法定义对象行为

  2. 重载是编译时多态,在同一个类中通过参数区分

  3. 重写是运行时多态,在继承关系中修改方法实现

  4. 合理使用构造方法和成员方法,以及正确运用重载和重写,是面向对象编程的重要基础

  5. 使用@Override注解可以提高代码的健壮性和可读性

相关推荐
weixin_307779138 小时前
Jenkins声明式流水线权威指南:从Model API基础到高级实践
开发语言·ci/cd·自动化·jenkins·etl
Aevget8 小时前
DevExtreme JS & ASP.NET Core v25.2预览 - DataGrid/TreeList全新升级
开发语言·javascript·asp.net·界面控件·ui开发·devextreme
C雨后彩虹8 小时前
事件推送问题
java·数据结构·算法·华为·面试
海涛高软8 小时前
Qt菜单项切换主界面
开发语言·qt
码界奇点8 小时前
基于Golang与Vue3的全栈博客系统设计与实现
开发语言·后端·golang·车载系统·毕业设计·源代码管理
知识分享小能手8 小时前
CentOS Stream 9入门学习教程,从入门到精通,CentOS Stream 9 的过滤器 —— 语法详解与实战案例(18)
linux·学习·centos
deng-c-f8 小时前
Linux C/C++ 学习日记(51):内存池
jvm·学习
刘孬孬沉迷学习8 小时前
WebRTC 协议
学习·5g·webrtc·信息与通信·信号处理
丝斯20118 小时前
AI学习笔记整理(33)—— 视觉Transformer (ViT)与自注意力机制
人工智能·笔记·学习