2

Python 面向对象 - ThankCAT

 1 year ago
source link: https://www.cnblogs.com/thankcat/p/17013616.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python 面向对象

方法没有重载#

在其他语言中,可以定义多个重名的方法,只要保证方法签名唯一即可。方法签名包含3个部分:方法名、参数数量、参数类型。

Python 中,方法的的参数没有声明类型(调用时确定参数的类型),参数的数量也可以由
可变参数控制。因此,Python 中是没有方法的重载的。定义一个方法即可有多种调用方式,相当于实现了其他语言中的方法的重载。

如果我们在类体中定义了多个重名的方法,只有最后一个方法有效。

建议:不要使用重名的方法!Python 中方法没有重载。

#Python 中没有方法的重载。定义多个同名方法,只有最后一个有效

class Person:

    def say_hi(self):
        print("hello")

    def say_hi(self,name):
        print("{0},hello".format(name))

p1 = Person()

#p1.say_hi() #不带参,报错:TypeError: say_hi() missing 1 required positional argument: 'name' 

p1.say_hi("陈浩")

方法的动态性#

Python 是动态语言,我们可以动态的为类添加新的方法,或者动态的修改类的已有的方法。

#测试方法的动态性
class Person:
    def work(self):
        print("努力上班!")

def play_game(self):
    print("玩游戏")

def work2(self):
    print("好好工作,努力上班!")

Person.play = play_game
Person.work = work2
p = Person()

p.play()    # 玩游戏
p.work()    # 好好工作,努力上班!

我们可以看到,Person 动态的新增了 play_game 方法,以及用 work2 替换了 work 方法。

私有属性和私有方法(实现封装)#

Python 对于类的成员没有严格的访问控制限制,这与其他面向对象语言有区别。关于私有属性和私有方法,有如下要点:

  1. 通常我们约定,两个下划线("__")开头的属性是私有的(private)。其他为公共的(public)。
  2. 类内部可以访问私有属性(方法)
  3. 类外部不能直接访问私有属性(方法)
  4. 类外部可以通过“_类名__私有属性(方法)名”访问私有属性(方法)

【注】方法本质上也是属性!只不过是可以通过()执行而已。所以,此处讲的私有属性和公有属性,也同时讲解了私有方法和公有方法的用法。如下测试中,同时也包含了私有方法和公有方法的例子。

【测试】私有属性和公有属性使用测试

#测试私有属性、私有方法
class Employee: 
    __company = "Danone" #私有类属性. 通过 dir 可以查到_Employee__company
    
    def __init__(self, name, age):
        self.name = name
        self.__age = age #私有实例属性

    def say_company(self):
        print("我的公司是:",Employee.__company) #类内部可以直接访问私有属性
        print(self.name,"的年龄是:",self.__age)
        
    def __work(self): #私有实例方法 通过 dir 可以查到_Employee__work
        return "工作!好好工作,好好赚钱,娶个媳妇!"

p1 = Employee("陈浩",18)

print(p1.name) 
# 陈浩

print(dir(p1)) 
# ['_Employee__age', '_Employee__company', '_Employee__work', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'say_company']

p1.say_company()
# 我的公司是: Danone
# 陈浩 的年龄是: 18

print(p1._Employee__age) #通过这种方式可以直接访问到私有属性 。通过 dir 可以查到属性:_Employee__age
# 18

print(p1._Employee__work())
# 工作!好好工作,好好赚钱,娶个媳妇!

#print(p1.__age) #直接访问私有属性,报错
#print(p1.__work()) #直接访问私有方法,报错

@property 装饰器

@property 可以将一个方法的调用方式变成“属性调用”

class Employee:

    def __init__(self, name, salary):
        self.name = name
        self.__salary = salary


    @property               #getter方法 
    def salary(self):
        return self.__salary

    @salary.setter          #setter方法
    def salary(self, salary):
        self.__salary = salary

    @salary.deleter         #deleter方法
    def salary(self):
        self.__salary = 0



emp = Employee("陈浩", 6000)
print("{0}当前的薪资是{1}".format(emp.name,emp.salary))
# 陈浩当前的薪资是6000

print("*"*12, "加薪啦", "*"*12)
emp.salary = 10000
print("{0}当前的薪资是{1}".format(emp.name,emp.salary))
# 陈浩当前的薪资是10000

print("*"*12, "离职了", "*"*12)
del emp.salary
print("{0}当前的薪资是{1}".format(emp.name,emp.salary))
# 陈浩当前的薪资是0

@property 主要用于帮助我们处理属性的读操作、写操作。对于某一个属性,我们可以直接通过类名.方法名 = 值,进行赋值操作。

属性和方法命名总结#

  1. _xxx:保护成员,不能用“from module import * ”导入,只有类对象和子类对象能访问这些成员。
  2. __xxx__:系统定义的特殊成员
  3. __xxx: 类中的私有成员,只有类对象自己能访问,子类对象也不能访问。(但,在类外部可以通过“对象名. _类名__xxx”这种特殊方式访问。Python 不存在严格意义的私有成员)

注:再次强调,方法和属性都遵循上面的规则


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK