一、提高代码复用性
通过继承和重写机制,我们可以重用已有的代码,并根据需要进行修改。这使得我们可以重构代码、优化代码结构,提高代码复用性。例如以下代码:
class Animal {
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal 对象
Animal b = new Dog(); // Dog 对象
a.move();// 执行 Animal 类的方法
b.move();//执行 Dog 类的方法
}
}
上面的代码展示了动物和狗两个类,狗类继承了动物类,重写了动物类中的move()方法。在main函数中创建了一个动物对象和一个狗对象,分别对其进行了move()操作。我们发现动物对象调用的是Animal类中的move()方法,而狗对象调用的是Dog类中重写的move()方法。这样做的好处在于,通过继承Animal类,我们不仅仅重用了父类中的方法,而且还可以对其进行修改和替换,从而提高了代码的复用性。
二、实现多态
我们知道多态是指一个对象表现出多种形态,这也是Java面向对象编程的一个重要特性。重写是实现多态的重要手段。以下是示例代码:
class Shape {
public void draw() {
System.out.println("Drawing Shape");
}
}
class Rectangle extends Shape {
public void draw() {
System.out.println("Drawing Rectangle");
}
}
class Circle extends Shape {
public void draw() {
System.out.println("Drawing Circle");
}
}
public class TestShape {
public static void main(String[] args) {
Shape s1 = new Shape();
Shape s2 = new Rectangle();
Shape s3 = new Circle();
s1.draw();
s2.draw();
s3.draw();
}
}
上面的代码展示了Shape类,以及Rectangle和Circle两个类,Rectangle和Circle类均继承Shape类并重写了draw()方法。在main函数中创建了Shape、Rectangle和Circle三个对象,对其进行了draw()操作。我们发现输出结果是Drawing Shape、Drawing Rectangle和Drawing Circle,这说明了在实现多态过程中,s1、s2、s3分别表现出自己的形态。这也是重写带来的重要特性。
三、提高程序可扩展性
通过重写机制,我们可以在不修改父类代码的情况下,增加子类的特性。例如以下代码:
class Fruit {
void eat() {
System.out.println("Fruits are tasty");
}
}
class Apple extends Fruit {
void eat() {
System.out.println("Apples are my favorite");
}
void eatTest(){
System.out.println("Test from Apple");
}
}
class Banana extends Fruit{
void eat() {
System.out.println("Bananas are good for health");
}
void eatTest(){
System.out.println("Test from Banana");
}
}
public class TestFruit {
public static void main(String args[]){
Fruit obj1 = new Apple();
Fruit obj2 = new Banana();
obj1.eat();
obj2.eat();
Apple a = new Apple();
a.eatTest();
}
}
上面的代码定义了Fruit、Apple和Banana三个类,分别定义了其自己的eat()方法,Apple和Banana类还分别定义了自己的eatTest()方法。在main函数中创建了一个Apple对象和一个Banana对象,并对其进行eat()操作。我们可以发现输出结果分别是Apples are my favorite和Bananas are good for health,这说明了在重写过程中,Apple和Banana分别增加了自己的特性,从而提高了代码的可扩展性。
四、提高代码的可读性
重写会增加代码的复杂度,不过在一定程度上可以提升代码的可读性。例如以下代码:
abstract class Vehicle {
abstract void drive();
}
class Car extends Vehicle {
void drive() {
System.out.println("Drive car");
}
}
class Bike extends Vehicle {
void drive() {
System.out.println("Drive bike");
}
}
public class TestVehicle {
public static void main(String args[]){
Vehicle car = new Car();
Vehicle bike = new Bike();
abstractClassExample(car);
abstractClassExample(bike);
}
public static void abstractClassExample(Vehicle v){
v.drive();
}
}
上面的代码定义了Vehicle抽象类和Car、Bike两个类,重写了抽象类中的drive()方法。在main函数中创建了一个Car对象和一个Bike对象,并对其分别进行了abstractClassExample()操作。我们可以发现输出结果分别是Drive car和Drive bike,这说明Car和Bike类中重写的方法能够提高代码的可读性,通过具体的实现来体现类的含义。
五、小结
总结起来,重写是Java面向对象编程的重要机制,可以提高代码的复用性、实现多态、提高程序的可扩展性和可读性。在日常工作中,我们应该合理利用重写机制,从而编写出更加规范和易于维护的代码。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/183504.html
微信扫一扫
支付宝扫一扫