# Python中使用class()，面向对象有什么优势

``````class Cycle:
def __init__(self,r):
self.pi=3.14
self.r=r
a=Cycle(4)
b=Cycle(7)``````

``````‘‘‘

‘‘‘
def square(r,pi):
return pi * (r**2)
PI=3.14
a_r=4
a_square=square(a_r,PI)
b_r=7
b_square=square(b_r,PI)``````

``````class Cycle:
def __init__(self,r):
self.pi=3.14
self.r=r

def square(value):
if not isinstance(value,Cycle):
raise ValueError("value muse be Cycle instace")
value.square=value.pi * (value.r**2)

a=Cycle(4)
b=Cycle(7)

square(a)
square(b)``````

``````‘‘‘

‘‘‘
class Cycle:
def __init__(self,r):
self.pi=3.14
self.r=r
def square(self,value):
return self.pi * (self.r**2)``````

``````class Rectangle:
def __init__(self,length,height):
self.length=length
self.height=height
class Cycle:
def __init__(self,r):
self.pi=3.14
self.r=r
def rec_square(value):
if not isinstance(value,Rectangle):
raise ValueError("value muse be Rectangle instace")
value.square=value.length * value.height
def cycle_square(value):
if not isinstance(value,Cycle):
raise ValueError("value muse be Cycle instace")
value.square=value.pi * (value.r**2)``````

``````‘‘‘

‘‘‘
class Geometry:
def get_square(self):
raise NotImplementedError

class Rectangle(Geometry):
def __init__(self,length,height):
self.length=length
self.height=height
def get_square(self):
return self.length*self.height

class Cycle(Geometry):
def __init__(self,r):
self.pi=3.14
self.r=r
def get_square(self):
return self.pi * (self.r**2)

def square(value):
if not isinstance(value,Geometry):
raise ValueError("value muse be Geometry instace")
value.square=value.get_square()``````

``````class People:
def __init__(self,website):
self.__favorite_website="1024.com"
return self.__favorite_website=="1024.com"``````

``````‘‘‘

‘‘‘
class People:
def __init__(self,website):
self.__favorite_website="1024.com"
return self.__favorite_website=="1024.com"
@property
def favorite_website(self):
return self.__favorite_website
@favorite_website.setter
def favorite_website(self,value):
if value=="1024.com":
raise ValueError("你不能去草榴，兄弟，你营养不足")
self.__favorite_website=value``````

Python中使用class()，面向对象有什么优势

(0)
(0)