Skip to main content

Bài 7: Lập trình Hướng đối tượng (OOP) trong Python

· One min read

1. Định nghĩa lớp và đối tượng

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

def greet(self):
print(f"Xin chào, tôi là {self.name}")

p = Person("Nam", 25)
p.greet()

2. Kế thừa trong Python

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

3. Bài Tập

  1. Viết một lớp Animal và lớp con Dog kế thừa từ Animal.
  2. Viết một lớp Car với các thuộc tính brand, model, year và một phương thức in thông tin xe.