day23 object-oriented

First, object-oriented

1, object-oriented and process-oriented

Process-oriented:

Pros: complex issues processes, thus simplifying

Disadvantages: poor scalability

Object-oriented:

Advantages: high scalability

Cons: write complex

2, with the object class

Object: combination of properties and methods

Class: a common set of properties and methods

In real life: first, there is another class of objects

Program: first, there is another class objects

# 定义类(类名建议用驼峰命名)
# class关键字 类名:

class Stutend:
    school = 'oldboy'
    
    def study(self):
        print('学习')

# 生成对象:类加括号,生成对象
stu_1 = Student()
# 获取属性和方法:通过.获取

print(stu_1.school)
print(stu_1.study)

# 查看类中的属性和函数
print(Student.__dict__)
# 类调用方法,需要传递self参数
Student.study(123)
# 对象调用方法,不许要传递self参数
stu_1.study

# 属性的查找顺序
# 先从对象自身找----->类中找----->没找到就报错

# 向对象放属性
# 方式一:
stu_1 = Student()
stu_1.name = 'nick'
# 方式二:通过__init__方法
class Student:
    school = '123'
    
    def __init__(self, name):
        self.name = name
    
    def study(self):
        print('学习')
        
# 产生对象
stu_1 = Student('nick')
# 当实例化对象的时候,会自动调用__init__方法,完成对象的初始化3、

3, binding method

# 绑定方法:
#       定义在类内部的方法:
#       如果类来调用:就是一个普通函数,有几个参数就需要传几个参数
#       对象来调用:他叫对象的绑定方法,第一个参数不需要传,自动传递

class Student:
    school = '123'
    
    def __init__(self, name):
        self.name = name
        
    def study(self):
        print(f'{self.name}学会了Python')
       
stu_1 = Student('nick')
stu_1.study()

4, Python objects in Everything

Python, dictionaries, lists, strings, etc. are objects ......

That is the type of class

Guess you like

Origin www.cnblogs.com/17vv/p/11412545.html