Chapter VII of object-oriented (1): object-oriented programming and category (class) Introduction

From a start, we included learning object-oriented programming. What is object-oriented programming it? First, we briefly explain some basic definitions:

Object-Oriented Programming: a programming paradigm
programming paradigm: process-oriented / object-oriented

List of structures, Dictionary of what used to efficiently store data: data structures
algorithms: programming logic

  1. Process-oriented compiled

The core process is the word, refers to the process steps to resolve the problem. - design a pipeline, mechanical way of thinking. After the break the problem step by step process

  • advantage:
    • The problem of complex processes, thus simplifying
  • Disadvantages:
    • Poor scalability
  1. Object-oriented programming - God point of view: everything is an object

What is the object: the object is a combination of characteristics and skills

  • advantage:
    • Scalability,
  • Disadvantages:
    • Programming high complexity
  • Scenario:
    • Constantly changing needs of users, Internet applications, games, enterprise applications

Object-oriented programming has three major characteristics:

  1. inherit
  2. Polymorphism
  3. Package

Object-oriented and process-oriented strengths, the selected mode to see the situation

7.1 class class

Class is a set of objects with similar features skill combination

He stressed: different point of view, the classification obtained is not the same

In the real world: existing objects, after class
in the program: first define a class, after generation (instantiate) objects

When analyzing an object you can start to start, for example, the following student categories:

  1. analysis
  • In the real world:
    • Object 1: Wang Xiaoming
      • feature:
        • College school = aaa
        • Name = Xiaoming
        • Sex = Male
        • Age = 12
      • skill:
        • Learn
        • eat
        • go to bed
    • Object 2: Li Xiao Yu
      • feature:
        • College school = aaa
        • Name = Li rain
        • Sex = Female
        • Age = 11
          Skills:
          Learn
          to eat
          sleep
  • Summarize student class in the real world:
    • Similar characteristics:
      • School = "aaa college"
    • Similar skills:
      • Learn
      • eat
      • go to bed
  1. The definition of class
class Student:
    school = 'aaa'  # 特征:变量
    
    def learn(self):  # 技能:函数
        print('learning')
        
    def eat(self):  # 这个self就是用来自动传参,不一定非要用self,用x什么的也行,只是约定俗成用self
        print('eating')
        
    def sleep(self):
        print('sleeping')
  1. Generates an object: instantiate
stu1 = Student()  # 类的返回就是个对象:实例化
stu2 = Student()

How to use Class 7.2

In the class definition phase, the class has been executed.

Function definition stage, will not be executed, only the call will be performed.

Class attribute operations (CRUD) and instantiated:

class Student:
    school = 'aaa'  # 变量又称-类的数据属性
    
    def learn(self):  # 函数又称-类的函数属性
        print('learning')
        
    def eat(self):
        print('eating')
        
    def sleep(self):
        print('sleeping')
        
    print('=====run=====')  # 测试执行定义类的语句是否执行内部代码

# 查看类的命名空间
print(Student.__dict__)
print(Student.__dict__['school'])  # 可以访问,但是我们可以用python提供的方法访问类的属性
print(Student.__dict__['learn'])  # 可以访问,但是我们可以用python提供的方法访问类的属性

# 访问类的属性:查看类的属性
print(Student.school) 
print(Student.learn) 

# 增加属性
Student.country = 'China'  # 往字典里增加变量
print(Student.country)

# 删除
del Student.country

# 改
Student.school = 'bbb'
Student.school

# 实例化:生成一个对象
stu1 = Student()  # 实例化一个叫做stu1的对象

Guess you like

Origin www.cnblogs.com/py-xiaoqiang/p/11210394.html