一 类中的方法

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. 【GeneXus】开发移动APP时,如何使用Canvas进行布局?

    当我们开发移动端APP的时候,经常遇到一种布局方式,那就是层级的布局,比如:一张照片我想在照片的上面显示它的名称,但不影响我照片展示的布局大小,也就是这个名称是浮在照片上的,如图: 如果要实现这样的布 ...

  2. windows创建git并连结github

    1.下载跟自己系统相对应的git版本 2.默认安装 3.绑定用户 git config --global user.name ""git config --global user. ...

  3. Spring--2.Spring之IOC--了解IOC容器

    IOC(容器),用来集成别的框架 1.IOC(Inversion(反转)Of Control):控制反转 控制:资源的获取方式: 主动式: BookServlet{ BookService bs=ne ...

  4. PGSQL 日期时间的比较

    pgsql支持日期时间的比较,但是需要注意的是,我们写sql的时候传入的参数一般是字符串类型,我们需要把把字符串转化为Date类型,否则会查不到内容. 例子: select * from user w ...

  5. 题解 LA3720

    题目大意 多组数据,每组数据给定两个整数 \(n,m\),请求出 \(n\times m\) 的点阵(即 \((n-1)\times(m-1)\) 的方格)中有多少条非水平竖直的经过至少两个格点的不同 ...

  6. Java 设置Word页边距、页面大小、页面方向、页面边框

    本文将通过Java示例介绍如何设置Word页边距(包括上.下.左.右).页面大小(可设置Letter/A3/A4/A5/A6/B4/B5/B6/Envelop DL/Half Letter/Lette ...

  7. unbuntu18.04安装启用splash

    官网:https://splash.readthedocs.io/en/stable/ 1.安装Docker https://www.cnblogs.com/wt7018/p/11880666.htm ...

  8. Manipulating Data from Oracle Object Storage to ADW with Oracle Data Integrator (ODI)

    0. Introduction and Prerequisites This article presents an overview on how to use Oracle Data Integr ...

  9. .NET 框架设计 - 模式、配置、工具 Demo 搜索

    ps:http://www.ituring.com.cn/article/195770 看完第二章,我也忍了!但是随着第三章“副本”的开启,我的阅读速度开始慢慢的减缓,虽然对其中每一个程序模式的理论也 ...

  10. GP工作室—Alpha版本发布1

    目录 GP工作室-Alpha版本发布1 一.简介 1.1作业要求 1.2团队成员 二.软件下载安装说明 五.项目总结 @(Gold Point团队の项目计划) GP工作室-Alpha版本发布1 一.简 ...