Một lớp là bản thiết kế hoặc nguyên mẫu do người dùng xác định từ đó các đối tượng được tạo. Các lớp cung cấp một phương tiện của dữ liệu bó và chức năng cùng nhau. Tạo một lớp mới tạo ra một loại đối tượng mới, cho phép các phiên bản mới của loại đó được thực hiện. Mỗi phiên bản lớp có thể có các thuộc tính được gắn vào nó để duy trì trạng thái của nó. Các phiên bản lớp cũng có thể có các phương thức [được xác định bởi lớp của nó] để sửa đổi trạng thái của nó.
Example:
class
Student:
stream
__ 'COE'
Rahul 886012 300000001
Rahul 886012 300000002
Rahul 886012 300000003
Rahul 886012 300000004
Rahul 886012 300000005
Rahul 886012 300000003
Rahul 886012 300000007
=
Rahul 886012 300000009
Rahul 886012 300000005
Rahul 886012 300000003
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 5222
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 54
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 55
=
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 57
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 58
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 59
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!1
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!2
=
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 57
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!5
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 59
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!7
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
class
0This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
class
2This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
class
4This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
class
6This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
class
8This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
Student:
0This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
Student:
2Output:
COE COE SHIVAM SACHIN 3425 3624 COE
Lưu ý: Để biết thêm thông tin, hãy tham khảo các lớp và đối tượng Python. For more information, refer to Python Classes and Objects.
Truy cập các chức năng của lớp cha
Khi một lớp kế thừa từ một lớp khác, nó kế thừa các thuộc tính và phương thức của một lớp khác. Một lớp kế thừa từ một lớp khác được gọi là lớp con và lớp mà lớp con được thừa hưởng được gọi là lớp cha. Nhưng bạn đã bao giờ tự hỏi làm thế nào để truy cập các phương pháp lớp cha mẹ? Điều này thực sự đơn giản, bạn chỉ cần gọi hàm tạo của lớp cha trong hàm tạo của lớp con và sau đó đối tượng của lớp con có thể truy cập các phương thức và thuộc tính của lớp cha.
Example:
class
Student:
4Student:
5 Student:
6
Rahul 886012 300000005
Rahul 886012 300000001
Rahul 886012 300000002
Rahul 886012 300000003
1
2
Rahul 886012 300000003
Rahul 886012 300000007
=
Rahul 886012 300000009
2
Rahul 886012 300000003
9=
stream
1Rahul 886012 300000005
Rahul 886012 300000001
stream
4Rahul 886012 300000003
stream
6
2
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9Rahul 886012 300000003
=
1
2
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9Rahul 886012 300000003
=
6class
=
8
Rahul 886012 300000005
Rahul 886012 300000001
Rahul 886012 300000002
Rahul 886012 300000003
'COE'
3
2
Rahul 886012 300000003
'COE'
6=
'COE'
8
2
Rahul 886012 3000000000
Rahul 886012 300000003
Rahul 886012 3000000002
Rahul 886012 300000005
Rahul 886012 300000001
Rahul 886012 3000000005
Rahul 886012 300000003
Rahul 886012 3000000007
Rahul 886012 3000000008
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9Rahul 886012 300000003
Rahul 886012 3000000012
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 55
=
Rahul 886012 3000000015
Rahul 886012 3000000016
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 59
Rahul 886012 3000000018
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 59
Rahul 886012 3000000020
Rahul 886012 3000000021
Rahul 886012 3000000022
Rahul 886012 3000000023
Output:
Rahul 886012 30000000
Lưu ý: Để biết thêm thông tin, hãy tham khảo kế thừa trong Python. For more information, refer to Inheritance in Python.
Truy cập phương thức lớp cha từ lớp bên trong
Một lớp bên trong hoặc lớp lồng nhau là một lớp được xác định bên trong cơ thể của một lớp khác. Nếu một đối tượng được tạo bằng một lớp, đối tượng bên trong lớp gốc có thể được sử dụng. Một lớp có thể có một hoặc nhiều hơn một lớp bên trong.
Các loại lớp bên trong:
- Nhiều lớp bên trong
- Lớp nội địa đa cấp
Nhiều lớp bên trong: Một lớp chứa nhiều hơn một lớp bên trong. A class containing more than one inner class.
Example:
class
Rahul 886012 3000000025
Rahul 886012 300000001
Rahul 886012 300000002
Rahul 886012 300000003
Rahul 886012 3000000007
Rahul 886012 300000005
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9Rahul 886012 3000000034
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
Rahul 886012 300000005
Rahul 886012 300000003
Rahul 886012 3000000038
=
Rahul 886012 300000003
Rahul 886012 3000000041
Rahul 886012 300000005
Rahul 886012 300000003
Rahul 886012 3000000044
=
Rahul 886012 300000003
Rahul 886012 3000000047
class
Rahul 886012 3000000050
Rahul 886012 300000005
Rahul 886012 300000001
Rahul 886012 3000000053
Rahul 886012 300000003
Rahul 886012 3000000007
Rahul 886012 3000000008
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9Rahul 886012 3000000059
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
class
Rahul 886012 3000000063
Rahul 886012 300000005
Rahul 886012 300000001
Rahul 886012 3000000053
Rahul 886012 300000003
Rahul 886012 3000000007
Rahul 886012 3000000008
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9Rahul 886012 3000000072
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
class
Rahul 886012 3000000063
Rahul 886012 3000000077
Rahul 886012 3000000078
Output:
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 5
Rahul
886012
30000000
74=
Rahul
886012
30000000
76 In multilevel inner classes, the inner class contains another class which is inner classes to the previous one.
Example:
Lớp bên trong đa cấp: Trong các lớp bên trong đa cấp, lớp bên trong chứa một lớp khác là các lớp bên trong của lớp trước.
Rahul 886012 300000001
Rahul 886012 300000002
Rahul 886012 300000003
Rahul 886012 3000000007
Rahul 886012 300000005
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9Rahul 886012 3000000034
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
class
Rahul 886012 3000000050
Rahul 886012 300000005
Rahul 886012 300000001
Rahul 886012 3000000053
Rahul 886012 300000003
Rahul 886012 3000000007
Rahul 886012 300000005
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 506
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
class
Rahul 886012 3000000063
Rahul 886012 3000000074
=
Rahul 886012 3000000076
Lớp bên trong đa cấp: Trong các lớp bên trong đa cấp, lớp bên trong chứa một lớp khác là các lớp bên trong của lớp trước.
class
Rahul 886012 3000000080
Rahul 886012 3000000008
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
stream
9SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 530
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
Rahul 886012 300000005
Rahul 886012 300000003
Rahul 886012 3000000088
=
Rahul 886012 300000003
Rahul 886012 3000000091
Rahul 886012 300000005
Rahul 886012 300000003
Rahul 886012 3000000094
=
Rahul 886012 300000003
Rahul 886012 3000000097
Rahul 886012 300000001
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 500
Rahul 886012 300000003
Rahul 886012 3000000007
class
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 510
2
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!9
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 553
Rahul 886012 300000005
Rahul 886012 300000001
Rahul 886012 300000002
Rahul 886012 300000003
Rahul 886012 3000000007
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 557
Rahul 886012 3000000008
Rahul 886012 300000003
Rahul 886012 3000000094
=
Rahul 886012 300000003
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 521
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 561
Rahul 886012 300000005
Rahul 886012 300000001
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 500
Rahul 886012 300000003
Rahul 886012 3000000007
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 565
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 566
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!8
Output:
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!