Java 继承与多态:代码重用与灵活性的巧妙结合

Java__0">Java 继承(子类和超类)

Java 中,可以从一个类继承属性和方法到另一个类。我们将“继承概念”分为两类:

子类(child): 从另一个类继承的类
超类(parent): 被继承的类

要从一个类继承,使用 extends 关键字。

示例:

class Vehicle {
  protected String brand = "Ford";    // Vehicle 属性
  public void honk() {          // Vehicle 方法
    System.out.println("Tuut, tuut!");
  }
}

class Car extends Vehicle {
  private String modelName = "Mustang";  // Car 属性
  public static void main(String[] args) {

    // 创建 myCar 对象
    Car myCar = new Car();

    // 在 myCar 对象上调用 honk() 方法(来自 Vehicle 类)
    myCar.honk();

    // 显示来自 Vehicle 类的 brand 属性的值和来自 Car 类的 modelName 的值
    System.out.println(myCar.brand + " " + myCar.modelName);
  }
}

输出:

Tuut, tuut!
Ford Mustang

注意:

  • 在上面的示例中,Vehicle 类是超类,Car 类是子类。
  • Car 类继承了 Vehicle 类的 brand 属性和 honk() 方法。
  • Car 类还可以添加自己的属性和方法,例如 modelName。

何时使用继承:

  • 代码重用:在创建新类时,重用现有类的属性和方法。
  • 代码的组织:将相关的类组织在一起,使其更容易理解和维护。

final 关键字:

如果不想让其他类从一个类继承,可以使用 final 关键字。

示例:

final class Vehicle {
  ...
}

class Car extends Vehicle {
  ...
}

输出:

Main.java:9: error: cannot inherit from final Vehicle
class Main extends Vehicle {
        ^
1 error

一些额外的说明:

  • 一个类只能有一个超类。
  • 子类可以访问超类的所有非私有成员(属性和方法)。
  • 子类可以覆盖超类的方法,以提供不同的实现。
  • 子类可以扩展超类的功能,添加新的属性和方法。

Java__85">Java 多态

多态 意味着“多种形式”,它发生在我们有许多通过继承相互关联的类时。

继承允许我们从另一个类继承属性和方法。多态使用这些方法执行不同的任务。这使我们能够以不同的方式执行单个操作。

示例:

假设有一个名为 Animal 的超类,它具有一个名为 animalSound() 的方法。Animal 的子类可以是 Pig、Cat、Dog、Bird - 它们也有它们自己的实现动物声音的方法(猪发出哼哼声,猫发出喵喵声等):

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

现在我们可以创建 Pig 和 Dog 对象,并在它们两者上调用 animalSound() 方法:

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal(); // 创建 Animal 对象
    Animal myPig = new Pig(); // 创建 Pig 对象
    Animal myDog = new Dog(); // 创建 Dog 对象
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

输出:

The animal makes a sound
The pig says: wee wee
The dog says: bow wow

何时以及为何使用“继承”和“多态”?

  • 代码重用: 在创建新类时,重用现有类的属性和方法。
  • 代码的组织: 将相关的类组织在一起,使其更容易理解和维护。
  • 灵活性: 允许代码以不同的方式执行,而无需更改代码本身。

多态的优点:

  • 代码更简洁:只需要编写一次代码,就可以在不同的类上使用。
  • 代码更易于维护:如果需要更改代码,只需更改一次,所有使用它的类都会自动更新。
  • 代码更易于扩展:可以轻松添加新的类,而无需更改现有的代码。

一些额外的说明:

  • 多态是面向对象编程的重要概念之一。
  • 多态可以使代码更简洁、更易于维护和扩展。
  • 抽象类和接口是实现多态的重要工具。

一些额外的思考:

  • 您可以想象其他可以利用多态的示例吗?
  • 多态在现实世界中有哪些应用?

最后

为了方便其他设备和平台的小伙伴观看往期文章:

微信公众号搜索:Let us Coding,关注后即可获取最新文章推送

看完如果觉得有帮助,欢迎 点赞、收藏、关注


http://www.niftyadmin.cn/n/5398598.html

相关文章

关于CSS常见选择器应用的基础教程

在网页开发中,CSS选择器是非常重要的一部分,它们用来指定你想要样式化的HTML元素。熟练掌握各种选择器的用法可以帮助你更有效地实现网页布局和设计。本文将介绍一些常见的CSS选择器,并演示它们的基本用法及应用场景。 一、元素选择器&#…

jenkins部署maven项目

流程: jenkins从代码仓库读取代码,将代码文件放入jenkins的工作空间,将jenkins工作空间的代码进行打包,将jar包远程发送给服务器。 一:所需插件二:Tools 三:System: 配置ssh连接的…

(案例贴2) html+css 倒计时器

欢迎大家使用这个计时器噢 老哥直接附代码咯. timer.html <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0">&l…

Flutter学习7 - Dart 泛型

1、泛型类 //泛型类 class Cache<T> {final Map<String, T> _cache {};void saveData(String key, T value) {_cache[key] value;}//泛型方法T? getData(String key) {return _cache[key];} }void main() {Cache<int> cache1 Cache();const String name…

微信小程序 --- 常用样式和组件

常用样式和组件 1. 组件和样式介绍 在开 Web 网站的时候&#xff1a; 页面的结构由 HTML 进行编写&#xff0c;例如&#xff1a;经常会用到 div、p、 span、img、a 等标签 页面的样式由 CSS 进行编写&#xff0c;例如&#xff1a;经常会采用 .class 、#id 、element 等选择…

AI新秀Mistral:“Open AI“ 新时代

最近互联网出现不少类似“下一代openai”、“GPT-4最强竞品”、“法国AI独角兽”、“欧洲的OpenAI”、“微软新宠儿”.... 的文章&#xff0c;都会附带一张图片&#xff0c;就是下面这张&#xff1a; 那么到底发生了什么&#xff0c;出来个什么东西呢&#xff1f;就是本文的主…

springboot+vue前后端分离适配cas认证的跨域问题

0. cas服务搭建参考:CAS 5.3服务器搭建_cas-overlay-CSDN博客 1. 参照springsecurity适配cas的方式, 一直失败, 无奈关闭springssecurity认证 2. 后端服务适配cas: 参考前后端分离项目(springbootvue)接入单点登录cas_前后端分离做cas单点登录-CSDN博客 1) 引入maven依赖 …

完美解决git 执行git push origin master指令 报错command not found

问题描述 报错信息为&#xff1a;在提交项目时的操作&#xff1a;找不到命令行 解决方案 &#xff08;1&#xff09;可以通过如下命令进行代码合并【注&#xff1a;pullfetchmerge】 git pull --rebase origin master&#xff08;2&#xff09;再执行语句&#xff1a; git p…