对象继承的方法
1.对象:
class 类 People 类名 ()里面为继承 的对象
object 对象;物体 object 相当于祖类
对象 经常使用两部分
1.属性 姓名 性别 身高 体重 年龄
2.方法 sleep eat cry coding
类相当于模板 对象相当于模板生成的产品
class People(object):
#类:属性
name = ''
sex = False
age = ''
height = ''
weight = ''
def eat(self):
print('人类出生就会吃喝玩乐')
def sleep(self):
print('人类很完美')
def work(self):
print('每个人都是地球的一部分')
1.1常见一个对象 叫做p
p = People()
list = list()
dic = dict()
p.name = '瞎蒙'
p.age = 17
p.sex = False
p.height = 175
p.weight = '45kg'
print(p.name)
p.eat()
p.sleep()
p.work()
class People(object):
1.2:init 初始化 魔术方法的一种 用来初始化对象
__init__()方法会自动调用 在创建对象的时候
__init__里面的属性 叫做对象属性
__init__(self,name,age,sex):
#将后面的值赋予自己的self.xxx属性
self.name = name
self.age = age
self.sex = sex
def study(self):
print('学习使我感到快乐')
#在创建对象的时候 直接赋值
p = People('瞎蒙',17,True)
print(p.name)
class Person(object):
def __init__(self,name = '',age = '',sex = True):
self.name = name
self.age = age
self,sex = sex
2.例题:
1.男生女生都属于人类,有身高,性别,年龄,姓名等属性
2.创建一个男生,叫张生,包括其他一些属性
3.创建一个女生,叫做崔莺莺,包括其他一些属性
4.女生来判别男生,如果是男的,年龄相差不超过5岁,身上没有负债
则愿意和他在一起
class Human(object):
def __init__(self,name = '',age =0,sex =False,height=120,money=0):
self.name = name
self.age = age
self.sex = sex
self.height = height
self.money = money
def isFitMe(self,other):
if self.sex == other.sex:
print('同性排斥')
return
if self.age - other.age >5 or self.age - other.age < -5:
print('年龄不合适')
if other.money < 0:
print('吾身可依')
return
print('喜结良缘')
cuiyingying = Human('崔莺莺',17,False,165,1000000)
zhangsheng = Human('张生',20,True,180,1000)
cuiyingying.isFitMe(zhangsheng)
对象总结
对象属性不能通过类名+属性的方式来调用 只能通过对象来调用
类属性可以通过类名+属性的方式来调用 也可以通过对象来调用
print(People。fond)
对象方法可以通过对象 + 方法名 这种形式类调用
也可以通过类名 + 方法名 ,然后讲对象当成参数传入方法中这种形式来调用
class People(object):
#类属性
name = '道德经'
age = ''
def __init__(self,fond = ''):
#对象属性
self.fond = fond
#对象方法 self是调用方法的本身
def say(self):
print('hello')
p1 = People()
p1.fond = '学习'
print(People.name)
print(p1.name)
print(p1.fond)
p1.say()
People.say(p1)
2.私有属性
对象属性中,凡是带下划线的,都是不希望外部使用(道德上)
但是并不是说我们完全不能使用
如果加的单下划线 ,可以通过p1.name = ‘下达’这种方式使用
如果加的是双下划线,可以通过p1_People__name这种方式使用
class People(object):
def __init__(self,name = '',age = '12',sex = '',fond = '学习'):
self.name = name
self._sex = sex
self.__age = age
self.__fond = fond
#get set 方法 get:获得 set:设置
@property
def fond(self):
print('fond被set了')
return self.__fond
fond.setter
def fond(self,fond):
return self.__fond
print('fond被set了')
p = People()
p.name = '张三'
print(p.name)
p._sex = '男'
print(p._People__age)
#set
str = '123456789'
str1 = str
p.fond = '开车'
print(p.fond)
如果有这个属性 则修改属性值
如果没有这个属性 则添加
p.girFriend = '小妹'
print(p.girFriend)
3.继承:
子类继承于父类
子类会有父类的属性和方法
子类也可以重写父类的属性和方法
子类也可以拥有自己独有的属性和方法
class People(object):
def __init__(self,age = '88',sex = ''):
self.age = age
self.sex = sex
def eat(self):
print('人类为吃而活')
def breath(self):
print('美国的空气如此香甜')
class Man(People):
def __init__(self,age = '',sex = '',huzi=''):
#继承父类已有的属性
super(Man,self).__init__(age,sex)
def smoke(self):
print('吸烟有害健康')
def eat(self):
#继承父类的eat方法
super(Man,self).eat()
print('上面这句话是继承people的,正在说的这句话才是我自己的')
class Boy(Man):
def __init__(self):
pass
m = Man()
print(m.age)
m.smoke()
m.eat()
3.1:面向对象编程的三大特点:
1.封装:函数
2.继承: 子类继承父类
3.多态: 不同对象调用同样方法 出现不用结果 就是多态
class A(object):
def say(self):
print('my name is A')
class B(A):
def say(self):
print('my name is B')
class C(A):
def say(self):
print('my name is B')
class D(B,C):
pass
d = D()
d.say()
4.类方法和静态方法
class People(object):
#类属性
count = 0
size = 0
def __abs__(self,name='',age=''):
self.name = name
self.age = age
#对象方法
def say(self):
print('hai')
class 类 method 方法 接上面的属性:
@classmethod
def classFun(cls):
print('Hello,我是类方法')
#ststic 静态 method方法
@staticmethod
#不需要指定self或者cls来调用
def method():
print('我是静态方法')
People.classFun()
People.method()
p1 = People
p1.classFun()
p1.method()
People.say(p1)
总结:任何一种类型的方法 都可以用类或者对象来调用
?什么时候使用对象方法,什么时候使用类方法和静态方法
1.在绝大部分情况下我们的方法都会声明成 对象方法
2.如果我们希望用类来处理这个方法,或者不希望某一个属性值不因为对象
而改变的时候 就可以用类方法
3.静态方法的使用绝大部分都可以用实力方法或者类方法来替代
统计:某一个类的对象 一共被创建了多少个
class FoodTemplate(object):
#一旦对象被创建会自动调用这个方法
#类属性
count = 0
def __init__(self):
print('创建了一次')
FoodTemplate.count +=1
pass
@classmethod
def myCount(cls):
print('一共创建了{}个'.format(FoodTemplate.count))
yuBing = FoodTemplate()
manTou = FoodTemplate()
FoodTemplate.myCount()
5.小练习:
男女相亲
男方对女方的要求:
0.对方必须是女的
1.女方升高不小于165
2.女方的年龄不能比自己大
3.女方的腰围一定要比自己小
4.女方读的书不能少于100
女方对男方的要求
0.对方必须是男的
1.对方的年龄不能比自己小 同时不能大于自己10岁
2.对方的升高不能比自己小
3.对方的腰围不能超过自己的1.5倍
4.男方应该有稳定收入,年薪不能少于20w
5.男方房子面积不能少于120,总结之不能少于200w
class People(object):
def __init__(self,name='',age='',sex='',waist='',height=''):
self.name=name
self.age=age
self.sex=sex
self.waist=waist
self.height= height
def sexIsFit(self,other):
if self.sex==True and self.sex==other.sex:
print('我的男的,但是你也是男的')
#在此 用False代表相亲失败
return False
if self.sex==False and self.sex==other.sex:
print('我是女的,你也是女的')
return False
def ageIsFit(self,other):
if self.age==False and self.age>other.age:
print('你太小了弟弟')
if self.sex==True and self.age<other.age:
print('你太大了姐姐')
return False
def waistIsFit(self,other):
if self.waist==False and self.waist>other.waist:
print('姐姐你腰围肉太多了')
if self.waist==True and self.waist<other.waist:
print('你腰真漂亮')
return False
class Man(People):
def __init__(self,salary='',house_area='',house_value=''):
super(Man,self).__init__(name='',sex='',age='',height='',waist='')
self.salary = salary
self.house_area= house_area
self.house_value=house_value
def makeFriendWithGirl(self,other):
result=super(Man,self).sexIsFit(other)
if result==False:
return
result=super(Man,self).ageIsFit(other)
if result==False:
return
if other.waist<165:
print('我喜欢腰围瘦一点的')
return
if other.readCount<100:
print('好看的皮囊和有趣的灵,是我喜欢的')
return
print('你是我的天使')
class Woman(People):
def __init__(self,name='',sex='',age='',height='',waist='',readCound=''):
super(Woman,self).__init__(name,age,sex,waist,height)
self.readCount=readCound
def makeFriendWithBoy(self,other):
result=super(Woman,self).sexIsFit(other)
if result ==False:
return
if self.age < other.age-10:
print('你比我年龄超过了10岁')
return
if self.waist * 1.5 < other.waist:
print('你的腰比我粗太多了')
return
if other.salary < 200000:
print('你的工资太低了吧!')
return
if other.house_area < 120 and other.house_value < 2000000:
print('你的房子太小')
return
print('你是我的男神')
jack=Man()
jack.sex=True
jack.age=21
jack.height=180
jack.waist=12
jack.salary=2000000000
jack.house_area=1000
jack.house_value=200000000
rose = Woman(name='rose',sex=False,age=15,height=178,waist=23,readCound=102)
rose.makeFriendWithBoy(jack)
以上就是今天的内容: