类和对象
类代表 群体;
独享代表 个体;
类相当于模板 对象相当于用模板生成的产品。
对象 有两部分:
1.属性 名字 性别 身高 体重
2.方法 sleep eat cry coding
类相当于模板,对象相当于用模板生成的产品。
#class 类 Poeple 类名 ()里面为继承的对象
#object 对象;物体 object相当于祖类
class People(object):
# 类 属性
name = ''
sex = False
age = 0
height = ''
weight = ''
def eat(self):
print('人类出生就会吃东西')
def sleep(self):
print('人类天生会睡觉')
def work(self):
print('每个人都有劳动的权利')
# 创建一个对象 叫做p
p = People()
p.name = '小明'
p.age = 17
p.sex = False
p.height = 176
p.weight = '69kg'
print(p.name)
p.eat()
p.sleep()
运行结果为:
小明
人类出生就会吃东西
人类天生会睡觉
__init__()方法
class People(object):
# init 初始化 魔术方法的一种 用来初始化对象
# __init__()方法会自动调用 在创建对象的时候
# init方法里面的属性 叫做对象属性
def __init__(self,name,age,sex):
# 将后面的值赋予自己的self.XX属性
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=0,sex=True):
self.name = name
self.age = age
self.sex = sex
我们可以试着做一个练习题:
1.男生女生都属于人类,有身高,性别,年龄,性别等属性;
2.创建一个男生,叫小明,包括其他一些属性;
3.创建一个女生,叫小樱,包括其他一些属性;
4.女生来判别男生,如果是男的,年龄相差不超过5岁,身上没有负债,就愿意和他在一起;
class Human(object):
def __init__(self,name,sex,age,height,weight,money = 0):
self.name = name
self.sex = sex
self.age = age
self.money = money
def isFitMySelf(self,other):
if self.sex == other.sex:
print('同性相斥')
return
if self.age - other.age > 5 or self.age - other.age < -5:
print('年龄不合适')
return
if other.money < 0 :
print('你是个好人')
return
print('喜结良缘')
xiaoMing = Human('小明',True,20,'180cm','60kg',1000000000)
xiaoYing = Human('小樱',False,18,'165cm','50kg')
xiaoYing.isFitMySelf(xiaoMing)
运行结果为:
喜结良缘
类属性和对象属性
对象属性不能通过雷小明加属性的方式来调用,只能通过对象来调用;
类属性可以通过类名+属性的方式来调用,也可以通过对象来调用。
对象方法可以通过 对象名+方法名 这种形式调用;
也可以通过 类名 + 方法名,然后将对象当成参数传入方法中这种形式来调用。
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)
运行结果为:
道德经
道德经
学习
Hello
Hello
私有属性
对象属性中,凡是带下划线的,都是不希望外部使用那个的(道德上);
但是并不是说我们完全不能使用;
如果加的是单下划线 _ ,可以通过 p1.name = '小明'这种方式使用;
如果加的是双下划线 __ ,可以通过 p1._People__name这种方式使用。
class People(object):
def __init__(self,name='',sex='',age='16',fond='学习'):
self.name = name
self._sex = sex
self.__age = age
self.fond = fond
# get set 方法
@property
def fond(self):
print('fond被get了')
return self.__fond
fond.setter
def fond(self,fond):
print('fond被set了')
self__fond = fond
p = People()
p.name = '张三'
print(p.name)
p._sex = '男'
print(p._sex)
print(p._People__age)
# set
str = '132456487'
# get
str1 = str
p.fond = '开车'
print(p.fond)
# 如果有这个实行 则修改属性值
# 如果没有这个属性 则添加
p.girlFriend = '小美'
print(p.girlFriend)
p.__age = '17'
print(p.__age)
运行结果为:
张三
男
16
开车
小美
17
继承
子类继承于父类;
子类会拥有父类的属性和方法;
子类也可以重写父类的属性和方法;
子类也可以拥有自己独有的属性和方法。
class People(object):
def __init__(self,age='',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('17','男','络腮胡')
print(m.age)
m.smoke()
m.eat()
运行结果为:
17
吸烟有害健康
人类为吃而活
上面这句话是继承自people的,正在说的这句话才是我自己的
面向对象编程的三大特点:
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 C')
class D(B,C):
pass
d = D()
d.say()
运行结果为:
my name is B
类方法和静态方法
任何一种类型的方法 都可以用类或者对象来调用。
什么时候使用对象方法,什么时候使用类方法和静态方法?
1.在绝大部分情况下我们的方法都会声明成 对象方法;
2.如果我们希望用类来处理这个方法,或者不希望某一个属性值不因为对象而改变的时候,就可用用类方法;
3.静态方法的使用绝大部分都可以用实例方法或者类方法来替代;class People(object):
# 类属性
count = 0
size = 0
def __init__(self,name = '',age = ''):
# 对象属性
self.name = name
self.age = age
# 对象方法
def say(self):
print('hi')
#class 类 method 方法
@classmethod
# cls class
def classFun(cls):
print('Hello,我是类方法')
# static 静态 method 方法
@staticmethod
# 不需要指定self或者cls来调用
def method():
print('我是静态方法')
People.classFun()
People.method()
p1 = People()
p1.classFun()
p1.method()
运行结果为:
Hello,我是类方法
我是静态方法
Hello,我是类方法
我是静态方法
统计:
某一个类的对象 一共被创建了多少个
class FoodTemplate(object):
# 一旦对象被创建会自动调用这个方法
# 类属性
count = 0
def __init__(self):
print('被创建了一次')
FoodTemplate.count += 1
@classmethod
def myCount(cls):
print('一共创建了{}个'.format(FoodTemplate.count))
yueBing = FoodTemplate()
manTou = FoodTemplate()
FoodTemplate.myCount()
运行结果为:
被创建了一次
被创建了一次
一共创建了2个
练习题
男女相亲
男方对女方的要求:
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='',yaowei='',height=''):
self.name = name
self.age = age
self.sex = sex
self.yaowei = yaowei
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.sex == False and self.age > other.age :
print('小弟弟,你太小')
return False
if self.sex == True and self.age < other.age:
print('大姐姐,你太大')
return False
class Man(People):
def __init__(self , salary='',house_area='',house_value=''):
super(Man,self).__init__(name='',sex='',age='',height='',yaowei='')
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.height < 165 :
print('我喜欢个子高的,你很好,但是我们不合适')
return
if other.yaowei > self.yaowei :
print('我喜欢稍微瘦一点的')
return
if other.readCount < 100 :
print('好看的皮囊和有趣的灵魂我都喜欢')
return
print('你是我的女神')
class Woman(People):
def __init__(self , name='',age='',sex='',yaowei='',height='',readCount =''):
super(Woman,self).__init__(name,age,sex,yaowei,height,)
self.readCount = readCount
def makeFriendWithBoy(self ,other):
result = super(Woman, self).sexIsFit(other)
if result == False:
return
result = super(Woman, self).ageIsFit(other)
if result == False:
return
if self.age < other.age - 10 :
print('你比我大了超过10岁')
return
if self.yaowei * 1.5 < other.yaowei :
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 = 176
jack.yaowei = 50
jack.salary = 20000000
jack.house_area = 130
jack.house_value = 2000000
rose = Woman(name='rose',sex=False,age=16,height=167,yaowei=45,readCount=120)
jack.makeFriendWithGirl(rose)
rose.makeFriendWithBoy(jack)
运行结果为:
你是我的女神
你是我的男神