Python学习
方法没有重载
python中,方法的参数没有生命类型(调用时确定参数的类型),参数的数量也可以由可变的控制参数,因此python中时没有方法重载的,定义一个方法即可由多种调用方式,想当于实现了其他语言中方法重载
方法的动态性,
下面是Demo
#测试方法的动态性
class Person:
def work(self):
print('努力上班')
def play_game(s):
print('{0}再玩游戏'.format(s))
def work2(s):
print('好好工作,努力上班,挣大钱,娶媳妇')
Person.play = play_game
p = Person()
p.work()
p.play()
Person.work = work2
p.work()
E:\python\python37\python.exe E:/python/PycharmProjects/pythonProject3/方法的动态性.py
努力上班
<__main__.Person object at 0x00000198C36BF188>再玩游戏
好好工作,努力上班,挣大钱,娶媳妇
Process finished with exit code 0
私有属性
私有和属性方法,由如下要点:
1.通常我们约定,两个下划线开头的属性是私有的(private),其他为公共的(public)
2.类内部可以访问私有属性(方法)
3.类外部不能直接访问私有属性(方法)
4.类外部可以通过_类名_私有属性(方法)名访问私有属性
注:方法本质上也是属性,只不过是可以通过()执行而已,所以,此处将的私有属性和共有属性,也同时讲解了私有方法和共有方法
下面是Demo
#测试私有属性
class Employee:
def __init__(self,name,age):
self.name =name
self.__age =age#增加两个下划线表示成为私有属性
e = Employee('高崎',18)
print(e.name)
print(e._Employee__age)
print(dir(e))
E:\python\python37\python.exe E:/python/PycharmProjects/pythonProject3/私有属性和私有方法.py
高崎
18
['_Employee__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
Process finished with exit code 0
私有方法
python中,一切都是对象,实际上,执行def定义函数后,系统就创建了相应的函数对象。
下面是Demo
#测试私有属性
class Employee:
__company ='百战程序员'
def __init__(self,name,age):
self.name =name
self.__age =age#增加两个下划线表示成为私有属性
def __wrok(self):#私有方法
print('好好工作,赚钱娶媳!')
e = Employee('高崎',18)
print(e.name)
print(e._Employee__age)
print(dir(e))
e._Employee__wrok()
print(Employee._Employee__company)
E:\python\python37\python.exe E:/python/PycharmProjects/pythonProject3/私有属性和私有方法.py
高崎
18
['_Employee__age', '_Employee__company', '_Employee__wrok', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
好好工作,赚钱娶媳!
百战程序员
Process finished with exit code 0
@property装饰器
@property装饰器可以将一个方法调用方式变成,属性调用
下面是Demo
#@property装饰器的测试
class Employee:
@property
def salary(self):
print('salay run,,,')
return 10000
emp1 = Employee()
emp1.salary
print(emp1.salary)
E:\python\python37\python.exe E:/python/PycharmProjects/pythonProject3/@property装饰器.py
salay run,,,
salay run,,,
10000
Process finished with exit code 0
@property装饰器方法
下面是Demo
#@property装饰器的方法
class Employee1:
def __init__(self,name,salary):
self.__name = name
self.__salary = salary
@property
def salary(self):
return self.__salary
@salary.setter
def salary(self,salary):
if 1000 < salary < 50000:
self.__salary = salary
else:
print('录入错误!薪水在1000-50000这个范围')
emp2 = Employee1('高崎', 30000)
emp2.salary = 2000
print(emp2.salary)
E:\python\python37\python.exe E:/python/PycharmProjects/pythonProject3/@property装饰器.py
2000
Process finished with exit code 0
#@property装饰器的方法1
class Employee1:
def __init__(self,name,salary):
self.__name = name
self.__salary = salary
def get_salary(self):
return self.__salary
def set_salary(self, salary):
if 1000 < salary < 50000:
self.__salary = salary
else:
print('录入错误!薪水在1000-50000这个范围')
emp2 = Employee1('高崎', 30000)
print(emp2.get_salary())
emp2.set_salary(2000)
print(emp2.get_salary())
E:\python\python37\python.exe E:/python/PycharmProjects/pythonProject3/@property方法.py
30000
2000
Process finished with exit code 0
面向对象三大特征
1.封装(隐藏):隐藏对象的属性和细节,对外只提供必要的方法,相当于将细节封装起来,只对外暴露相关的方法,通过前面学习的私有属性,私有方法的方式,实现封装,python追求简洁的语法,没有严格的语法级别的访问控制符,更多的是依靠程序员自觉实现
2.继承:继承可以让子类具有父亲的特性,提高了代码的重用性,从设计上是一种增量进化,原有父类设计不变的情况下,可以增加新的功能,或者改进已有的算法
3.多态:多态是指一个方法调用由于对象不同会产生不同的行为,生活中这样的例子比比皆是:同样是休息方法,不不同休息方法不同,张三休息是睡觉,李4休息是玩游戏,程序员休息是敲几行代码
下面是Demo
继承
继承是面向对象程序设计的重要特征,也是实现代码复用的重要手段,如果一个新类继承自一个设计好的类,就直接具备了已有类的特征,就大大降低了工作难度,已有的类,我们称之为父类或者基类,新的类,我们称为子类或者派生类
下面是Demo
#测试继承的基本使用
class person:
def __init__(self,name,age):
self.name = name
self.__age = age#不能调用私有属性
def say_age(self):
print('年龄,年龄,我也不知道')
class student(person):
def __init__(self,name,age,score):
person.__init__(self,name,age)#必须显示的调用父类初始化方法,不然解释器不会去调用
self.score = score
pass
print(student.mro())
s =student('高崎',18,60)
s.say_age()
print(s.name)
print(dir(s))
print(s._person__age)
E:\python\python37\python.exe E:/python/PycharmProjects/pythonProject3/继承.py
[<class '__main__.student'>, <class '__main__.person'>, <class 'object'>]
年龄,年龄,我也不知道
高崎
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_person__age', 'name', 'say_age', 'score']
18
Process finished with exit code 0