1. 对象
1.编程语言发展历程:机器语言、汇编语言、高级语言:1.面向过程的语言:C 2.面向对象的语言:Java,OC,C++,C#,Python
类和对象
class 类 People 类名 ()里面为集成的对象,
object相当于祖类
对象经常使用的两部分:以人来举例
1.属性:名字 性别 身高 体重
2.方法:吃 睡 哭 编码
class People(object):
name = ''
sex = False
age = 0
height = 0
weight = ''
def eat(self):
print('人类出生就会吃东西')
def sleep(self):
print('人类天生会睡觉')
def work(self):
print('每个人都有劳动的权利')
# 创建一个对象 叫做p
p = People()
list = list()
dic = dict()
p.name = '小王'
p.age = 17
p.sex = False
p.height = 176
p.weight = '60kg'
print(p.name)
p.eat()
class People(object):
# 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.女生来判别男生,如果是男的,年龄相差不超过五岁,身上没有负债
则愿意和他在一起
class Human(
object):
def
__init__(
self, name=
'', age=
0, sex=
True, height=
0, money=
0):
self.name = name
self.age = age
self.sex = sex
self.height = height
self.money = money
def isFitMySelf(
self, other):
print(
self.name)
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(
'喜结良缘')p1 = Human(
'张生',
30,
False,
178,
1500000)p2 = Human(
'崔莺莺',
24,
False,
165, -
100000)p3 = Human(
'wxz',)
# Human.isFitMySelf(p1,p2)p1.isFitMySelf(p2)
总结
class People(object):
# 类属性
name = 'wxz'
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)
# 对象属性不能通过类名+属性的方式调用, 只能通过对象来调用
# 类属性可以通过类名+属性的方式调用, 也可以通过对象来调用
# print(People.fond)
p1.say()
# 对象方法可以通过对象 + 方法名这种形式来调用
# 也可以通过类名 + 方法名,将对象当成参数传入方法中 这种形式来调用
People.say(p1)
2.私有属性
class People(object):
def __init__(self, name='', sex='', age='', fond='学习'):
self.name = name
# 对象._属性 调用
self._sex = sex
# 对象._类名__属性 调用
self.__age = age
# 对象属性中,凡是带下划线的,都是不希望外部使用的(道德上)
# 但是并不是说我们不能使用
# 如果加的是单下划线 _ 可以通过p._name调用
# 若果加的是双下划线 __ 可以通过p._People__name调用
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 = '张三'
p._sex = '男'
print(p.name)
print(p._sex)
# p.__age = '17'
print(p._People__age)
p.fond = '开车'
print(p.fond)
# 若有
# p.girlFriend = '小美'
# print(p.girlFriend)
3. 继承
子类继承于父类
子类会有父类的属性和方法
子类也可以重写父类的属性和方法
类也可以拥有自己独有的属性和方法
class People(object):
def __init__(self,age='77',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()
class Boy(Man):
def __init__(self):
pass
m = Man('17', '男', '络腮胡')
print(m.age)
m.smoke()
m.eat()
面向对象编程的三大特点:
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()
4. 类方法和静态方法
class People(object):
# 类属性
count = 0
size = 0
def __init__(self,name='',age=''):
# 对象属性
self.name = name
self.age= age
# 对象方法
def say(self):
print('Hello')
# class 类 method 方法
@classmethod
def classFun(cls):
print('Hello,我是类方法')
# static 静态 method 方法
@staticmethod
# 不需要指定self或者cls来调用
def method():
print('我是静态方法')
People.classFun()
People.method()
p1 = People()
p1.classFun()
p1.method()
总结:
任何一种类型的方法都可以用类或者对象来调用
什么时候使用对象方法?什么时候使用了方法和静态方法?
1.在绝大部分情况下,我们方法都会声明成对象方法
2.如果我们希望用类来处理这个方法,或者不希望某一个属性值不因为对象,而改变时,就可以用类方法
3.静态方法的使用绝大部分都可以用实例方法或者类方法来替代
统计某一个类的对象 一共被创建了多少个
class FoodTemplate(object):
# 一旦对象被创建会自动调用这个方法
count = 0
def __init__(self):
print('创建了一次')
FoodTemplate.count += 1
pass
@classmethod
def myCount(cls):
print('一共创建了{}个'.format(FoodTemplate.count))
yueBing = FoodTemplate()
manTou = FoodTemplate()
FoodTemplate.myCount()
下面通过一个练习熟悉类的使用:
练习.男女相亲
男方对女方的要求为:
0.对方必须是女的
1.女方身高不小于165
2.女方年龄不能比自己大
3.女方腰围要比自己小
4.女方读的书不能少于100
女方对男方的要求为:
0.对方必须是男的
1.对方的年龄不能比自己小且不能大于自己10岁
2.对方的身高不能比自己小
3.对方腰围不能超过自己1.5倍
4.对方应有稳定收入,年薪不能少于20万
# 5.对方房子面积不能少于120,总价值不能少于200万
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
# 4
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
# 6
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
# 2
def makeFriendWithGirl(self ,other):
# 3
result = super(Man,self).sexIsFit(other)
if result == False :
return
# 5
result = super(Man,self).ageIsFit(other)
if result == False :
return
# 7
if other.height < 165 :
print('我喜欢个子高的,你很好,但是我们不合适')
return
# 8
if other.yaowei > self.yaowei :
print('我喜欢稍微瘦一点的')
return
# 9
if other.readCount < 100 :
print('好看的皮囊和有趣的灵魂我都喜欢')
return
# 10
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 = 35
jack.salary = 20000000
jack.house_area = 130
jack.house_value = 2000000
rose = Woman(name='rose',sex=False,age=16,height=167,yaowei=25,readCount=108)
# 1
# jack.makeFriendWithGirl(rose)
rose.makeFriendWithBoy(jack)
class People(object):
# 类属性
name = 'wxz'
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)
# 对象属性不能通过类名+属性的方式调用, 只能通过对象来调用
# 类属性可以通过类名+属性的方式调用, 也可以通过对象来调用
# print(People.fond)
p1.say()
# 对象方法可以通过对象 + 方法名这种形式来调用
# 也可以通过类名 + 方法名,将对象当成参数传入方法中 这种形式来调用
People.say(p1)