私有化
#私有化
#封装:1.私有化属性
# 2.定义公有的set和get方法
# 3.只能在类内访问
#好处:1.隐藏属性不被外界随意修改
# 2.类内通过提供公有方法来修改
# 3.在方法内可做逻辑处理
class Student:
#__age=18 #类属性
def __init__(self,name,age):#每创建一个对象都会调用这个方法,在该方法内初始化对象属性,使得每个对象有该属性
#对象属性
self.name=name
self.age=age
self.__score=59#私有属性只能在类内部访问,一般需要在类内提供公有方法来给外界访问或修改
def setScore(self,score):
if score>=0 and score<=100:
self.__score=score
else:
print('分数不在规定范围内')
def getScore(self):
return self.__score
def __str__(self):#self表示当前对象
return '姓名:{},年龄{},考试分数{}'.format(self.name,self.age,self.__score)
yupeng=Student('宇鹏',18)
print(yupeng)
#姓名:宇鹏,年龄18,考试分数59
yupeng.age=21
yupeng.__score=90 #私有属性改变不了,不报错但也不生效
print(yupeng)
#姓名:宇鹏,年龄21,考试分数59
print(yupeng.getScore())
yupeng.setScore(99)
print(yupeng.getScore())
print(yupeng)
'''
59
99
姓名:宇鹏,年龄21,考试分数99
'''
class Student:
__score=99 #类属性
def __init__(self,name,age):#每创建一个对象都会调用这个方法,在该方法内初始化对象属性,使得每个对象有该属性
#对象属性
self.__name=name
self.__age=age
def setName(self,name):
if len(name)==6:
self.__name=name
else:
print('名字必须6位')
def getName(self):
return self.__name
def __str__(self):#self表示当前对象
return '姓名:{},年龄{}'.format(self.__name,self.__age)
s=Student('宇鹏',18)
print(s)
print(dir(Student))
print(dir(s))#多了一些对象属性出来
#dir可以看到的东西是我可以调用的
私有化装饰器@property
#在开发中看到一些私有化处理:装饰器
class Student:
__score=99 #类属性
def __init__(self,name,age):#每创建一个对象都会调用这个方法,在该方法内初始化对象属性,使得每个对象有该属性
#对象属性
self.name=name
self.__age=age
@property
def age(self):
return self.__age
@age.setter#age是上边定义的函数,因此必须先有get,再有set
def age(self,age):
if age > 0 and age < 100:
self.__age=age
else:
print('不在规定范围内')
# def setAge(self,age):
# if age>0 and age<100:
# self.__age=age
# else:
# print('不在规定范围内')
# def getAge(self):
# return self.__age
def __str__(self):#self表示当前对象
return '姓名:{},年龄{}'.format(self.name,self.__age)
s=Student('peng',20)
s.name='xiaopeng'
print(s.name)
#用装饰器前
# s.setAge(30)
# print(s.getAge())
#用装饰器后
print(s.age)#找age函数,但又不需要加括号
s.age=30
print(s.age)
类之间的关系
#类与类之间的关系 is a,has a(包含)
'''
公路(Road):
属性:公路名称,公路长度
车(Car):
属性:车名,时速
方法:1.求车名在哪条公路上以多少的时速行驶了多长,
get_time(self,road)
2.初始化车属性信息__init__方法
3.打印对象显示车的属性信息
'''
import random
class Road:
def __init__(self,name,len):
self.name=name
self.len=len
class Car:
def __init__(self,brand,speed):
self.brand=brand
self.speed=speed
def get_time(self,road):
ran_time=random.randint(1,10)
msg='{}品牌的车在{}上以{}速度行驶{}小时'.format(self.brand,road.name,self.speed,ran_time)
print(msg)
def __str__(self):
return '{}品牌的,速度:{}'.format(self.brand,self.speed)
road=Road('京藏高速',12000)
car=Car('奥迪',120)
car.get_time(road)
print(car)
关联
#has a
#student book computer
'''
知识点:
1.has a
一个类中使用了另外一种自定义的类型
student中使用了computer book
2.类型:
系统类型:
str int float
list dict tuple set
自定义类型:
student book computer
'''
class Computer:
def __init__(self,brand,color):
self.brand=brand
self.color=color
def online(self):
print('正在使用电脑上网...')
def __str__(self):
return self.brand+"--"+self.color
class Book:
def __init__(self,bname,author,num):
self.bname=bname
self.author=author
self.num=num
def __str__(self):
return self.bname+"--"+self.author+"--"+str(self.num)
class Student:
def __init__(self,name,computer,book):
self.name=name
self.computer=computer
self.books=[]#已借的书
self.books.append(book)
def borrow_book(self, book):
for book1 in self.books:
if book1.bname==book.bname:
print('已经借过此书!')
break
else:
self.books.append(book)
print('借阅成功')
def show_book(self):
for book in self.books:
print(book.bname)
def __str__(self):
return self.name+"--"+str(self.computer)+"--"+str(self.books)
computer=Computer('mac','白色')
book=Book('恶意','东野圭吾',12)
student=Student('小王',computer,book)
print(student)
#小王--mac--白色--[<__main__.Book object at 0x0000013C926F8828>]
student.show_book()
book1=Book('盗墓笔记','南派三叔',12)
print('开始借书----------------')
student.borrow_book(book1)
print('----------------')
student.show_book()
'''
恶意
开始借书----------------
借阅成功
----------------
恶意
盗墓笔记
'''
继承
#is a
#如果A是B,那么就称B是A的基类
'''
继承:Student,Doctor-->都属于人,都有相同的属性和行为
'''
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print(self.name+'正在吃饭...')
def run(self):
print(self.name+'正在跑步...')
class Student(Person):#继承父类Person的所有的方法
pass
class Doctor(Person):
def __init__(self,name,age):
print('----doctor自己的init')
super().__init__(name,age)
class Employee(Person):
def __init__(self,name,age,department,salary):
super().__init__(name,age)
self.department=department#自己独有的属性
self.salary=salary
#继承下来的东西也可以用self调用,也是该对象的一部分,但实质上他只是引用了父类的东西
def doWork(self):
print('员工{}正在做工作,它的薪资为{}'.format(self.name,self.salary))
stu=Student('大王',48)
stu.run()
stu=Student('小王',18)
stu.run()
'''
大王正在跑步...
小王正在跑步...
'''
doct=Doctor('小惠',18)
doct.eat()
'''
----doctor自己的init
小惠正在吃饭...
'''
emplo=Employee('关欣',25,'技术部',9000)
emplo.run()
emplo.doWork()
'''
关欣正在跑步...
员工关欣正在做工作,它的薪资为9000
'''
继承总结
1、如果我的类有一个eat方法是有参的,而父类的eat无参,—重写。会默认先找当前类再去找父类,但此时他与java不一样,即使参数个数不一样,他也不会往父亲找了,因此此时你调用这个类的eat方法,必须有参。
2、优先找子类,再找父类,如果子类没有__init__,则去调用父类的
3、如果类继承了父类也自己定义了__init__,就需要在当前类的init中调用父类的__init__。
super().init(参数)
super(类名,对象).init(参数)
4、子类调用父类方法
super().eat()
练习
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print(self.name+'正在吃饭...')
def run(self):
print(self.name+'正在跑步...')
class Student(Person):
def __init__(self,name,age):
super().__init__(name,age)
def eat(self,food):
super().eat()
print('{}太好吃了'.format(food))
def study(self,course):
print('{}正在学习{}课程'.format(self.name,course))
s=Student('xiao',12)
s.eat('鸡腿')
'''
xiao正在吃饭...
鸡腿太好吃了
'''
python没有重载
class Person:
def __init__(self,name):
self.name=name
def eat(self):
print('--->eat1')
def eat(self,food):
print('---->eat:',food)
#python不存在重载,后面出现同名的函数会覆盖前面的
#因此这里的无参函数是调用不了的
p=Person('jack')
p.eat('番茄')
python可以多继承
#多继承,pyhton可以,java不可
class A:
def test(self):
print('---->AAAAAAa')
class B:
def test1(self):
print('----->BBBBBBBBBB')
def same(self):
print('----->B-same')
class C(A,B):
def test2(self):
print('-------->CCCCCCC')
def same(self):
print('----->c-same')
c=C()
c.test()
c.test1()
c.test2()
c.same()
'''
---->AAAAAAa
----->BBBBBBBBBB
-------->CCCCCCC
print('----->c-same')
'''
#多继承,pyhton可以,java不可
class Base:
def same(self):
print('---Base-----')
class A(Base):
def test(self):
print('---->AAAAAAa')
class B(Base):
def test1(self):
print('----->BBBBBBBBBB')
def same(self):
print('----->B-same')
class C(Base):
def test2(self):
print('-------->CCCCCCC')
def same(self):
print('----->c-same')
class D(A,B,C):
pass
d=D()
d.same()
import inspect
print(inspect.getmro(D))
#查看这个类的查找顺序----广度优先,一旦在这些类中找到要的方法就停止
'''
----->B-same
(<class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.Base'>, <class 'object'>)
'''
'''
如果父类中有相同名称的方法,搜索顺序是
'''