Python_10 debug、类和对象

发布时间 2023-04-23 13:57:23作者: Alisa-sweet

一、查缺补漏

  1. APP测试流程梳理https://www.cnblogs.com/dengqing9393/p/6497068.html

  2. 有关类的知识点http://testingpai.com/article/1626334899176

  3. 有关实例的讲解http://testingpai.com/article/1626338391240

  4. if _ name _ == '_ main _' 是什么http://testingpai.com/article/1618471963781

  5. 私有属性http://testingpai.com/article/1626340793276

  6. if _ name _ == '_ main _'的快捷方式:输入main 按tab

二、debug

  1. 下一步操作:Step over F8

  2. 跳进函数内部操作:Step into F7

  3. 程序快速执行,直接到下一个断点停住,在断点之间来回跳动 F9

  4. 退出断点,点击debug右侧红色方块

三、类和对象

  1. 类(种类):拥有相同特点或者属性的一类事务的集合(抽象) 如:人类、动物(猫、狗)、车(汽车、自行车)

  2. 对象(具体的东西):类的实体,实例 如: 具体是什么狗、具体是什么车

  3. 封装:对某些功能内部实现的封装,提供一个接口,调用某个接口然后就能实现某些特定的功能

    如:python的函数、app、提款机、计算器

四、类

  1. 类的定义

    1. python2.x 写法:class 类名称(): 不更新

    2. python3.x 写法:class 类名称: 现在使用的 如:class TestCase:

  2. 类实例方法和实例化(类实例化之后就叫类实例/类对象)最好不要对一个类实例化多次

    1. 类实例化:cl = TestCase() 用是任意名称cl对象来接收这个类,表示调用这个类,cl为类实例对象

    2. 类实例方法:就是写在类里面的普通方法,主要是函数

访问调用方法:在类的外部可使用类实例名称.方法名称()方法调用,在类的内部使用self.方法名称()调用

_ name _  Run:  Scratches and Consoles  dem002  104  105  106  107  188  109  119  111  112  113  class TestCase:  def  if a>lß:  print( 'ok')  else :  if  ' _main__ '  D: \python\python . exe  ok  cl = TestCase()  cl. test01(11) 'jutes WI  "D: /2621YEAR/\m5h/python/c1ass 10/day11/dem002.py"

self解释:在类中定义函数时,规定第一个传的参数必须是函数本身,self中保存的是该类的内存地址

if _ name _ == '_ main _' 解释:是个入口,但是如果不写他也可以调用类中函数,实例化和调用需要顶格写

  1. 类属性:类属性写在类里面,需单独写出来的,不用写在函数里面

类属性的访问不需要实例化,可以直接通过类进行访问

访问调用方法:在类外部可使用类名称.类属性名称和类实例.类属性名称,类实例就是实例化之后的名称

在类内部可使用self.类属性名称

93  96  97  98  99  dem002  D: . exe  class Dog:  if  color =  name_  color  ' __main__ ' .  = Dog. color #  print(color)  "D: 10/1

  1. 实例属性:写在类里面的def _ init _(self):下面的变量self.变量名,实例属性必须实例化之后访问,自动执行访问

访问调用方法:在类的外部可使用类实例.实例属性名称,类不能直接访问实例属性

在类的内部使用self.实例属性名称

当实例化属性有多个参数时调用需要在实例化的括号里面加实参

97  99  101  102  103  104  dem002  D: . exe  white  - class Dog:  def  self.color = colorl  # self  if  _ name _  ' _main_  dog = Dog('white')  color = dog. color #  print(cotor)  "D:/2021YEAR/iNYf/python/ctass 10/daY11/dem0ß2.py

当类属性和实例属性同名,使用类实例去访问的时候,优先使用类实例属性

97  98  lee  101  102  103  104  105  dem002  D: . exe  class Dog:  color = '䀕  def :  # self  self .color =  •if  name_  dog = Dog()  ' _main_ ' .  - dog. color  color -  print(cotor)  "D: /2621YEAR/fi#$lf/python/c1ass 10/day11/de

def _ init _(self):实例属性是初始化方法,当类实例化之后就自动执行,未调用的时也执行,当类后加()已经是实例化了,所以实例属性中不能使用实例化对象

97  98  99  102  dem002  D: \ python\python . exe  white  - Dog:  def colorl) :  self. = colorl  print(cotorl)  # self  _ name _  ' _ _ main _ ' .  dog = Dog('white')  "D: /2321YEAR/fi&YE/python/ctass 16/day11/dem032.py

  1. 类方法:使用@classmethod来修饰函数的方法就叫类方法

类方法的self名称(类名称)应与类实例方法的self名称(类地址)不一致

访问调用方法:类名.方法名称

79  80  81  82  83  84  85  d em 001  D: \python\python . exe  test alisa  -class TestCase:  @ctassmethod  def test(cts, name):  print( 'test' , name)  if  _name_  _maxn  TestCase. test( ' atisa ' )  "D:/2021YEAR/fiWf/python/ctass Il/day

  1. 私有属性 可作用于类属性、类实例属性

外部不可访问,但是可以使用类实例.类名 _想要访问的私有对象访问对象

私有属性是以_ _(双下划线开头)的属性,不能在类外面进行访问,只能在类内部进行访问

29  30  31  32  33  34  35  36  dem001  D: . exe  ok helten  class TestCase:  def  self. _ _ name =  def test(self):  print( 'ok' , self. _ _ name)  if  _ name _  ' _ _ main _ _ ' .  case = Testcase()  case . test()  "D: /2021YEAR/fiiiiJE/python/cuass It

  1. 私有方法:私有方法是以双下划线开头的方法

五、一些帮助理解的东西

  1. 类实例化:就像人类是一个统称,人类有可以吃饭的方法,但是要具体到一个实例也就是某一个人才能真正实现这个吃饭的方法,人类这个统称是不能吃饭的

  2. 不论什么方法调用时最好都要实例化对象,因为对象调用一切

  3. 以单下划线开头(_foo)的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用“from xxx import *”导入

    以双下划线开头的(_ _foo)代表类的私有成员

    以双下划线开头和结尾的(_ foo )代表python里特殊方法专用的标识,如 _ _init _()代表类的构造函数

  1. 要取类中函数的对象的值时用新对象接收,要调用类函数里面的对象时后面加()括号

  2. 我们学的函数,在类里面不叫函数,叫方法

  3. 类属性、实例属性、私有属性,其实本质就是变量