精通 Java Inheritance: Overriding Methods, toString, 与 Parameterized Constructors
目录 (页码)
1 2 3 4 5 6 |
1. 介绍 .................................. 1 2. 理解 Inheritance and Constructors .... 3 3. toString Method 的威力 ............... 7 4. Overriding Methods in Child Classes ............. 10 5. 实践示例: Bike and Vehicle Classes ...... 13 6. 结论 ...................................... 18 |
1. 介绍
Java programming 提供了一系列丰富的功能,使开发者能够创建结构清晰、可重用且易于维护的代码。在这本 eBook 中,我们深入探讨诸如 inheritance、the toString method、parameterized constructors 与 method overriding 等关键概念。基于详细的教程记录,我们探索如何利用这些概念来增强 object 表示和 behavior 在您的应用程序中的效果。
要点:
- 了解 inheritance 与 constructors 如何协同工作。
- 理解 overriding methods 对于获得更好输出的重要性。
- 发现 Java 如何使用 the toString method 来展示 object。
- 通过 annotations、code samples 与 visual diagrams 进行练习。
- 试验 class relationships(例如:Bike、Vehicle、Truck、Car)。
下面是一张比较表,概述了在 Java 中使用和不使用 toString methods 的区别:
情景 | 输出描述 |
---|---|
没有自定义的 toString method | 默认的内存地址(例如:com.example.XYZ) |
使用 base class 的 toString | 继承自 parent 的实现 |
重写的 toString 与 concatenating super | 组合输出:child 与 parent 的信息 |
使用时机:
- 当需要更具描述性的输出时,使用 overriding.
- 使用 parameterized constructors 初始化带有自定义值的 objects.
2. 理解 Inheritance 和 Constructors
Inheritance 是 object-oriented programming 中的一个基本概念,它允许 child (derived) class 从 parent (base) class 继承属性和 behaviors (methods)。它提供了一种重用代码以及添加新功能或修改现有 behaviors 的机制。
关键要素:
- Parent (Base) Class: 包含通用属性和 methods.
- Child (Derived) Class: 从 parent 继承并可以 override 或 extend functionalities.
- Parameterized Constructors: 特殊的 constructors,接受参数以有效初始化 object states.
例如,当创建一个 Bike object 时,它可能会使用其 parent Vehicle Class 中的 parameterized constructor,以确保在 instantiation 时拥有正确的属性。
3. toString Method 的威力
Java 中的 toString method 用于提供 object 的字符串表示。当您打印一个没有自定义 toString 的 object 时,Java 将显示一个默认字符串,其中包含 class 名称和 object 的 hash code(内存地址),这在实际应用中很少有用。
考虑以下情景:
- 如果没有提供 toString method,打印 object 会显示一个令人困惑的地址值。
- 当您在您的 class 中 override toString 时,您可以输出有意义的信息。
一种值得注意的方法是通过使用 super.toString 将您的自定义字符串与 parent class 的 toString 输出结合起来。该技术确保同时显示 child 与 base class 的所有关键信息。
4. Overriding Methods in Child Classes
Method overriding 允许 child class 提供其对 parent class 中已定义的 method 的自有实现。当 child class 需要修改与其特定上下文相关的 behavior 时,这一点尤为重要。
讨论要点:
- 具有与 parent method 相同 method signature 的 child method 将会覆盖 parent’s implementation.
- 最佳实践是使用 @Override annotation。虽然输出可能不变,但该 annotation 清楚地表明该 method 被 override 了。
- Overriding 提升代码可读性并保持 class behavior 的清晰。
例如,如果 Vehicle 和 Bike Classes 都有 run() method,那么如果 Bike 提供了其自有版本(例如,打印 “running bike” 而不是 “running vehicle”),则 Bike 的 run() method 将优先执行。
5. 实践示例: Bike and Vehicle Classes
下面是一个示例 Java 程序,演示了上述讨论的概念,并附有 inline comments 解释代码的每一部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
/* Program: Bike and Vehicle Inheritance Example This example demonstrates: • The use of parameterized constructors • Overriding the toString method • Method overriding between parent and child classes */ class Vehicle { // Private attributes for encapsulation private String type; // Parameterized constructor for initializing Vehicle public Vehicle(String type) { this.type = type; } // Overridable toString method in Vehicle public String toString() { return "Vehicle Type: " + type; } // Method to be overridden public String run() { return "running vehicle"; } } class Bike extends Vehicle { private String model; // Parameterized constructor calling the super class constructor public Bike(String type, String model) { super(type); // Calling Vehicle's constructor this.model = model; } // Overriding toString method to combine both Bike and Vehicle information @Override public String toString() { // Concatenates Bike details with the parent class's toString output return "Bike Model: " + model + ", " + super.toString(); } // Overriding run method for Bike specifics @Override public String run() { // When Bike's run method is invoked, it outputs a customized string return "running bike"; } // Additional method as per transcript demonstration: Unique bike action public String uniqueAction() { return "unique bike maneuver"; } } public class Main { public static void main(String[] args) { // Creating an object of Bike with parameterized initialization Bike myBike = new Bike("Motor Vehicle", "Sportster"); // Displaying Bike information using the overridden toString method System.out.println(myBike.toString()); // Expected Output: Bike Model: Sportster, Vehicle Type: Motor Vehicle // Demonstrating method overriding System.out.println(myBike.run()); // Expected Output: running bike // Accessing unique method from Bike System.out.println(myBike.uniqueAction()); // Expected Output: unique bike maneuver // Demonstration of inheriting parent's run method behavior: Vehicle myVehicle = new Vehicle("Generic Vehicle"); System.out.println(myVehicle.run()); // Expected Output: running vehicle } } |
逐步解释:
- Vehicle Class 被定义,具有 parameterized constructor 和 toString method,用于打印 vehicle type.
- Bike class 扩展自 Vehicle,在初始化期间调用 super constructor.
- Bike override 了 toString method,将其自身的 model 信息与 parent 的输出拼接。
- Bike 中的 run() method 被 override,以显示 “running bike” 而不是默认的 “running vehicle”。
- 在 Main class 中,创建了 Bike 和 Vehicle 的实例,以展示 override 和 inherited methods.
- 运行代码后将显示 override methods 的增强输出。
图示:Inheritance Structure
1 2 3 4 |
[Vehicle] │ ▼ [Bike] |
• Vehicle: 包含通用 attributes/methods(例如:type, run(), toString())
• Bike: 从 Vehicle 继承,并增加了特定的 attributes(例如:model)以及其自身的 methods 和 overrides.
6. 结论
在这本 eBook 中,我们探讨了 Java 中 inheritance 的多样性,重点关注了:
- parameterized constructors 在高效 object 初始化中的重要性.
- override the toString method 如何实现信息丰富的 object 表示.
- method overriding 的原则,确保 child classes 可以修改或 extend parent behavior.
- 通过实践示例(Bike vs. Vehicle)巩固这些概念.
请记住,实践是掌握这些 programming concepts 的关键。尝试使用其他 classes——如 Truck 和 Car——进一步丰富您的理解。如有疑问,请检查您的代码,测试各种实现,并查阅文档以不断提升您的技能。
注意:本文由 AI 生成。