Hướng dẫn how to create object of one class in another class in python - cách tạo đối tượng của một lớp này trong một lớp khác trong python
Tôi đang học Python qua sách và internet và tôi bị mắc kẹt trong một vấn đề lớp học. Show
2 câu hỏi:
Khi tôi cố gắng tạo một thể hiện của một lớp trong lớp khác (riêng biệt), tôi có thể thực hiện trong một phương thức. Đây là mã:
Tuy nhiên, điều này sẽ cho phép tôi truy cập vào lớp FOO và đó là thông tin trong các phương thức thanh khác? Nếu vậy, làm thế nào? Tôi đã thử những điều sau và gặp lỗi:
Đây là lỗi:
Tôi nhận được cùng một lỗi khi tôi sử dụng:
Ngoài ra, làm thế nào tôi nên chuyển 'giá trị' từ lớp này sang lớp khác? Xin vui lòng cho tôi biết nếu cách tiếp cận chung và/hoặc cú pháp của tôi là sai.
Python3Course : Campus Preparation Duration : As per your schedule5 3 Course : Campus Preparation Duration : As per your schedule7 21 3 23 27 3 29Ví dụ: Lớp có phương thức __init __ ()class with __init__() method Python3
6 7 8 9 0 9 22 4 9 6 3 8 7 0 9 0 3 4 5 6 9 8 3 4Course : Campus Preparation Duration : As per your schedule1 6 9Course : Campus Preparation Duration : As per your schedule4 Course : Campus Preparation Duration : As per your schedule5 3 Course : Campus Preparation Duration : As per your schedule7 Course : Campus Preparation Duration : As per your schedule8 Output: Course : Campus Preparation Duration : As per your schedule Lớp bên trong ở PythonMột lớp được xác định trong một lớp khác được gọi là lớp bên trong hoặc lớp lồng nhau. Nếu một đối tượng được tạo bằng cách sử dụng lớp bên trong lớp con thì đối tượng cũng có thể được sử dụng bởi lớp cha hoặc lớp gốc. Một lớp cha có thể có một hoặc nhiều lớp bên trong nhưng nhìn chung các lớp bên trong được tránh.inner class or nested class. If an object is created using child class means inner class then the object can also be used by parent class or root class. A parent class can have one or more inner classes but generally inner classes are avoided. Chúng ta có thể làm cho mã của chúng ta trở nên định hướng đối tượng hơn bằng cách sử dụng một lớp bên trong. Một đối tượng duy nhất của lớp có thể chứa nhiều đối tượng phụ. Chúng tôi có thể sử dụng nhiều đối tượng phụ để cung cấp một cấu trúc tốt cho chương trình của chúng tôi. Example:
Python3
Name: Green Name: Light Green Code: 024avc0 Name: Green Name: Light Green Code: 024avc1 7 8 9 0Name: Green Name: Light Green Code: 024avc6 9Name: Green Name: Light Green Code: 024avc8 3 # create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()0 Name: Green Name: Light Green Code: 024avc6 9# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()3 3 9# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()6 Name: Green Name: Light Green Code: 024avc1 7 0 9 0
Name: Green Name: Light Green Code: 024avc6 3 4In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM5 6 9In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM8
This is an outer class This is the inner class This is an inner class of inner class0 Name: Green Name: Light Green Code: 024avc1 7 8 9 0Name: Green Name: Light Green Code: 024avc6 9Name: Green Name: Light Green Code: 024avc8 3 # create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()0 Name: Green Name: Light Green Code: 024avc6 9# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()3 3 9# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()6 Name: Green Name: Light Green Code: 024avc1 7 0 9 0Name: Green Name: Light Green Code: 024avc6 3 4In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM5 6 9In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM8
This is an outer class This is the inner class This is an inner class of inner class0 Name: Green Name: Light Green Code: 024avc6 9Name: Green Name: Light Green Code: 024avc8 3 class 0Course : Campus Preparation Duration : As per your schedule8 Name: Green Name: Light Green Code: 024avc6 9class 3 3 class 5Name: Green Name: Light Green Code: 024avc1 7 class 8 9 0Output: Name: Green Name: Light Green Code: 024avc Name: Green Name: Light Green Code: 024avc6def __init__(self): self.foo1 = FOO() 3 def __init__(self): self.foo1 = FOO() 4import os class FOO(): def __init__(self): self.values = [2, 4, 6, 8] def do_something(self, x, y): os.system("clear") z = self.values[x] * self.values[y] print "%r * %r = %r" % (self.values[x], self.values[y], z) class BAR(): def __init__(self): foo1 = FOO() def something_else(self, a, b): foo1.do_something(a, b) bar = BAR() bar.something_else(1, 2) 11def __init__(self): self.foo1 = FOO() 6import os class FOO(): def __init__(self): self.values = [2, 4, 6, 8] def do_something(self, x, y): os.system("clear") z = self.values[x] * self.values[y] print "%r * %r = %r" % (self.values[x], self.values[y], z) class BAR(): def __init__(self): foo1 = FOO() def something_else(self, a, b): foo1.do_something(a, b) bar = BAR() bar.something_else(1, 2) 9import os class FOO(): def __init__(self): self.values = [2, 4, 6, 8] def do_something(self, x, y): os.system("clear") z = self.values[x] * self.values[y] print "%r * %r = %r" % (self.values[x], self.values[y], z) class BAR(): def __init__(self): foo1 = FOO() def something_else(self, a, b): foo1.do_something(a, b) bar = BAR() bar.something_else(1, 2) 14Name: Green Name: Light Green Code: 024avc1 Course : Campus Preparation Duration : As per your schedule5 3 18Syntax: # create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass() 20 3 22
import os class FOO(): def __init__(self): self.values = [2, 4, 6, 8] def do_something(self, x, y): os.system("clear") z = self.values[x] * self.values[y] print "%r * %r = %r" % (self.values[x], self.values[y], z) class BAR(): def __init__(self): foo1 = FOO() def something_else(self, a, b): foo1.do_something(a, b) bar = BAR() bar.something_else(1, 2) 23Tại sao lớp bên trong? Cho nhóm hai hoặc nhiều lớp. Giả sử chúng ta có hai lớp từ xa và pin. Mỗi điều khiển từ xa đều cần một pin nhưng pin không có won từ xa được sử dụng. Vì vậy, chúng tôi làm cho pin trở thành một lớp bên trong đến điều khiển từ xa. Nó giúp chúng tôi lưu mã. Với sự giúp đỡ của lớp bên trong hoặc lớp lồng nhau, chúng ta có thể che giấu lớp bên trong khỏi thế giới bên ngoài. Do đó, ẩn mã là một tính năng tốt khác của lớp bên trong. Bằng cách sử dụng lớp bên trong, chúng ta có thể dễ dàng hiểu các lớp vì các lớp có liên quan chặt chẽ. Chúng tôi không cần phải tìm kiếm các lớp trong toàn bộ mã, tất cả đều gần như cùng nhau. Mặc dù các lớp bên trong hoặc lồng nhau không được sử dụng rộng rãi trong Python, nhưng nó sẽ là một tính năng tốt hơn để thực hiện mã vì việc tổ chức khi chúng ta sử dụng lớp bên trong hoặc lớp lồng nhau.Multiple inner class Python3Các loại lớp bên trong như sau: & nbsp; Name: Green Name: Light Green Code: 024avc1 7 8 9 0Name: Green Name: Light Green Code: 024avc6 9Name: Green Name: Light Green Code: 024avc8 3 # create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()0 Name: Green Name: Light Green Code: 024avc6 9# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()3 3 9# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()6 Name: Green Name: Light Green Code: 024avc1 7 0 9 0Name: Green Name: Light Green Code: 024avc1 7 0 9 0Name: Green Name: Light Green Code: 024avc6 3 4 56 57Name: Green Name: Light Green Code: 024avc6 3 4In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM5 6 9In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM8 Name: Green Name: Light Green Code: 024avc6 3 4In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM5 6 9In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM8
This is an outer class This is the inner class This is an inner class of inner class0 Name: Green Name: Light Green Code: 024avc6 9Name: Green Name: Light Green Code: 024avc8 3 class 0Name: Green Name: Light Green Code: 024avc6 9class 3 3 class 5Name: Green Name: Light Green Code: 024avc1 7 class 8 9 0Name: Green Name: Light Green Code: 024avc6 3 4 11 6 9 14 73 3 4 98 6 9 01Name: Green Name: Light Green Code: 024avc1 Course : Campus Preparation Duration : As per your schedule5 3 18
This is an outer class This is the inner class This is an inner class of inner class0 Name: Green Name: Light Green Code: 024avc6 9Name: Green Name: Light Green Code: 024avc8 3 class 0Name: Green Name: Light Green Code: 024avc6 9class 3 3 class 5Name: Green Name: Light Green Code: 024avc1 7 class 8 9 0 73 3 4 91 6 9In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM8 73 3 4 98 6 9 01Name: Green Name: Light Green Code: 024avc6 3 4 11 6 9 14Course : Campus Preparation Duration : As per your schedule8 Name: Green Name: Light Green Code: 024avc1 Course : Campus Preparation Duration : As per your schedule5 3 18 20 3 22 3 50 51 3 50 54
In outer class Name: Doctor Name: Dr. Savita Degree: BDS Name: Dr. Amit Degree: DM Tại sao lớp bên trong?Cho nhóm hai hoặc nhiều lớp. Giả sử chúng ta có hai lớp từ xa và pin. Mỗi điều khiển từ xa đều cần một pin nhưng pin không có won từ xa được sử dụng. Vì vậy, chúng tôi làm cho pin trở thành một lớp bên trong đến điều khiển từ xa. Nó giúp chúng tôi lưu mã. Với sự giúp đỡ của lớp bên trong hoặc lớp lồng nhau, chúng ta có thể che giấu lớp bên trong khỏi thế giới bên ngoài. Do đó, ẩn mã là một tính năng tốt khác của lớp bên trong. Bằng cách sử dụng lớp bên trong, chúng ta có thể dễ dàng hiểu các lớp vì các lớp có liên quan chặt chẽ. Chúng tôi không cần phải tìm kiếm các lớp trong toàn bộ mã, tất cả đều gần như cùng nhau. Mặc dù các lớp bên trong hoặc lồng nhau không được sử dụng rộng rãi trong Python, nhưng nó sẽ là một tính năng tốt hơn để thực hiện mã vì việc tổ chức khi chúng ta sử dụng lớp bên trong hoặc lớp lồng nhau. Ví dụ: Lớp bên trong đa cấpMultilevel inner class Python3
6Name: Green Name: Light Green Code: 024avc1 7 8 9 0Name: Green Name: Light Green Code: 024avc6 9 64 3 9 67Name: Green Name: Light Green Code: 024avc1 7 0 9 0Name: Green Name: Light Green Code: 024avc6 3 4 76 57Name: Green Name: Light Green Code: 024avc1 class 80Name: Green Name: Light Green Code: 024avc6 7 8 9 0 73 9 88 3 9 91Name: Green Name: Light Green Code: 024avc6 7 0 9 0 73 3 4 00 57Name: Green Name: Light Green Code: 024avc6 class 04 73 7 0 9 0 10 3 4 13 57Course : Campus Preparation Duration : As per your schedule5 3 Course : Campus Preparation Duration : As per your schedule7 Course : Campus Preparation Duration : As per your schedule8 3 50 21 3 23 24 3 50 27 3 29 30Đầu ra This is an outer class This is the inner class This is an inner class of inner class Làm thế nào để bạn sử dụng một đối tượng của một lớp trong một lớp khác trong Python?Nếu chúng ta có hai lớp khác nhau và một trong số này được xác định một lớp khác về việc gọi hàm tạo. Sau đó, phương thức và thuộc tính của một lớp khác có thể được truy cập bởi các đối tượng lớp đầu tiên (nghĩa là các đối tượng trong các đối tượng). Ở đây trong ví dụ dưới đây, chúng ta học cách truy cập đối tượng (các phương thức và thuộc tính của nó) trong một đối tượng.the method and attributes of another class can be accessed by first class objects ( i.e; objects within objects ). Here in the below example we learn to access object (its methods and attributes) within an object.
Làm thế nào để bạn tạo một đối tượng của một lớp khác trong Python?Một lớp giống như một hàm tạo đối tượng hoặc "bản thiết kế" để tạo các đối tượng ... Tạo một lớp học.Để tạo một lớp, hãy sử dụng lớp từ khóa: .... Tạo đối tượng.Bây giờ chúng ta có thể sử dụng lớp có tên MyClass để tạo các đối tượng: .... Tham số tự..... Sửa đổi thuộc tính đối tượng..... Xóa thuộc tính đối tượng..... Xóa đối tượng .. Phương pháp __ init __ là gì?Phương pháp __init__ là tương đương Python của hàm tạo C ++ theo cách tiếp cận hướng đối tượng.Hàm __init__ được gọi mỗi khi một đối tượng được tạo từ một lớp.Phương thức __init__ cho phép lớp khởi tạo các thuộc tính của đối tượng và không phục vụ mục đích nào khác.Nó chỉ được sử dụng trong các lớp học.the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes. |