一 类中的方法

1.1 介绍

(1) 普通方法
(2) 绑定方法

  1. 绑定到对象 (自动传递对象参数)
  2. 绑定到类 (自动传递类参数)

(3) 静态方法 (无论类还是对象,都可以调用)

class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane() #普通方法的调用只能使用类来调用,因为没有参数
Palne.capitain()

执行

[root@node10 python]# python3 test.py
Traceback (most recent call last):
File "test.py", line 18, in <module>
obj = Plane()
TypeError: __init__() missing 1 required positional argument: 'name'

对象使用参数

class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #普通方法的调用,只能使用类来调用,因为没有参数
Plane.capitain()
obj.capitain() #这里会默认传参,不能一一对应

执行

self 系统会默认传递,但是新参没有,不能一一对应

[root@node10 python]# python3 test.py
will have a capitain
Traceback (most recent call last):
File "test.py", line 22, in <module>
obj.capitain()
TypeError: capitain() takes 0 positional arguments but 1 was given

1.2 绑定方法的调用

[root@node10 python]# cat test.py
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定方法的调用
obj.fly()

执行

[root@node10 python]# python3 test.py
plane can fly

由于方法fly只是简单的打印,就可以使用对象调用

class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定方法的调用
obj.fly()
Plane.fly(111)

执行

[root@node10 python]# python3 test.py
plane can fly
plane can fly

当带有self的参数就会报错(如果函数体里用到了该对象,类名调用的方式不可以.)

class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定方法的调用
Plane.fly(111)

执行

[root@node10 python]# python3 test.py
Traceback (most recent call last):
File "test.py", line 22, in <module>
Plane.fly(111)
File "test.py", line 7, in fly
print ("plane can fly",self.name)
AttributeError: 'int' object has no attribute 'name'

1.3 绑定到类方法的调用

class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()

执行

[root@node10 python]# python3 test.py
will help people

打印出这个类

class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()

执行

[root@node10 python]# python3 test.py
<class '__main__.Plane'>
will help people

对象可以调用类中的属性和方法,但是类不能调用对象中的属性和方法

class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
cls.name
print (cls)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()

执行

[root@node10 python]# python3 test.py
Traceback (most recent call last):
File "test.py", line 24, in <module>
Plane.save()
File "test.py", line 14, in save
cls.name
AttributeError: type object 'Plane' has no attribute 'name'

1.4 调用类中的属性

class Plane():
material = "合金"
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls.material)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()

执行

[root@node10 python]# python3 test.py
合金
will help people

1.5 对象调用

先把obj所归属的类找出来,然后把该类当成参数进行传递.

class Plane():
material = "合金"
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls.material)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()
obj.save()

执行

[root@node10 python]# python3 test.py
合金
will help people
合金
will help people

1.6 静态方法的调用

class Plane():
material = "合金"
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls.material)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng")
#静态方法的调用
obj.attack()
Plane.attack()

执行

[root@node10 python]# python3 test.py
some bad people use if for attack
some bad people use if for attack

调用成功

031.Python类中的方法的更多相关文章

  1. python 类中的方法

    首先,方法是类内部定义的函数,所以方法是类的属性而不是实例的属性. 其次,方法只能在所属的类拥有实例的时候才能被调用.当存在一个实例的时候,我们可以说方法被绑定到实例.如果没有实例,那么我们就说方法是 ...

  2. Python 类中__init__()方法中的形参与如何修改类中属性的值

    一.__init__()方法 如果__init__()方法为 class Cat(): def __init__(self,num) : self.num=num Python中类的__init__( ...

  3. Python 简明教程 --- 20,Python 类中的属性与方法

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 与客户保持良好的关系可以使生产率加倍. -- Larry Bernstain 目录 类中的变量称为属 ...

  4. Python 装饰器装饰类中的方法

    title: Python 装饰器装饰类中的方法 comments: true date: 2017-04-17 20:44:31 tags: ['Python', 'Decorate'] categ ...

  5. 如何访问python类中的私有方法

    在python中,不像c#/java类语言,支持类的私有方法,这点有点像objc,虽然objc可以通过扩展extension来实现,但源于objc的运行时特性,我们还是可以通过非常手段来进行访问的.不 ...

  6. 孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘

    孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天发现了python的类中隐藏着一些特殊的私有方法. 这些私有方法不管我 ...

  7. python 类中方法总结 --- 实例方法、类方法、静态方法

    在python的类语法中,可以出现三种方法,具体如下: (1)实例方法 1)第一个参数必须是实例本身,一般使用[self]表示. 2)在实例方法中,可以通过[self]来操作实例属性,[类名]来操作类 ...

  8. 第8.6节 Python类中的__new__方法深入剖析:调用父类__new__方法参数的困惑

    上节<第8.5节 Python类中的__new__方法和构造方法__init__关系深入剖析:执行顺序及参数关系案例详解>通过案例详细分析了两个方法的执行顺序,不知大家是否注意到了,在上述 ...

  9. 第8.12节 Python类中使用__dict__定义实例变量和方法

    上节介绍了使用实例的__dict__查看实例的自定义属性,其实还可以直接使用__dict__定义实例变量和实例方法. 一. 使用__dict__定义实例变量 语法: 对象名. dict[属性名] = ...

随机推荐

  1. 原生js面向对象编程-选项卡(点击)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. 记一次docker镜像导出导入流程

    目标:导出测试环境的镜像到本地机器 过程: 测试机: docker save -o /Dockerfile/crontabService/php72.tar lnmp72:v1.4 压缩,要不文件太大 ...

  3. goland编辑器永久激活

    1 下载goland破解文件补丁 链接: https://pan.baidu.com/s/1i3dFAwscXPzKV-1imvgkdA 提取码: furt 2 打开goland的安装文件,将下载好的 ...

  4. Django框架初体验

    前言 从今天开始学习测试开发知识,并会把每一次学习的过程和成果记录到博客,由于我也没怎么接触过python相关的开发框架,所以前期应该是艰难的,但是我相信努力就会有收获,如果你和我一样是个小白,那我们 ...

  5. Nginx在Centos 7中配置开机启动

    1.创建脚本 # vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v ...

  6. python 线程信号量

    线程信号量和进程信号量相似 # 线程信号量 import time from threading import Semaphore from threading import Thread def t ...

  7. Django项目运行

    1.图像界面 2.配置ip和端口 2.命令行 python manage.py runserver ip:端口号 ip:端口号可以不写,为默认 也可以只修改端口号

  8. 实验五:配置Eth-Trunk链路聚合(手工负载分担模式)

    1.配置图 2.配置命令 LSW1的eth trunk 1配置如下: 配置命令如下: [S1]Eth-Trunk1 创建Eth-Trunk1端口 [S1-Eth-Trunk1]mode lacp-st ...

  9. 批量解决win10图标上有两个蓝色箭头的方法

    双击“此电脑”,点击“C盘”,可以看到一个”用户“文件夹,双击”用户“, 选择现在正在使用的用户名,双击用户名,找到该文件夹下的”桌面“或”Desktop“点击“属性”, 在“常规”选项卡中的属于项中 ...

  10. SpringCloud与微服务系列专栏

    一. 前置知识 学习SpringCloud之前需要具备和掌握如下框架和工具的使用:SpringMVC,Spring,Spring Boot,Mybatis,Maven,Git. SpringCloud ...