python - 如何在内部类中调用外部类参数?
问题描述
您好,我是在 python 中使用类的初学者。我正在尝试在内部类中获取外部类参数参数。例如我有:
class Rectangle:
n_edges = 4
def __init__(self, length, width):
self.length = length
self.width = width
def rectangle_area(self):
return self.length*self.width
def rectangle_perimeter(self):
return 2*self.length + 2*self.width
class Patch:
def __init__(self, cor1, cor2):
self.cor1 = cor1
self.cor2 = cor2
# I want here to get length and width
所以我想在 Patch 类中有 Rectangle 类的长度和宽度参数,而不是在 Patch 的构造函数中将它们作为参数传递
任何想法 ?
非常感谢 !
解决方案
首先用第一类 i 保存 python 文件。e 将矩形类保存在 python 文件中(rectangle.py)。然后在其他python文件中导入矩形。py 文件,所以你可以使用矩形类的所有功能
推荐阅读
- c# - C# - 每次重新颁发 SSL 证书时,都需要更改应用程序中的序列号
- ios - 仅在 iOS 14、SwiftUI 时显示一些视图
- git - 有什么简单快速的方法来判断一个 git repo 是否使用了 LFS
- python - 破碎方程求解器
- python - 创建一个函数来检查一个数 n 是否可以被两个数 x 和 y 整除。所有输入都是正数,非零数字
- ios - 如何将 ARPlaneAnchor 转换为 2d 线(平行投影)
- javascript - Javascript Map 函数未存储正确的值
- r - 如何在R中重置为0的cumsum?
- react-native - 从 flatlist 切换到 Listitem 并检索数据
- python - AttributeError:模块“cv2.cv2”没有属性“nameWindow”