Python - 面向对象

一、方法

内置函数:https://docs.python.org/zh-cn/3/library/functions.html

方法的声明与调用

def sayHello():
    print("Hello")

sayHello()

方法传参

def eat(fruit):
    print("Eat "+ fruit)

eat("Apple")


#指定参数默认值
def eat(fruit="Apple"):
    print("Eat " + fruit)

eat()

定义返回值

通过return返回值,可以返回多个

如果函数没有定义return返回值,则返回值默认为None

def sum(a,b):
    return a+b

print(sum(1,2))

#返回多个变量
def sum(a,b):
    return a+b ,a-b

print(sum(1,2)[0]) #返回的是个元组
print(sum(1,2)[1])

递归函数

def test(n):
    n+=1
    print(n)
    test(n)

test(0)

#RecursionError: maximum recursion depth exceeded while calling a Python object
#到九百多后旧会报错
#因为每个函数在调用自己的时候,还没有退出,占内存,多了肯定会导致内存崩溃.
#在计算机中,函数调用是通过栈(stack)这样数据结构实现的,每当进入一个函数调用,
#栈就会加一层栈帧,每当函数返回,栈就会少一层栈帧.由于栈的大小不是无限的,所以,递归调用次数多了,会导致栈溢出.

匿名函数

sum = lambda a,b:a+b
print(sum(1, 2))

高阶函数

将函数作为参数进行传参

def test(str1,method):
    method(str1)

test("test",print)

装饰器

在不修改原函数的前提下为函数增加附加功能

装饰器的实现为:嵌套函数+高阶函数

def auth(func):
    def wrapper():
        print('auth')
        func()
    return wrapper

@auth
def f1():
    print("f1 called")

f1()
#======================#
# @auth 等价于下
f2 = auth(f1)
f2()

二、类

类的声明与实例化

#1.声明类
class People:                
    pass

#2.实例化类
p1 = Perple()

#3.添加属性
p1.name="Tiger"
p1.age=22

构造函数

class People:
    #构造方法,类被实例化时调用
    #第一个参数必须是 self ,代表实例本身
    def __init__(self, name, age):
        self.name=name
        self.age=age

p1 = People("Tiger",22)

print(p1.name,p1.age)

添加类变量和方法

class People:
    country = "China"
    def __init__(self, name, age):
        self.name=name
        self.age=age
    def printInfo(self):
        print("My name is {}. I am {} years old. I am from {}".format(self.name,self.age,self.country))

p1 = People("Tiger",22)
p1.printInfo()

私有化

用于定义私有属性、私有方法

定义语法:直接在最开始加__

私有化目的:提高安全性,只允许类内部调用

class People:
    __country = "China"
    def __init__(self, name, age):
        self.name=name
        self.age=age
    def __printInfo(self):
        print("My name is {}. I am {} years old. I am from {}".format(self.name,self.age,self.__country))

p1 = People("Tiger",22)
p1.__printInfo()  #执行会报错
print(People.__country) #执行会报错

其他

class Person():
    #对象初始化时调用
    def __init__(self, name):
        self.name = name
    # print对象时调用
    def __str__(self):        
        return "I was printed"
    #对象销毁时调用
    def __del__(self):
        print("I was destroyed")

三、类继承

声明

class Parent(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printInfo(self):
        print("My name is {}. I am {} years old.".format(self.name, self.age))

class Child(Parent):
    pass

p1 = Child("Name1", 22)
p1.printInfo()

重写方法构造函数

class Parent:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printInfo(self):
        print("I am Parent")


class Child(Parent):
    def __init__(self, name, age, phone):
        #Parent.__init__(self, name, age)
        #super(Child,self).__init__(name,age)
        super().__init__(name, age)  #推荐写法
        self.phone = phone

    def printInfo(self):
        print("I am Child")


p1 = Child("Name1", 22, "123")
p1.printInfo()
print(p1.phone)

多层继承

class Grandfather(object):
    def house(self):
        print("I have a house")

class Father(Grandfather):
    def car(self):
        print("I have a car")

class Child(Father):
    pass

c = Child()
c.house()
c.car()

多重继承

class Father(object):
    def sing(self):
        print("I can sing")

class Mother(object):
    def dance(self):
        print("I can dance")

class Child(Father, Mother):
    pass

c = Child()
c.sing()
c.dance()
Last modification:February 20th, 2020 at 03:52 pm