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
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[]
Đã 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[]
3class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
4class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
5class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
6class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
7class
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[]
3class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
4class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
5object_of_first_class.attribute_of_first_class8
class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
7class
class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
9class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
4GFG Inside second method4
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[]
1def 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[]
9class Student:
Id=""
def __init__[self, Id]:
self.Id = Id
4GFG Inside second method9
def
object_of_first_class.attribute_of_first_class2
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[]
2class
2
Output:
GFG Inside second method