在Python中,有以下几种方法可以定义对象数组:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("John", 25)
person2 = Person("Alice", 30)
persons = [person1, person2]
import numpy as np
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
point1 = Point(2, 3)
point2 = Point(4, 5)
points = np.array([point1, point2])
import pandas as pd
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Accord")
cars = pd.Series([car1, car2])
需要注意的是,以上的方法只是将对象保存在数组中,并不是真正的对象数组。在Python中,列表和数组是动态的,可以存储不同类型的元素。如果需要在静态类型语言中定义真正的对象数组,可以使用第三方库,如NumPy或Pandas。