上次学习了函数的问题,今天我们了解一下Python语句的对象及类的属性和方法!
1,对象
举个例子
class People(object):
name = ''
sex = '男'
age = 2
height = ''
weight = ''
def eat(self):
print('人类出生就会吃东西')
def sleep(self):
print('人类天生就会睡觉')
def work(self):
print('每个人都有劳动的权利')
#创建一个对象 叫做p
p = People()
#类属性
p.name = '小王'
p.age = 15
p.sex = '女'
p.height = 185
p.weight = '70kg'
print(p.height)
p.eat()
如上:类和对象 群体 个体。其中class是类,People是类名,()里面为继承的对象
object 对象:物体 object相当于祖类
而对象又分为两部分
1,属性 例如:名字,性别,身高,体重
2,方法 例如:sleep eat cay coding
1.1 init
class People(object):
#init 初始化 魔术方法的一种 用来初始化对象
#init()方法会自动调用 在创建对象的时候
# init里面的属性 叫做对象属性
def __init__(self,name,age,sex):
#将后面的值赋予self**属性
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.0
# 1,,男生女生都属于人类,有身高,性别,年龄,姓名等属性
# 2,创建一个男生,叫张生。包括其他一些属性
# 3,创建一个女生,叫崔莺莺,包括其他一些属性
# 4,女生来判别男生 ,如果是男的,年龄相差不超过5岁,身上没有负债
# 则愿意跟他在一起
class Peole(object):
def __init__(self,name,age,sex,height,fuzhai):
self.name = name
self.sex = sex
self.age = age
self.height = height
self.fuzhai = fuzhai
nan = Peole('张生',25,'男',180,0)
nv = Peole('崔莺莺',23,'女',180,0)
if nan.sex == '男' and -5<=nan.age-nv.age <=5 and nan.fuzhai == 0:
print('愿意和他在一起')
else:
pass
class Peole(object):
def __init__(self,name= '',age = 0,sex = False,height = 120,money = 0):
self.name = name
self.sex = sex
self.age = age
self.height = height
self.money = money
def pipi(self,other):
if self.sex == other.sex:
print('再见')
return
if not -5<=self.age - other.age<=5:
print('告辞')
return
if other.money<0:
print('凉凉')
return
else:
print('nice')
nv = Peole('崔莺莺',17,False,165,1000000)
nan = Peole('张生',19,True,180,99999)
nv.pipi(nan)
2,对象总结
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)
如上,在输出各种结果时 我们总结出结果
1,对象属性不能通过类名加属性的方式来调用 只能通过对象来调用
2,类属性可以通过类名加属性的方试来调用 也可以通过对象来调用
3,对象方法可以通过对象+方法名 这种形式调用
4,也可以通过类名 + 方法名 然后将对象当成参数传入方法中这种形式来调用
3,私有属性
有些代码不愿被别人爬取 所以将信息代码稍作处理 即有些代码变成私有属性
#对象属性中 凡是带下划线的 都是不希望外部使用(道德约束)
#如果加的单下划线 可以通过 p1.name = 'dasdsa'这种方式实现
#如果加的是双下划綫 可以通过 pi.name = p._peole__name
class People(object):
def __init__(self,name = '',sex = '',age = '15',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 = '张三'
p.sex = '男'
print(p.name)
print(p.sex)
# p.__age = 17
print(p._People__age)
# 如果有这个属性 则修改属性值 如果没有这个属性 则添加
p.pipi = '猪'
print(p.pipi)
# p.fond = '吃饭'
# print(p.fond)
p.fond('吃饭')
4,继承
关于继承 ,就是字面意思即:
1,子类继承于父类
2,子类会有父亲的属性和方法
3,子类也可以拥有重写父类的属性和方法
4,子类也可以拥有自己独有的属性和方法
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 = '' , jiba = ''):
# 继承父类已有的属性
super(Man,self).__init__(age,sex)
def luguan(self):
print('撸管使人快乐')
def eat(self):
super(Man,self).eat()
print('上面这句话是继承自people的,正在说的这句话才是我自己的')
class Boy(Man):
def __init__(self):
pass
m = Man('17','男','18cm')
print(m.age)
m.luguan()
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): #吧b,c谁在前输出谁
pass
d = D()
d.say()
5,类方法和静态方法
任何一种类型的方法 都可以用类或者对象来调用
问:什么时候使用对象方法,什么时候使用类方法和静态方法
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('hai')
#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()
# + viod MyFun:(int) age
p1.say()
People.say(p1)
统计 : 某一和类的对象 一共被创建了多少次
class a (object):
count = 0 #类属性
# 一但对象被创建会自动调用这个方法
def __init__(self):
print('创建了一次')
a.count +=1
pass
@classmethod
def mycount(cls):
print('一共被创建了{}个'.format(a.count))
yuebing = a()
a.mycount()
练习2.0
# 男女相亲
# 男方对女方的要求 0,必须是女的1,女方身高不小于165 2,年龄不能比自己大 3,腰围比自己小4,女方读的书不能小于100
# 女对男 :0,必须是男的 2,年龄比自己小 3,身高不能比自己矮 4,腰围不超过1.5倍 1,年薪不小于20w 5,房子面积不能小于120 总价值不能小于200w
class People (object):
def __init__(self,age = '',sex = '',height = '',weight = ''):
self.age = age
self.sex = sex
self.height = height
self.weight = weight
class Man(People):
def __init__(self, age='', sex='',height = '',weight = '',money = '',house = '',housemoney = ''):
self.money = money
self.house = house
self.housemoney = housemoney
super(Man,self).__init__(age,sex,height,weight)
def pipi(self, other):
if self.sex == other.sex:
print('刚交的朋友?')
return False # 在此代表相亲失败
if self.age < other.age:
print('大姐你好')
return
if other.height < 165:
print('腿能让我摸一摸吗')
return
if self.weight < other.weight:
print('先吃饭?')
return
if other.book < 100:
print('对不起,我不和不爱读书的人说话')
else:
print('饥渴难耐')
class WoMan(People):
def __init__(self, age='', sex='', height='', weight='',book = ''):
self.book = book
super(WoMan,self).__init__(age,sex,height,weight)
def pipi(self,other):
if self.sex == other.sex:
print('同性交友?')
return
if self.age < other.age and other.age-self.age>=10:
print('出去')
return
if self.height>other.height:
print('滚出去')
return
if self.weight*1.5<=other.weight:
print('再见')
return
if other.money<200000:
print('告辞')
return
if other.house<=120 and other.housemoney<=2000000:
print('你是?')
return
else:
print('准备好被一套带走了吗')
man = Man(25,'男',185,24,100000000,300,80000000)
woman = WoMan(20,'女',175,18,500)
man.pipi(woman)
woman.pipi(man)
今天所学大概如此!!
今天叕是元气满满的一天! 明天继续加油干!!!!!!!!!!