Python OOP-1

2021-02-28  本文已影响0人  若尘___

0. OOP-Python面向对象

1. 面向对象概述(ObjectOriented, OO)

2. 类的基本实现

3. anaconda基本使用

4. 类和对象的成员分析

5.关于self

6. 面向对象的三大特性

6.1 封装

class Student():
    name = "aaa"
    age = 18
    
Student.__dict__

# 实例化
a = Student()
a.__dict__ 
print(a.name)
aaa
class A():
    name = "dana"
    age = 18
    
    # 注意say的写法,参数有一个self
    def say(self):
        self.name = "aaa"
        self.age = 200

# 此案例说明
# 类实例的属性和其对象的实例的属性在不对对象的实例属性赋值的前提下,
# 指向同一变量

# 此时,A称为类实例
print(A.name)
print(A.age)

print("*" * 20)

# id可以鉴别出一个变量是否和另一个变量是同一变量
print(id(A.name))
print(id(A.age))

print("*" * 20)
a = A()

print(a.name)
print(a.age)
print(id(a.name))
print(id(a.age))
dana
18
********************
2245155422024
140717429003616
********************
dana
18
2245155422024
140717429003616
# 此时,A称为类实例
print(A.name)
print(A.age)

print("*" * 20)

# id可以鉴别出一个变量是否和另一个变量是同一变量
print(id(A.name))
print(id(A.age))

print("*" * 20)
a = A()
# 查看A内所有属性
print(A.__dict__)
print(a.__dict__)

a.name = "yaona"
a.age = 16
print(a.__dict__)

print(a.name)
print(a.age)
print(id(a.name))
print(id(a.age))
dana
18
********************
2245155422024
140717429003616
********************
{'__module__': '__main__', 'name': 'dana', 'age': 18, 'say': <function A.say at 0x0000020ABDBC7E18>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
{}
{'name': 'yaona', 'age': 16}
yaona
16
2245155597256
140717429003552
class Student():
    name = "dana"
    age = 18
    
    # 注意say的写法,参数有一个self
    def say(self):
        self.name = "aaa"
        self.age = 200
        print("My name is {0}".format(self.name))
        print("My age is {0}".format(self.age))
        
    def sayAgain(s):
        print("My name is {0}".format(s.name))
        print("My age is {0}".format(s.age))
        
yueyue = Student()
yueyue.say()
yueyue.sayAgain()
My name is aaa
My age is 200
My name is aaa
My age is 200
class Teacher():
    name = "dana"
    age = 19
    
    def say(self):
        self.name = "yaona"
        self.age = 17
        print("My name is {0}".format(self.name))
        # 调用类的成员变量需要用__class__
        print("My age is {0}".format(__class__.age))
        
    def sayAgain():
        print(__class__.name)
        print(__class__.age)
        print("Hello, nice to see you again")
        
t = Teacher()
t.say()
# 调用绑定类函数使用类名
Teacher.sayAgain()
My name is yaona
My age is 19
dana
19
Hello, nice to see you again
# 关于self的案例

class A():
    name = "ruochen"
    age = 18
    
    def __init__(self):
        self.name = "aaaa"
        self.age = 200
        
    def say(self):
        print(self.name)
        print(self.age)
        
class B():
    name = "bbb"
    age = 99
    
a = A()
# 此时,系统会默认把a作为第一个参数传入函数
a.say()

# 此时,self被a替换
A.say(a)
# 同样可以把A作为参数传入
A.say(A)

# 此时,传入的是类实例B,因为B具有name和age属性,所有不会报错
A.say(B)

# 以上代码,利用鸭子模型
aaaa
200
aaaa
200
ruochen
18
bbb
99
# 私有变量案例
class Person():
    # name是共有的成员
    name = "ruochen"
    # __age就是私有成员
    __age = 18
                
p = Person()
# name是公有变量
print(p.name)
# __age是私有变量
# 注意报错信息 
print(p.__age)

ruochen



---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-41-e118b6505697> in <module>
     11 # __age是私有变量
     12 # 注意报错信息
---> 13 print(p.__age)


AttributeError: 'Person' object has no attribute '__age'
# name.mangling 技术
print(Person.__dict__)

p._Person__age = 19
print(p._Person__age)
{'__module__': '__main__', 'name': 'ruochen', '_Person__age': 18, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
19
上一篇下一篇

猜你喜欢

热点阅读