- 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Rectangle:
def __init__(self, long=10, wide=10):
self.long1 = long
self.wide1 = wide
def area(self):
print(f'面积:{self.long1*self.wide1}')
def perimeter(self):
print(f'周长:{(self.long1+self.wide1*2)}')
- 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
class NMR:
def __init__(self, x1=0, y1=0):
self.x = x1
self.y = y1
def distance(self, x2=0, y2=0):
print(f'两点间的距离为:{((self.x - x2) ** 2 + (self.y - y2) ** 2) ** 0.5}')
- 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切
class Circle:
def __init__(self, radius1=0, center1=(0, 0)):
self.radius = radius1
self.center = center1
# self.radius0 = radius2
# self.center0 = center2
def perimeter(self, a=3.14):
print(f'圆的周长是:{2*a*self.radius}')
def area(self, a=3.14):
s = f'圆的面积为:{a * self.radius ** 2}'
return s
def combination(self, radius0=0, center0=(0, 0)):
if self.radius + radius0 == ((self.center[0] - center0[0])**2 + (self.center[1] - center0[1])**2)**0.5:
print('外切')
else:
print('没有外切')
- 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度
class Line:
def __init__(self, start1=(0, 0), end1=(0, 0)):
pass
-
定义一个狗类和一个人类:
狗拥有属性:姓名、性别和品种 拥有方法:叫唤
人类拥有属性:姓名、年龄、狗 拥有方法:遛狗
class Dog: def __init__(self, name1='', sex1='', var1=''): self.name = name1 self.sex = sex1 self.var = var1 def action(self): print(f'{a}叫唤') a = Dog('大黄').name Dog().action() class Person: def __init__(self, name2='', age='', dog=a): self.name = name2 self.age = age self.dog = dog def action(self): print(f'{self.name}遛{self.dog}')