python之面向对象

三大编程范式:面向过程、函数式编程、面向对象编程
面向过程:核心是过程,过程指的是解决问题的步骤,设计一条流水线,机械式的思维方式
  优点:复杂的问题流水化,
   缺点:可扩展性差
   应用场景:功能一旦实现,应用扩展性很少的场景

面向对象:核心是对象二字,对象是特征与技能的结合体
   优点:可扩展性强,
   缺点:编程复杂度高
   应用场景:用户需求经常变化,互联网应用、游戏,企业内部应用

类:是一系列对象相似的特征与技能的结合体
   强调:站在不同的角度,得到的分类是不一样的

  在现实世界中:一定先有对象,后有类
  在程序中: 一定得先定义类,后调用类来产生对象

 1 class Student:
 2     school = '一中'  # 数据属性
 3 
 4     def learn(self):  # 函数属性
 5         print("is learning")
 6 
 7     def eat(self):
 8         print("is eating")
 9 
10     def sleep(self):
11         print("is sleeping")
12     print("test")  # 类在定义阶段,代码会执行
13     
14     
15 stu1 = Student()
16 stu2 = Student()
17 stu3 = Student()
18 print(stu1, stu2, stu3)
 1 # 查看类的名称空间
 2 print(Student.__dict__)
 3 print(Student.__dict__["school"])
 4 print(Student.__dict__["learn"])
 5 
 6 #
 7 print(Student.school)  # 等同于 print(Student.__dict__["school"])
 8 print(Student.learn)
 9 
10 #
11 Student.country = "China"
12 print(Student.__dict__)
13 print(Student.country)
14 
15 #
16 del Student.country
17 print(Student.country)
18 
19 #
20 Student.school = "二中"
21 print(Student.school)
查看类的名称空间,增删改查类的属性

     类Student加上 __init__方法,为对象自己定制自己独有的特征

 1 # __init__ 方法,为对象自己定制自己独有的特征
 2 
 3 class Student:
 4     school = '一中'
 5 
 6     def __init__(self, name, sex, age):
 7         self.Name = name
 8         self.Sex = sex
 9         self.Age = age
10 
11     def learn(self):
12         print("is learning")
13 
14     def eat(self):
15         print("is eating")
16 
17 
18 stu1 = Student("王二丫", "", 18)  # stu1(当做第一个参数) 连同后面的 ("王二丫", "女", 18) 3个参数组成4个参数,传给init
19 
20 # 加上__init__方法后,实例化的步骤
21 # 1、先产生一个空对象,stu1
22 # 2、Student.__init__(stu1,)
23 print(Student.__init__)  # 类Student函数方法 __init__ 的地址: <function Student.__init__ at 0x00000260A133D620>
加__init__ 方法
 1 #
 2 print(stu1.__dict__)  # 查实例 stu1,结果{'Name': '王二丫', 'Sex': '女', 'Age': 18}
 3 print(stu1.Name)
 4 print(stu1.Sex)
 5 print(stu1.Age)
 6 
 7 #
 8 stu1.Name = "李二丫"
 9 print(stu1.__dict__)
10 print(stu1.Name)
11 
12 # 删除
13 del stu1.Name
14 print(stu1.__dict__)
15 
16 #
17 stu1.class_name = "101K"
18 print(stu1.__dict__)
19 
20 # 增加实列stu2
21 stu2 = Student("李三", "", 38)  #传值 __init__(stu2, "李三", "男", 38)
22 print(stu2.__dict__)
23 print(stu2.Name)
24 print(stu2.Age)
25 print(stu2.Sex)
加上__init__方法后,增删改查
 

  类Student,属性查找

class Student:
    school = '一中'

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

    def learn(self, x):
        print(" %s is learning %s" % (self.Name, x))

    def running(self):
        print(" %s is running" % self.Name)

    def eat(self):
        print("%s is eating" % self.Name)


stu1 = Student("王二丫", "", 18)  # 实列化
stu2 = Student("李三炮", "", 38)
stu3 = Student("王五", "", 24)
View Code

        __dict__ 属性

      python中的__dict__属性,__dict__是一个字典,键是属性名,值为属性值。
      Python的实例有自己的__dict__,它对应的  类也有自己的__dict__ (有些特殊的对象是没有__dict__属性的,这里不做讨论)

1 print(stu1.__dict__)  # {'Name': '王二丫', 'Sex': '女', 'Age': 18}
2 print(stu2.__dict__)  # {'Name': '李三炮', 'Sex': '男', 'Age': 38}
3 print(stu3.__dict__)  # {'Name': '王五', 'Sex': '男', 'Age': 24}

       类中的数据属性:是所有对象共有的,绑定到不同的对象是不同的绑定方法,对象调用绑定方法时,会把对象本身当作第一个传入,传给self  

1 print(Student.school, id(Student.school)) # 4个id值相同,因为调用的都是类Student中同一个数据属性 school
2 print(stu1.school, id(stu1.school))
3 print(stu2.school, id(stu1.school))
4 print(stu3.school, id(stu1.school))
  类中的函数属性:是绑定给对象,绑定到不同的是不同的绑定犯法,对象调用绑定方式时,会把对象本身当做第一个传入,传给self
 1 print(Student.learn)
 2 Student.learn(stu1, "room")
 3 Student.learn(stu2, "library")
 4 Student.learn(stu3, "subway")
 5 # 等同于
 6 stu1.learn("room")  # learn(stu1,"room")
 7 stu2.learn("library")  # learn(stu1,"library")
 8 stu3.learn("subway")  # learn(stu1,"subway")
 9 
10 
11 # 这3个的函数地址不一样,因为实例对象不同
12 print(stu1.learn)
13 print(stu2.learn)
14 print(stu3.learn)
 
1 stu1.plus = "from stu1"  # 实例stu1 增加数据属性
2 print(stu1.__dict__)  {'Name': '王二丫', 'Sex': '', 'Age': 18, 'plus': 'from stu1'}
3 print(stu1.plus)  # from stu1

 类 - 继承

什么是继承?
继承指的是类与类之间的关系,是一种什么“是”什么的关系,
继承的功能之一就是用来解决代码重用问题。继承是一种创建新类的方式,
在python中,新建的类可以继承一个或多个父类,父类又可以成为基类或超类,
新建的类称为派生类或子类

 1 class Parentclass1:
 2     pass
 3 
 4 
 5 class Parentclass2:
 6     pass
 7 
 8 
 9 class SubClass1(Parentclass1):
10     pass
11 
12 
13 class SubClass2(Parentclass1, Parentclass2):
14     pass
15 
16 
17 print(SubClass1.__bases__)  # __bases__ 查看 类SubClass1 继承了哪些父类
18 print(SubClass2.__bases__)
继承

 

 1 class Hero:
 2     def __init__(self, nickname, attack_power, life_value):
 3         self.nickname = nickname
 4         self.attack_power = attack_power
 5         self.life_value = life_value
 6 
 7     def attack(self, enemy):
 8         enemy.life_value -= self.attack_power
 9 
10 
11 class Garen(Hero):
12     camp = "Demacia"
13 
14 
15 class Riven(Hero):
16     camp = "Noxus"
17 
18 
19 g1 = Garen("草丛伦", 66, 78)
20 r1 = Riven("雷文", 80, 98)
21 print(g1.nickname, g1.life_value, g1.attack_power, g1.camp)
继承2

继承-属性查找小练习

      Q:b.f2() 运行显示的结果是?

      A : from Foo .f2  --- >  from Bar .f1

    

 1 class Foo:
 2     def f1(self):
 3         print("from Foo .f1")
 4 
 5     def f2(self):
 6         print("from Foo .f2")
 7         self.f1()
 8 
 9 
10 class Bar(Foo):
11     def f1(self):  # 重点 b = Bar(),顺序,对象自己、对象所在类查找、父类查找
12         print("from Bar .f1")
13 
14 
15 b = Bar()
16 b.f2()
17 print(b.__dict__)

     





原文地址:https://www.cnblogs.com/zhanghonghong/p/9134912.html