Trong Python, Lập trình hướng đối tượng [OOPs] là một mô hình lập trình sử dụng các đối tượng và lớp trong lập trình. Nó nhằm mục đích thực hiện các thực thể trong thế giới thực như kế thừa, đa hình, đóng gói, v.v. trong lập trình. Khái niệm chính của OOP là liên kết dữ liệu và các chức năng hoạt động trên dữ liệu đó với nhau thành một đơn vị duy nhất để không phần nào khác của mã có thể truy cập dữ liệu này
Các khái niệm chính về lập trình hướng đối tượng [OOP]
- Lớp
- Các đối tượng
- đa hình
- đóng gói
- Di sản
- Trừu tượng dữ liệu
Úi
Lớp
Một lớp là một tập hợp các đối tượng. Một lớp chứa các bản thiết kế hoặc nguyên mẫu mà từ đó các đối tượng được tạo ra. Nó là một thực thể logic chứa một số thuộc tính và phương thức.
Để hiểu nhu cầu tạo một lớp, hãy xem xét một ví dụ, giả sử bạn muốn theo dõi số lượng chó có thể có các thuộc tính khác nhau như giống, tuổi. Nếu một danh sách được sử dụng, phần tử đầu tiên có thể là giống chó trong khi phần tử thứ hai có thể đại diện cho tuổi của nó. Giả sử có 100 con chó khác nhau, thì làm sao bạn biết phần tử nào được cho là phần tử nào? .
Một số điểm trên lớp Python.
- Các lớp được tạo bởi lớp từ khóa
- Thuộc tính là các biến thuộc về một lớp
- Các thuộc tính luôn công khai và có thể được truy cập bằng cách sử dụng dấu chấm [. ] nhà điều hành. Ví dụ. Lớp học của tôi. Thuộc tính của tôi
Cú pháp định nghĩa lớp
class ClassName: # Statement-1 . . . # Statement-N
Ví dụ. Tạo một lớp trống trong Python
con trăn
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy48
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy49
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy50
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy0
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy2
Trong ví dụ trên, chúng ta đã tạo một lớp có tên là dog bằng cách sử dụng từ khóa class
Các đối tượng
Đối tượng là một thực thể có trạng thái và hành vi được liên kết với nó. Nó có thể là bất kỳ đối tượng nào trong thế giới thực như chuột, bàn phím, ghế, bàn, bút, v.v. Số nguyên, chuỗi, số dấu phẩy động, mảng chẵn và từ điển đều là đối tượng. Cụ thể hơn, bất kỳ số nguyên đơn lẻ hoặc bất kỳ chuỗi đơn lẻ nào cũng là một đối tượng. Số 12 là một đối tượng, chuỗi “Xin chào, thế giới” là một đối tượng, danh sách là một đối tượng có thể chứa các đối tượng khác, v.v. Bạn đã sử dụng các đồ vật từ lâu và thậm chí có thể không nhận ra điều đó
Một đối tượng bao gồm
- Tiểu bang. Nó được đại diện bởi các thuộc tính của một đối tượng. Nó cũng phản ánh các thuộc tính của một đối tượng
- Cư xử. Nó được đại diện bởi các phương thức của một đối tượng. Nó cũng phản ánh phản ứng của một đối tượng với các đối tượng khác
- Xác thực. Nó đặt một tên duy nhất cho một đối tượng và cho phép một đối tượng tương tác với các đối tượng khác
Để hiểu trạng thái, hành vi và danh tính, chúng ta hãy lấy ví dụ về class dog [đã giải thích ở trên].
- Danh tính có thể được coi là tên của con chó
- Trạng thái hoặc Thuộc tính có thể được coi là giống, tuổi hoặc màu sắc của con chó
- Các hành vi có thể được coi là con chó đang ăn hay ngủ
Ví dụ. Tạo một đối tượng
Python3
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy3____14
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy5
Điều này sẽ tạo một đối tượng có tên là obj của lớp Dog được định nghĩa ở trên. Trước khi đi sâu vào các đối tượng và lớp, chúng ta hãy hiểu một số từ khóa cơ bản mà chúng ta sẽ sử dụng khi làm việc với các đối tượng và lớp
cái tôi
- Các phương thức lớp phải có thêm một tham số đầu tiên trong định nghĩa phương thức. Chúng tôi không cung cấp giá trị cho tham số này khi chúng tôi gọi phương thức, Python cung cấp nó
- Nếu chúng ta có một phương thức không có đối số, thì chúng ta vẫn phải có một đối số
- Điều này tương tự với con trỏ này trong C++ và tham chiếu này trong Java
Khi chúng ta gọi một phương thức của đối tượng này là myobject. phương thức [arg1, arg2], điều này được Python tự động chuyển đổi thành MyClass. method[myobject, arg1, arg2] – đây là tất cả những gì đặc biệt về bản thân
Ghi chú. Để biết thêm thông tin, hãy tham khảo bản thân trong lớp Python
Phương thức __init__
Phương thức __init__ tương tự như hàm tạo trong C++ và Java. Nó được chạy ngay khi một đối tượng của một lớp được khởi tạo. Phương pháp này rất hữu ích để thực hiện bất kỳ khởi tạo nào bạn muốn thực hiện với đối tượng của mình.
Bây giờ chúng ta hãy định nghĩa một lớp và tạo một số đối tượng bằng phương thức self và __init__
ví dụ 1. Tạo một lớp và đối tượng với các thuộc tính lớp và thể hiện
Python3
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy0
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy9
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy11
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy13
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy15
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy18
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy05
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy06
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy07
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy08
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00______301
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00______306
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy08
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy04
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00______407
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy480
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy481
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy484
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy487
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00______3484____402
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy493
Đầu ra
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy
ví dụ 2. Tạo Lớp và đối tượng bằng các phương thức
Python3
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy0
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy9
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy11
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy13
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy15
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy18
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy05
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy517
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy484
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy08
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy06
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy07
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy08
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00______301
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00______306
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy21
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy22
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy23
Đầu ra
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Ghi chú. Để biết thêm thông tin, hãy tham khảo Các lớp và đối tượng Python
Di sản
Kế thừa là khả năng của một lớp kế thừa hoặc kế thừa các thuộc tính từ một lớp khác. Lớp dẫn xuất các thuộc tính được gọi là lớp dẫn xuất hoặc lớp con và lớp mà các thuộc tính được dẫn xuất từ đó được gọi là lớp cơ sở hoặc lớp cha. Lợi ích của thừa kế là
- Nó thể hiện tốt các mối quan hệ trong thế giới thực
- Nó cung cấp khả năng sử dụng lại mã. Chúng ta không phải viết đi viết lại cùng một mã. Ngoài ra, nó cho phép chúng tôi thêm nhiều tính năng hơn vào một lớp mà không cần sửa đổi nó
- Nó có tính chất bắc cầu, nghĩa là nếu lớp B kế thừa từ lớp A khác, thì tất cả các lớp con của B sẽ tự động kế thừa từ lớp A
Các loại thừa kế –
Thừa kế đơn.
Kế thừa một cấp cho phép lớp dẫn xuất kế thừa các đặc điểm từ lớp cha đơn.
Kế thừa đa cấp.
Tính kế thừa đa cấp cho phép lớp dẫn xuất kế thừa các thuộc tính từ lớp cha trực tiếp, lớp này kế thừa các thuộc tính từ lớp cha của nó.
Kế thừa theo thứ bậc.
Kế thừa mức phân cấp cho phép nhiều hơn một lớp dẫn xuất kế thừa các thuộc tính từ lớp cha.
Nhiều kế thừa.
Tính kế thừa nhiều mức cho phép một lớp dẫn xuất kế thừa các thuộc tính từ nhiều lớp cơ sở.
Ví dụ. Kế thừa trong Python
Python3
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy24
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy25
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy26
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy28
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy29____3519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy32
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy18
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy37
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy05
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy45
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy47
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy50
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy57
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy62
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy66____119
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy484
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy08
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy81
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy86
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy88
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy90
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy18
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy95
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy98
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy100
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy103
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy105
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01____1107
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy109
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy111
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy66____119
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy484
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy08
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy81
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy86
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy139
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy03
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy144
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy145
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy146
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy148
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy149
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy150
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy151
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy150
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy153
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy150
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy155
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy157
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy158
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy159
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy160
Đầu ra
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy0
Trong bài viết trên, chúng ta đã tạo hai lớp i. e. Person [lớp cha] và Nhân viên [Lớp con]. Lớp Nhân viên kế thừa từ lớp Người. Chúng ta có thể sử dụng các phương thức của lớp người thông qua lớp nhân viên như đã thấy trong chức năng hiển thị trong đoạn mã trên. Một lớp con cũng có thể sửa đổi hành vi của lớp cha như đã thấy thông qua phương thức detail[]
Ghi chú. Để biết thêm thông tin, hãy tham khảo hướng dẫn Kế thừa trong Python của chúng tôi
đa hình
Đa hình đơn giản có nghĩa là có nhiều hình thức. Ví dụ: chúng ta cần xác định xem các loài chim đã cho có bay hay không, bằng cách sử dụng tính đa hình, chúng ta có thể thực hiện việc này bằng một hàm duy nhất
Ví dụ. Đa hình trong Python
Python3
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy162
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy163
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy166____119
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy172
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy176____119
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy182
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy185
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy163
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy176____119
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy195
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy198
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy176____119
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy007
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy009
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy011
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy012
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy014
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy015
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy017
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy018
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy019
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy020
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy021
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy022
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy023
Đầu ra
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy0
Ghi chú. Để biết thêm thông tin, hãy tham khảo Đa hình trong Hướng dẫn Python của chúng tôi
đóng gói
Đóng gói là một trong những khái niệm cơ bản trong lập trình hướng đối tượng [OOP]. Nó mô tả ý tưởng gói dữ liệu và các phương thức hoạt động trên dữ liệu trong một đơn vị. Điều này đặt ra các hạn chế đối với việc truy cập trực tiếp các biến và phương thức và có thể ngăn việc sửa đổi dữ liệu ngẫu nhiên. Để tránh thay đổi ngẫu nhiên, biến của đối tượng chỉ có thể được thay đổi bằng phương thức của đối tượng. Các loại biến đó được gọi là biến riêng
Một lớp là một ví dụ về đóng gói vì nó đóng gói tất cả dữ liệu là các hàm thành viên, biến, v.v.
Ví dụ. Đóng gói trong Python
Python3
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy024
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy025
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy026
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy028
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy18
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy036
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy038
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy041
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy038
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy044
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy51
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy046
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy1
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy17
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy18
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy519
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01____2053
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01____2055
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy057
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy01
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy00
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy063
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy02
_______201____309____400
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy19
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy069
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy06
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy071
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy4
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy073
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy09
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy075
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy076
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy077
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy078
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy079
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy080
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy081
Đầu ra
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy0
Trong ví dụ trên, chúng ta đã tạo biến c làm thuộc tính private. Chúng tôi thậm chí không thể truy cập trực tiếp vào thuộc tính này và thậm chí không thể thay đổi giá trị của nó
Ghi chú. để biết thêm thông tin, hãy tham khảo Đóng gói trong Hướng dẫn Python của chúng tôi
Trừu tượng dữ liệu
Nó ẩn các chi tiết mã không cần thiết khỏi người dùng. Ngoài ra, khi chúng tôi không muốn đưa ra các phần nhạy cảm trong quá trình triển khai mã của mình và đây là lúc sự trừu tượng hóa dữ liệu xuất hiện
Trừu tượng hóa dữ liệu trong Python có thể đạt được thông qua việc tạo các lớp trừu tượng
Lập trình hướng đối tượng trong Python. Phần 2 [Ẩn dữ liệu và In đối tượng]
https. //youtube. be/CiH7lN4
Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên