person.py中定义类如下:
class Rectangle:
def __init__(self, length, width):
self.__length = length # 两个下划线(__)表示私有变量
self.__width = width
# 读取属性
@property
def length(self):
return self.__length
# 读取属性
@property
def width(self):
return self.__width
# 设置函数
@length.setter
def length(self, llength):
if llength > 0:
self.__length = llength
else:
print("length is not negative number.")
# 设置函数
@width.setter
def width(self, wwidth):
if wwidth > 0:
self.__width = wwidth
else:
print("width is not negative number.")
def __str__(self):
return "length of Rectangle is %d, width of Rectangle is %d" % (self.__length, self.__width)
另一个.py文件中调用:
from Person import *
# 调用另一个.py文件中的类,必须按照上面的写法
rect = Rectangle(12, 6)
print(rect)
print("Area is %d" % (rect.length * rect.width))
# 调用另一个.py文件中的类,必须按照上面的写法
rect = Rectangle(12, 6)
print(rect)
print("Area is %d" % (rect.length * rect.width))