Learn a little python knowledge in three minutes 2----------My understanding of python classes (Class) and objects (Object)

Insert image description here



1. What are (Class) and (Object)?对象

Python is an object-oriented programming language, and its basic object-oriented programming mechanism is classes and objects.
A class is a blueprint or template that defines the properties and methods of an object. They are a code structure that can contain property storage and function operations, making it more structured and readable. An object is an instantiation of these classes, has the properties and methods defined by the class, and can use them to operate in the program.

2. Implementation of Python classes and objects

1. Define the class

In Python, a class definition begins with the keyword class, followed by the name of the class. The parentheses after the class name can contain the name of the parent class. If a class has no parent class, it is written as object. Class definitions end with a colon (:). Here is a simple example of defining a class:

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print(f"Hi, my name is {
      
      self.name}, and I am {
      
      self.age} years old.")

2. Create objects

In Python, an object is an instantiation of a class. Instantiation is the process by which we create Python objects. An object can be created using the name of the class. Here is an example of creating an object:

student1 = Student('Tom', 20)
student2 = Student('Jack', 21)

3. Call the properties and methods of the class

The attributes of a class can be called in the form of object name.property name, and the methods of a class can be called in the form of object name.method name. Here is an example of accessing class properties and methods:

student1.name  # 访问对象的属性
student1.introduce()  # 调用对象的方法

3. Use python to implement an animal class (Animal) and its two subclasses (Cat and Dog)

# 定义动物类
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print(f"I am {
      
      self.name}, and I am {
      
      self.age} years old.")

    def eat(self):
        print("I am eating.")

# 定义猫类
class Cat(Animal):
    def __init__(self, name, age, color):
        super().__init__(name, age)
        self.color = color

    def introduce(self):
        print(f"I am {
      
      self.name}, and I am {
      
      self.color} in color. I am {
      
      self.age} years old.")

    def sleep(self):
        print("I am sleeping.")

# 定义狗类
class Dog(Animal):
    def __init__(self, name, age, breed):
        super().__init__(name, age)
        self.breed = breed

    def introduce(self):
        print(f"I am {
      
      self.name}, and I am {
      
      self.breed} breed. I am {
      
      self.age} years old.")

    def bark(self):
        print("I am barking.")

# 创建猫对象和狗对象,并调用它们的方法
cat1 = Cat("Kitty", 2, "Gray")
cat1.introduce()  # 输出:I am Kitty, and I am Gray in color. I am 2 years old.
cat1.eat()  # 输出:I am eating.
cat1.sleep()  # 输出:I am sleeping.

dog1 = Dog("Puppy", 3, "Golden Retriever")
dog1.introduce()  # 输出:I am Puppy, and I am Golden Retriever breed. I am 3 years old.
dog1.eat()  # 输出:I am eating.
dog1.bark()  # 输出:I am barking.

Summarize

In this example, the Animal class is the parent class, and Cat and Dog are its subclasses. Both Cat and Dog inherit the properties and methods of the Animal class, and they each implement some additional methods of their own. Demonstrate class and object usage by creating Cat and Dog objects to instantiate these two classes and calling their methods.

Guess you like

Origin blog.csdn.net/qlkaicx/article/details/131331356