Tại sao Python là ngôn ngữ OOP

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 Tommy
48

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
49

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
50

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
0

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
2

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 Tommy
3____14
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
5

Đ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

  1. 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ó
  2. 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ố
  3. Đ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 Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
0

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
9

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
11
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
13

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
15

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
18
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
05

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
06

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
07

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
08
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00______301
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00______306
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
08

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
04

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00______407
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
480

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
481

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
484
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
487

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00______3484____402
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
493

Đầ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 Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
0

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
9

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
11
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
13

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
15

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
18
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
05

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
517
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
484
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
08

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
06

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
07

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
08
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00______301
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00______306
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
21

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
22

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
23

Đầu ra

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1

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 Tommy
24

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
25

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
26

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
28
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
29____3519

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
32

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
18
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
37

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
05

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
45
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
47

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
50
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
57

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
62

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
66____119
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
484
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
08

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
81
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
86

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
88

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
90

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
18
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
95

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
98
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
100

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
103
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
105

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01____1107

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
109
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
111

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
66____119
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
484
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
08

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
81
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
86

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
139
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
03
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
144

 

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
145

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
146
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
148
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
149
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
150
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
151
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
150
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
153
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
150
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
155
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
157

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
158

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
159

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
160

Đầu ra

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
0

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 Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
162

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
163

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
166____119
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
172
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
176____119
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
182
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
185

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
163

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
176____119
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
195
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
198

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
176____119
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
007
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
009
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
011

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
012
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
014

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
015
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
017

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
018

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
019

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
020

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
021

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
022

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
023

Đầu ra

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
0

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 Tommy
024

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
025

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
026

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
028

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
18
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
036
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
038

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
041
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
038

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
044

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
51
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
046

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
1
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
17
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
18
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
519

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01____2053

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01____2055

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
057
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
01
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
00
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
063
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
02

_______201____309____400

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
19
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
069

 

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
06

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
071
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
4
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
073

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
09
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
075

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
076

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
077

 

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
078

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
079

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
080

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
081

Đầu ra

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
0

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

Tại sao Python là ngôn ngữ dựa trên OOP?

Python, giống như mọi ngôn ngữ hướng đối tượng khác, cho phép bạn định nghĩa các lớp để tạo đối tượng . Các lớp Python dựng sẵn là các kiểu dữ liệu phổ biến nhất trong Python, chẳng hạn như chuỗi, danh sách, từ điển, v.v. Một lớp là một tập hợp các biến thể hiện và các phương thức liên quan xác định một loại đối tượng cụ thể.

Python có thực sự là OOP không?

Python là ngôn ngữ lập trình hướng đối tượng nhưng nó cũng hỗ trợ lập trình chức năng. Tuy nhiên, nó nhằm ưu tiên lập trình dựa trên đối tượng.

Python có phải là ngôn ngữ OOP tốt nhất không?

Phân tích ngôn ngữ OOP phù hợp cho quá trình phát triển là điều có thể gây khó khăn cho các lập trình viên. Các ngôn ngữ OOP mang lại sự dễ dàng và minh bạch cho các ứng dụng ít phức tạp hơn. C++, GO, PYTHON, RUBY, JAVA là những ngôn ngữ OOP hàng đầu .

Chủ Đề