Hướng dẫn how do you create a object of another class in python? - làm thế nào để bạn tạo một đối tượng của một lớp khác trong python?

Vì vậy, tôi có 1 sinh viên gói với một học sinh lớp và tôi có một bản chính bên ngoài gói đó và tôi đang cố gắng tạo một đối tượng của ví dụ học sinh

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id

Tệp riêng biệt chính.py:

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()

Bất kỳ trợ giúp sẽ được đánh giá cao.

Đã hỏi ngày 13 tháng 10 năm 2013 lúc 5:01Oct 13, 2013 at 5:01

Hướng dẫn how do you create a object of another class in python? - làm thế nào để bạn tạo một đối tượng của một lớp khác trong python?

user2875416user2875416user2875416

51 huy hiệu vàng1 huy hiệu bạc6 huy hiệu đồng1 gold badge1 silver badge6 bronze badges

2

Trong khi lớp được xác định bên ngoài mã của bạn, lớp cần được nhập. Giả sử main.py và student.py nằm trong cùng một thư mục:

student.py

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id

main.py

def main():
    from student import Student
    print("is workign")
    temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()

Hướng dẫn how do you create a object of another class in python? - làm thế nào để bạn tạo một đối tượng của một lớp khác trong python?

Đã trả lời ngày 13 tháng 10 năm 2013 lúc 5:11Oct 13, 2013 at 5:11

Lucemialucemialucemia

6.1575 Huy hiệu vàng41 Huy hiệu bạc74 Huy hiệu đồng5 gold badges41 silver badges74 bronze badges

2

Cải thiện bài viết

Lưu bài viết

Điều kiện tiên quyết: Những điều cơ bản của rất tiếc trong PythonBasics of OOPs in Python

Trong bài viết này, chúng tôi sẽ học cách truy cập các phương thức và thuộc tính đối tượng trong các đối tượng 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. Chúng tôi xác định hai lớp (lớp thứ nhất và lớp thứ hai) với các định nghĩa thích hợp. & Nbsp;

  • Lớp đầu tiên bao gồm một hàm tạo và một phương thức.
  • Hàm tạo tạo thành một đối tượng của lớp thứ hai trong thuộc tính của lớp thứ nhất.
  • Phương pháp xác định sự hiện diện trong phương pháp hạng nhất.
  • Tương tự, lớp thứ hai bao gồm một hàm tạo và phương thức.
  • Các hàm tạo tạo thành một thuộc tính.
  • Phương pháp xác định sự hiện diện trong phương pháp lớp thứ hai.

Vì thuộc tính của lớp thứ nhất hoạt động như một đối tượng của lớp thứ hai, vì vậy tất cả các phương thức và thuộc tính của lớp thứ hai có thể được truy cập bằng cách sử dụng điều này:

object_of_first_class.attribute_of_first_class

Dưới đây là việc thực hiện:

Python3

class first:

    def

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
0
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
2

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
3
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
5
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
6
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
7

    def

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
0
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
2

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
3
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
4
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
5
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
6
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
7

class

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
9

    def

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
0
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
2

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
3
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
5
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
6
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
7

    def

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
0
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
2

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
3
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
4
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
5
object_of_first_class.attribute_of_first_class
8
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
7

class

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
9

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
4
<__main__.first object at 0x7fde6c57b828>
<__main__.second object at 0x7fde6c57b898>
GFG  
Inside second method
4

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
3
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
def main():
    from student import Student
    print("is workign")
    temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
7
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
6
def main():
    from student import Student
    print("is workign")
    temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
9

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id
4
<__main__.first object at 0x7fde6c57b828>
<__main__.second object at 0x7fde6c57b898>
GFG  
Inside second method
9

    def

object_of_first_class.attribute_of_first_class
2
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
1
 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
2

class2

Output:

<__main__.first object at 0x7fde6c57b828>
<__main__.second object at 0x7fde6c57b898>
GFG  
Inside second method

Chúng ta có thể tạo đối tượng của lớp trong một lớp khác không?

Bạn cũng có thể tạo một đối tượng của một lớp và truy cập nó trong một lớp khác. Điều này thường được sử dụng để tổ chức các lớp tốt hơn (một lớp có tất cả các thuộc tính và phương thức, trong khi lớp khác giữ phương thức chính () (mã sẽ được thực thi)). Hãy nhớ rằng tên của tệp Java sẽ khớp với tên lớp.. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name.

Một đối tượng có thể là một lớp của một đối tượng khác?

Không, theo nghĩa là các đối tượng được tạo ra từ các lớp, vì vậy rõ ràng chúng không phải là đối tượng. Các thành viên tĩnh của lớp chỉ thuộc về lớp., so obviously they are not objects. The static members of the class just belongs to the class.

__ init __ trong Python 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.

Những gì được sử dụng để tạo ra một đối tượng trong Python?

Một lớp là một bản thiết kế để tạo một đối tượng.Từ 'cá thể' có nghĩa là 'một bản sao'.Các thuộc tính hoặc trạng thái của một đối tượng được biểu thị bằng các biến thể hiện và hành vi được biểu thị bằng các phương thức hoặc hàm.Bạn có thể tạo nhiều đối tượng trong cùng một lớp. is a blueprint for creating an object. The word 'instance' means 'a copy'. The properties or state of an object are represented by instance variables and the behavior is represented by methods or functions. You can create multiple objects in the same class.