python学习笔记-面向对象设计

1、类的定义

分为经典类和新式类

#经典类:
class 类名:
    pass
    
#新式类:
class 类名(父类): # class 类名(object)
    pass

大前提:

1、只有在python2中才分新式类和经典类,python3中统一都是新式类
2、新式类和经典类声明最大不同在于,所有新式类必须继承至少一个父类
3、所有类不管是否显式声明父类,都有一个默认继承object父类

2、类的属性

2.1划分

类是用来描述一类事物,类的对象是指这一类事物的一个个体。是事物就有属性,属性分为

1、数据属性:就是变量
2、函数属性:就是函数,就是在类中的方法

类和对象均用.来访问自己的属性

2.2访问方式

查看类属性有2种方式

dir(类名):查出的是一个属性名字列表
类名.__dict__:查出的是一个字典

class Chinese:
    work = "程序员"

    def sleep():
        print("晚上12点睡觉")

    def eat(self):
        print("点好外卖,送餐中")


print(Chinese.work)  # 加点本质上就是查__dict__的属性字典。
Chinese.sleep()
Chinese.eat(1)

#原理如下 print(Chinese.__dict__) # 查看类的属性字典 #结果为:{'__module__': '__main__', 'work': '程序员', 'sleep': <function Chinese.sleep at 0x0088E1E0>, 'eat': <function Chinese.eat at 0x0088E270>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} print(Chinese.__dict__["work"]) Chinese.__dict__["sleep"]()#通过属性字典找到该函数属性的内存地址,然后加()运行该函数 Chinese.__dict__["eat"](1) # 类的其他属性 print(Chinese.__name__) # Chinese 类Chinese的名字(字符串) print(Chinese.__doc__) #None 文档字符串 print(Chinese.__module__) #__main__ 模块 print(Chinese.__base__) #<class 'object'> 类的第一个父类 print(Chinese.__bases__) # (<class 'object'>,) 类的所有父类构成的元组 print(Chinese.__dict__) # 类的属性 print(Chinese.__class__) #<class 'type'> 实例对应的类(仅新式类中)

3、面向对象的程序设计

例1

def Chinese(name,age,sex):

    def init(name,age,sex):
        dic={"name":name,
             "age":age,
             "sex":sex,
             "sleep":sleep,
             "eat":eat
        }
        return dic

    def sleep():
        print("晚上12点睡觉")

    def eat(dic):
        print("%s点好外卖,送餐中" %dic["name"])

    return init(name,age,sex)

p1=Chinese("steven","25","")
p1["sleep"]() #晚上12点睡觉
p1["eat"](p1)#steven点好外卖,送餐中

例2

class Chinese:
    work = "程序员"

    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex

    def sleep():
        print("晚上12点睡觉")

    def eat(self):#self就是实例自己
        print("%s点好外卖,送餐中" %self.name)


p1=Chinese("steven","25","")#相当于p1=Chinese.__init__(p1,"袁浩","18","female")
#p1.sleep() 报错,不需要传参数却传了个参数。因为class自动处理相当于执行了p1.sleep(p1),因此,类中的方法在创建的时候要传一个参数self。如def sleep(self)
p1.eat()

print(p1.__dict__)      #显示实例的属性字典
print(Chinese.__dict__) #显示类的实例字典
# print(p1.name)
# print(p1.__dict__["name"])

#Chinese.sleep()     #运行表示是类调用方法,跟实例没关系
#Chinese.eat(p1)

执行结果为:

steven点好外卖,送餐中
{'name': 'steven', 'age': '25', 'sex': '男'}
{'__module__': '__main__', 'work': '程序员', '__init__': <function Chinese.__init__ at 0x00E3E1E0>, 'sleep': <function Chinese.sleep at 0x00E3E270>, 'eat': <function Chinese.eat at 0x00E3E2B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}  

总结:

1、两个例子都应用了面向对象的思想。使用class定义类不代表一定是面向对象,用面向对象编程语言编程不一定就是面向对象,用非面向对象语言也能实现面向对象,面向对象是一种设计思想

2、例2对比例1,使用__init__方法,相当于自动生成了属性字典并返回,就不需要return。实例化时会触发该函数运行

3、实例就只有数据属性,没有函数属性

4、属性的增删改查

#====== 类属性的增删改查=====
class Chinese:
    work = "程序员"

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

    def play_ball(self,ball):
        print("%s 正在打 %s" % (self.name, ball))


# 查询
print(Chinese.work)
# 修改
Chinese.work = '复仇者'
print(Chinese.work)
# 增加
Chinese.country = "china"
print(Chinese.country)
# 删除
print(Chinese.__dict__)
del Chinese.country
print(Chinese.__dict__)


# 增加类的函数属性
def eat_foot(self, food):
    print("正在吃%s" % food)

Chinese.eat = eat_foot
print(Chinese.__dict__)

# 修改类的函数属性(重新增加一个属性,覆盖要修改的属性)
#

# =====实例属性的增删改查=====
p1 = Chinese("steven")
print(p1.__dict__)
# 查看
print(p1.name)
print(p1.play_ball)

# 增加实例的数据属性
p1.age = 18
print(p1.__dict__) #{'name': 'steven', 'age': 18}
print(p1.age)


# 增加实例的函数属性。(一般不这么用)
def test(self):
    print("来自实例的函数属性")
p1.test = test
print(p1.__dict__)
#p1.test()  # 这里会报错,提示缺少请求参数。实例只有在调用类的函数属性时才自动加上self参数。

# 修改
p1.age = 19
print(p1.__dict__)
print(p1.age)

# 删除
del p1.age
print(p1.__dict__)

5、作用域

#-----场景1-----
class Chinese:
    work = "程序员"

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

def play_ball(self, ball):
    print("%s 正在打 %s" % (self.name, ball))

p1 = Chinese("steven")
print(p1.work)

p1.work = "医生"
print(Chinese.work)  # "程序员"
print(p1.work)  # "医生"

#-----场景2-----
work = "程序员"
class Chinese:

    def __init__(self,name):
        self.name = name
        print("---->",work)

    def play_ball(self, ball):
        print("%s 正在打 %s" % (self.name, ball))

p1 = Chinese("steven")  # 执行结果为"----> 程序员"。如果类或实例加点进行访问,访问的只能是类或实例的作用域。这里没加点所以能输出。
#print(Chinese.work) 报错,只能访问类自己的作用域,不能访问到外部的work
#print(p1.work)报错

#------场景3-----
work = "程序员"
class Chinese:
    work= "律师"
    l = ["a", "b"]
    def __init__(self,name):
        self.name = name
        print("---->", work)

    def play_ball(self, ball):
        print("%s 正在打 %s" % (self.name, ball))

print(Chinese.__dict__)
print(Chinese.work)  # "律师"。
p1 = Chinese("steven")  # 结果为"----> 程序员"。因为在类里面定义的都跑到类的字典里去了,没有用点访问跟实例和类都没关系不会从类或实例的字典里去找,只是个普通变量
print("实例---》", p1.work)  # "实例---》 律师"。通过点的方式调用,就会从类内部去找

#-----场景四-----
work = "程序员"
class Chinese:
    work= "律师"
    l = ["a", "b"]
    def __init__(self,name):
        self.name = name
        print("---->", work)

    def play_ball(self, ball):
        print("%s 正在打 %s" % (self.name, ball))

p1 = Chinese("steven")
print(p1.l)
# p1.l=[1,2,3]
# print(Chinese.l)
# print(p1.__dict__)
p1.l.append("c")  # 这里p1没有l,这里拿到的l就是类的属性,改的也是类属性
print(p1.__dict__)  # {"name":"steven"}
print(Chinese.l)  # ["a","b","c"]
 

猜你喜欢

转载自www.cnblogs.com/steven223-z/p/12359442.html