Java学习笔记——面向对象之多态性

目录

一、多态的概念

二、多态的优点

三、多态存在的三个必要条件

四、多态的实现方式


一、多态的概念

多态性是面向对象编程的又一个重要特征,它是指在父类中定义的属性和方法被子类继承之后,可以具有不同的数据类型或表现出不同的行为,这使得同一个属性或方法在父类及其各个子类中具有不同的含义。

二、多态的优点

  • 消除类型之间的耦合关系
  • 可替换性
  • 可扩充性
  • 接口性
  • 灵活性
  • 简化性

三、多态存在的三个必要条件

Java实现多态有 3 个必要条件:继承、重写和向上转型。

  • 继承:在多态中必须存在有继承关系的子类和父类。
  • 重写:子类对父类中某些方法进行重新定义,在调用这些方法时就会调用子类的方法。
  • 向上转型:在多态中需要将子类的引用赋给父类对象,只有这样该引用才既能可以调用父类的方法,又能调用子类的方法。

 代码实例

class Shape{
    void draw(){}
}

class Circle extends Shape{
    void draw(){
        System.out.println("Circle draw");
    }
}

class Square extends Shape{
    void draw(){
        System.out.println("Square draw");
    }
}

class Triangle extends Shape{
    void draw(){
        System.out.println("Triangle draw");
    }
}

注: 使用多态时,若父类没有该方法,则编译错误;

四、多态的实现方式

  1. 重写
  2. 接口
  3. 抽象类和抽象方法

代码实例 

class Employee {
    private String name;  // 姓名
    private String address;  // 地址
    private int number;  // 号码

    public Employee(String name, String address, int number){
        System.out.println("Employee的构造函数");
        this.name = name;
        this.address = address;
        this.number = number;
    }

    public String toString(){
        return name + " " + address + " " + number;
    }

    public String getName(){
        return name;
    }

    public String getAddress(){
        return address;
    }

    public int getNumber(){
        return number;
    }

    public void setAddress(String newAddress){
        address = newAddress;
    }

    public void mailCheck() {
        System.out.println("邮寄支票给: " + this.name
                + " " + this.address);
    }
}

class Salary extends Employee{

    private double salary;

    public Salary(String name, String address, int number, double salary){
        super(name, address, number);
    }

    public double getSalary() {
        return salary;
    }
    public void setSalary(double newSalary) {
        if(newSalary >= 0.0) {
            salary = newSalary;
        }
    }
    public double computePay() {
        System.out.println("计算工资,付给:" + getName());
        return salary/52;
    }

    public void mailCheck() {
        System.out.println("Salary 类的 mailCheck 方法 ");
        System.out.println("邮寄支票给:" + getName()
                + " ,工资为:" + salary);
    }
}

public class Day19{
    public static void main(String[] args){
        Salary s = new Salary("Jack", "Beijing", 3, 3600.00);
        Employee e = new Salary("B", "Shanghai", 2, 2400.00);
        System.out.println("使用 Salary 的引用调用 mailCheck -- ");
        s.mailCheck();
        System.out.println("\n使用 Employee 的引用调用 mailCheck--");
        e.mailCheck();
    }
}

输出结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值