Java基础(十五)面向对象编程 OOP 多态

Java面向对象基础知识笔记(四)


1. 对象数组的使用

在Java中,我们可以创建包含对象的数组。对象数组是一种特殊类型的数组,其中每个元素都是一个对象的引用。你可以将任何类的对象存储在对象数组中,并通过索引来访问和操作这些对象。

以下是对象数组的基本用法:

// 创建对象数组
ClassName[] arrayName = new ClassName[size];

// 实例化对象并存储到数组中
arrayName[index] = new ClassName();

// 访问对象数组中的元素
ClassName obj = arrayName[index];

// 修改对象数组中的元素
arrayName[index].propertyName = value;

例如,我们可以创建一个Person类的对象数组:

class Person {
    String name;
    int age;
}

public class Main {
    public static void main(String[] args) {
        // 创建对象数组
        Person[] people = new Person[3];

        // 实例化对象并存储到数组中
        people[0] = new Person();
        people[0].name = "Alice";
        people[0].age = 25;

        people[1] = new Person();
        people[1].name = "Bob";
        people[1].age = 30;

        people[2] = new Person();
        people[2].name = "Charlie";
        people[2].age = 35;

        // 访问对象数组中的元素
        System.out.println("Name: " + people[0].name + ", Age: " + people[0].age);
        System.out.println("Name: " + people[1].name + ", Age: " + people[1].age);
        System.out.println("Name: " + people[2].name + ", Age: " + people[2].age);
    }
}

2. 使用对象作为方法的参数

在Java中,我们可以将对象作为方法的参数传递。这对于需要传递多个相关数据的情况非常有用。通过将对象作为参数传递给方法,我们可以方便地访问和操作该对象的属性。

以下是使用对象作为方法参数的示例:

class Person {
    String name;
    int age;
}

public class Main {
    public static void printPersonInfo(Person person) {
        System.out.println("Name: " + person.name + ", Age: " + person.age);
    }

    public static void main(String[] args) {
        Person person = new Person();
        person.name = "Alice";
        person.age = 25;

        // 调用方法,并传递对象作为参数
        printPersonInfo(person);
    }
}

3. 向上转型和向下转型

1. 向上转型(Upcasting)

向上转型是指将子类类型的引用赋值给父类类型的引用。它是一种隐式的类型转换,在继承关系存在的情况下可以自动进行。

调用实例方法是,要看是哪个对象调用的就执行哪个方法,调用 static 方法时,调用的是父类方法(静态方法属于类)实例方法看等号右边,静态方法看等号左边

例如,如果有一个Cat类继承自Animal类,那么可以将Cat对象向上转型为Animal类型:

Cat cat = new Cat();
Animal animal = cat; // 向上转型

这里,cat对象被视为Animal对象,因为Cat继承自Animal,可以访问和操作Animal类的属性和方法。

2. 向下转型(Downcasting)

向下转型是指将父类类型的引用赋值给子类类型的引用。它是一种显式的类型转换,需要使用强制类型转换符()来进行。

在进行向下转型之前,应先使用instanceof运算符进行类型检查,以确保转型是安全的。

  • 使用 instanceof 时,对象的类型必须和 instanceof 后面的参数所指定的类有继承关系,否则会出现编译错误。
  • java16 的增强之后,对于 instanceof 的判断以及类型转换可以合二为一,如下:
if (pet instanceof Dog dog) {
	dog.catchingFlyDisc();
}
if (pet instanceof Brid bird) {
	brid.fly();
}

以下是一个向下转型的示例:

Animal animal = new Cat(); // 向上转型
Cat cat;

if (animal instanceof Cat) { // 类型检查
    cat = (Cat) animal; // 向下转型
    // 访问和操作Cat类特有的属性和方法
}

在这个示例中,我们首先将Cat对象向上转型为Animal类型,然后使用instanceof检查animal是否为Cat类型。如果是,就可以进行向下转型,并访问和操作Cat类特有的属性和方法。

多态


多态是面向对象编程的重要概念之一。它允许我们使用父类类型的引用来引用子类类型的对象,从而实现不同对象的统一处理。方法重写是实现多态的基础

多态有两种形式:编译时多态(静态多态)和运行时多态(动态多态)。

  • 编译时多态(重载):在编译阶段根据方法的参数类型和数量来决定调用哪个方法。多个同名方法通过参数列表来区分。例如,重载的print()方法可以接受不同类型的参数,并根据参数类型的不同执行不同的代码逻辑。
class Printer {
    public void print(String str) {
        System.out.println("Printing string: " + str);
    }

    public void print(int num) {
        System.out.println("Printing number: " + num);
    }
}

public class Main {
    public static void main(String[] args) {
        Printer printer = new Printer();
        printer.print("Hello");
        printer.print(123);
    }
}
  • 运行时多态(重写):在程序运行时根据对象的实际类型来决定调用哪个方法。子类可以重写(覆盖)父类的方法,并改变其行为。通过使用父类类型的引用来引用子类类型的对象,可以实现多态。
class Animal {
    public void makeSound() {
        System.out.println("Animal is making a sound");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat is meowing");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal1 = new Cat(); // 向上转型
        Animal animal2 = new Dog(); // 向上转型

        animal1.makeSound(); // 调用Cat类的makeSound方法
        animal2.makeSound(); // 调用Dog类的makeSound方法
    }
}

在上面的示例中,我们创建了Animal类的两个子类CatDog。通过向上转型将它们分别赋值给Animal类型的引用animal1animal2。然后,通过调用makeSound()方法,可以实现多态,具体执行哪个子类的方法由实际对象决定。

总结一下,多态允许我们根据上下文和对象的实际类型来选择合适的方法,提高代码灵活性和可复用性。根据用到的不同对象类型,响应不同的操作。

你可能感兴趣的:(Java,java,开发语言)