4

12Python面向过程和面向对象

 2 years ago
source link: https://blog.csdn.net/qq_51082388/article/details/122492767
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1、编程的两大思想——面向过程和面向对象

在这里插入图片描述

2、类和对象的创建

类:多个类似事物组成的群体的统称,能够帮助我们快速理解和判断事物的性质。
在这里插入图片描述
在这里插入图片描述

3、类对象与类属性

解决问题:如何去定义它?
在这里插入图片描述

class Student:#Student为类的名称(类名)由一个或者多个单词组成,每个单词首字母大写,其余小写
    pass

问:Student是对象吗?内存有开空间吗 ?
如何去判断?

class Student:#Student为类的名称(类名)由一个或者多个单词组成,每个单词首字母大写,其余小写
    pass
print(id(Student))
print(type(Student))
print(Student)
1419131921712
<class 'type'>
<class '__main__.Student'>

class Student:#Student为类的名称(类名)由一个或者多个单词组成,每个单词首字母大写,其余小写
  native_pace='激励' #直接写在类里的变量,称为类属性
  #实例方法
  def eat(self):
      print('吃饭饭')
  #静态方法
  @staticmethod
  def method():
      print('我使用了staticmethod进行修饰,所以我是静态方法')
  #类方法
  @classmethod
  def method(cls):
      print('我是类方法,因为我使用了classmethod进行修饰')

 #定义在类之外的称为函数,在类之内定义的称为方法

def drink():
    print('喝水')
#初始化方法
  def_init_(self,name,age):
  self.name=name#self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋给实体属性
  self.age=age

如何创建类?
在这里插入图片描述

上边的代码有误,出现报错,原因是第五行 def和那杠杠那里是有空格的,上边的代码没有敲空格,也没有对齐

class Student:#Student为类的名称(类名)由一个或者多个单词组成,每个单词首字母大写,其余小写
  native_pace='激励' #直接写在类里的变量,称为类属性
  #初始化方法
  def __init__(self,name,age):
    self.name=name    #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋给实体属性
    self.age=age
  #实例方法
  def eat(self):
      print('吃饭饭')
  #静态方法
  @staticmethod
  def method():
      print('我使用了staticmethod进行修饰,所以我是静态方法')
  #类方法
  @classmethod
  def method(cls):
      print('我是类方法,因为我使用了classmethod进行修饰')

 #定义在类之外的称为函数,在类之内定义的称为方法

def drink():
    print('喝水')
#创建Student类的对象
stu1=Student('张三',20)
stu1.eat()
print(stu1.name)
print(stu1.age)

调用方法1

吃饭饭
张三
20

调用方法2

Student.eat(stu1)
吃饭饭

4、类方法与静态方法

在这里插入图片描述
如何调用类属性?

类属性的使用方式

class Student:#Student为类的名称(类名)由一个或者多个单词组成,每个单词首字母大写,其余小写
  native_pace='激励' #直接写在类里的变量,称为类属性
  #初始化方法
  def __init__(self,name,age):
    self.name=name    #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋给实体属性
    self.age=age
  #实例方法
  def eat(self):
      print('吃饭饭')
  #静态方法
  @staticmethod
  def method():
      print('我使用了staticmethod进行修饰,所以我是静态方法')
  #类方法
  @classmethod
  def method(cls):
      print('我是类方法,因为我使用了classmethod进行修饰')

 #定义在类之外的称为函数,在类之内定义的称为方法

#类属性的使用方式
print(Student.native_pace)
stu1=Student('张三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
激励
激励
激励
class Student:#Student为类的名称(类名)由一个或者多个单词组成,每个单词首字母大写,其余小写
  native_pace='激励' #直接写在类里的变量,称为类属性
  #初始化方法
  def __init__(self,name,age):
    self.name=name    #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋给实体属性
    self.age=age
  #实例方法
  def eat(self):
      print('吃饭饭')
  #静态方法
  @staticmethod
  def method():
      print('我使用了staticmethod进行修饰,所以我是静态方法')
  #类方法
  @classmethod
  def method(cls):
      print('我是类方法,因为我使用了classmethod进行修饰')

 #定义在类之外的称为函数,在类之内定义的称为方法

#类属性的使用方式
print(Student.native_pace)
stu1=Student('张三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
Student.native_pace='天津'
print(stu1.native_pace)
print(stu2.native_pace)
激励
激励
激励
天津
天津

在这里插入图片描述

上边类方法是写的method,这里改了,改成cm,就能够运行输出了

class Student:#Student为类的名称(类名)由一个或者多个单词组成,每个单词首字母大写,其余小写
  native_pace='激励' #直接写在类里的变量,称为类属性
  #初始化方法
  def __init__(self,name,age):
    self.name=name    #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋给实体属性
    self.age=age
  #实例方法
  def eat(self):
      print('吃饭饭')
  #静态方法
  @staticmethod
  def method():
      print('我使用了staticmethod进行修饰,所以我是静态方法')
  #类方法
  @classmethod
  def cm(cls):
      print('我是类方法,因为我使用了classmethod进行修饰')

 #定义在类之外的称为函数,在类之内定义的称为方法

#类属性的使用方式
print(Student.native_pace)
stu1=Student('张三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
Student.native_pace='天津'
print(stu1.native_pace)
print(stu2.native_pace)
print('----------------')
Student.cm()
激励
激励
激励
天津
天津
----------------
我是类方法,因为我使用了classmethod进行修饰
Student.method()
我使用了staticmethod进行修饰,所以我是静态方法

动态绑定属性和方法

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def eat(self):
        print(self.name+'在吃饭')
stu1=Student('张三',20)
stu2=Student('李四',24)
print(id(stu1))
print(id(stu2))
print('-----------为stu2动态绑定性别属性---------------')
stu2.gender='女'
print(stu1.name,stu1.age)
print(stu2.name,stu2.age,stu2.gender)
2517594759120
2517594759024
-----------为stu2动态绑定性别属性---------------
张三 20
李四 24 女
stu1.eat()
stu2.eat()
张三在吃饭
李四在吃饭

在这里插入图片描述
报错原因:因为stu2并没有绑定show方法

class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def eat(self):
        print(self.name+'在吃饭')
stu1=Student('张三',20)
stu2=Student('李四',24)
print(id(stu1))
print(id(stu2))
print('-----------为stu2动态绑定性别属性---------------')
stu2.gender='女'
print(stu1.name,stu1.age)
print(stu2.name,stu2.age,stu2.gender)
print('----------------------------')
stu1.eat()
stu2.eat()
def show():
    print('定义在类之外的,称函数')
stu1.show=show
stu2.show=show
stu1.show()
stu2.show()

绑定之后运行结果

1635341082576
1635341082480
-----------为stu2动态绑定性别属性---------------
张三 20
李四 24 女
----------------------------
张三在吃饭
李四在吃饭
定义在类之外的,称函数
定义在类之外的,称函数

知识点总结

在这里插入图片描述

attribute
在这里插入图片描述

为啥那么慢呢?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK