【Python 进阶1】面向对象编程

创建类

面向对象概念思想几乎都一样,但是 Python 代码可能和 C++ 大不相同

创建员工类

class Employee:
    '所有员工的基类'
    empCount = 0
    __Count = 0 # 私有属性
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1
        
    def displayCount(self):
        print("Total Employee %d" % Employee.empCount)
        
    def displayEmployee(self):
        print("Name : ", self.name, ", Salary: ", self.salary)
  • empCount 变量是一个类变量,它的值将在这个类的所有实例之间共享,在内部类或外部类使用 Employee.empCount 访问

  • init() 被称为类的构造函数或初始化方法,功能和 C++ 中相同

  • self 代表类的实例,self 在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数,类似于 C++ 的 this 指针

类的专有方法解释
__init__:构造函数,在生成对象时调用
__del__ :析构函数,释放对象时使用
__repr__ :打印,转换
__setitem__ :按照索引赋值
__getitem__:按照索引获取值
__len__:获得长度
__cmp__:比较运算
__call__:函数调用
__add__:加运算
__sub__:减运算
__mul__:乘运算
__truediv__:除运算
__mod__:求余运算
__pow__:乘方
__str__:返回对象的描述信息

创建实例对象

以下使用类的名称 Employee 来实例化,并通过 init 方法接收参数。

if __name__ == '__main__':
    emp1 = Employee("马泽","10元")
    emp2 = Employee("maze","20元")
    emp1.displayEmployee()
    emp2.displayEmployee()

类的继承

单继承,重写父类方法

class people:
   name = '' 
   age = 0
   __weight = 0 # 私有
   # 构造方法
   def __init__(self,n,a,w):
       self.age = a
       self.name = n
       self.__weight = w
   def __str__(self):
       return str(self.age)+':'+str(self.name)
   def speak(self):
       print("%s说:我 %d 岁了" %(self.name,self.age))

# 单继承
class student(people):
    grade = ''
    def __init__(self,n,a,w,g):
        people.__init__(self,n,a,w)
        self.grade = g
    def speak(self):
        print("%s说:我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))

多继承

# 定义另外一个类,多继承准备工作
class speaker():
    topic = ''
    name = ''
    def __init__(self,n,t):
        self.topic = t
        self.name = n
    def speak(self):
        print("我叫%s,我今天演讲的题目是:%s"%(self.name,self.topic))

# 多继承,谁在前面,则优先调用对应的重写方法,这里优先调用 speaker 
class sample(speaker,student): 
    a = ''
    def __init__(self,n,a,w,g,t):
        speaker.__init__(self,n,t)
        student.__init__(self,n,a,w,g)

方法重写

class Parent:
    def myMethod(self):
        print('调用父类方法')

class Child(Parent):
    def myMethod(self):
        print('调用子类方法')

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    c = Child()
    c.myMethod()
    super(Child,c).myMethod() #用子类对象调用父类已被覆盖的方法

多继承 super 的用法,super 调用的特殊情况

运算符重载

对类的专有方法进行重载

class Vector:
    def __init__(self,a,b):
        self.a = a
        self.b = b
    def __str__(self):
        return 'Vector (%d,%d)'%(self.a,self.b)

    def __add__(self, other):
        return Vector(self.a+other.a,self.b+other.b)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    v1 = Vector(1,2)
    v2 = Vector(1,2)
    print(v1+v2)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的温柔香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值