1、面向对象

类(class):是一种用来描述具有相同属性和方法的对象的集合。

类变量:类变量在整个实例化的对象中是公用的。一般定义在类中且在函数体之外。

方法:类中的函数

数据成员:类变量或者实例变量用于处理类及其实例对象的相关的数据。

方法重写:如果从父类继承的方法不能满足子类的需求,可以对其进行改写,这个过程叫方法的覆盖(override),也称为方法的重写。

局部变量:定义在方法中的变量,只作用于当前实例的类。

实例变量:在类的声明中,属性是用变量来表示的,这种变量就称为实例变量,实例变量就是一个用 self 修饰的变量。

继承:即一个派生类(derived class)继承基类(base class)的字段和方法。继承也允许把一个派生类的对象作为一个基类对象对待。就像我们定义一个fruit(水果)类,然后又定义了一个fruit类的派生类apple(苹果),它有着fruit类的一些属性和方法,也有着自己的一些独特的属性和方法,和fruit类是一种’is-a’的关系。

实例化:类的一个具体对象,类像当于一个模板,只有我们将其实例化为一个对象后才能对其进行相应的操作。

对象:通过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法。


2、类定义

定义一个类:

class ClassName:....    ....    ....

类名建议采用驼峰式命名,或者全部大写字母


3、使用类对象方法

类对象支持两种操作:属性引用和实例化

属性引用:和python中的其他语法一样,obj.name

在类中带__的属性为类的私有属性,私有属性在类外部无法直接访问,像__name.

class Fruit:#这是类的一个基本属性self.number = 100def get_number(self):a = self.number + 100return a
f = Fruit()print('We have {0} fruits'.format(f.number))print('We have {0} fruits'.format(f.get_number()))

输出结果:

We have 100 fruitsWe have 200 fruits

4、构造方法

python类中有一个名为__init__()的特殊方法,叫构造方法,该方法在类进行实例化时会自动进行调用(可以用于类属性初始化等),类似C++里面类的构造函数。

def __init__(self):self.data = []

类定义了 __init__() 方法,类的实例化操作会自动调用__init__()方法。

class Fruit:def __init__(self):                print('你已经实例化了一个对象')
f = Fruit()

输出结果

你已经实例化了一个对象

init_() 方法可以有参数,参数通过 init() 传递到类的实例化操作上。

class Complex:def __init__(self,real,image):self.r = realself.i = imagedef get_complex(self):                print('complex real is %.2f , image is %.2f'%(self.r,self.i))
a = Complex(3.5,-3)a.get_complex()

输出结果如下:

complex real is 3.50 , image is -3.00

self代表类的实例,而非类。类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self。但self并不是Python中的关键字哦。

不知是否可以这样理解,self就代表的是你按照一个类实例化一个对象后的对象的地址。很像C++类中this指针class Test:

def prt(self):        print(self)        print(self.__class__)
t = Test()t.prt()

输出:

<__main__.Test object at 0x0000025EC6D45608><class '__main__.Test'>

5、类的方法

在类的内部,使用 def 关键字来定义一个方法,与一般函数定义不同,类方法必须包含参数 self, 且为第一个参数。如果不需要self传递参数,需要在函数前面加上@staticmethod,表示静态方法

​​​​​​​

class Complex:def __init__(self, real=None, image=None):self.r = realself.i = image
def get_complex(self):        print('complex real is %.2f , image is %.2f' % (self.r, self.i))
    @staticmethoddef test(a, b):        print('complex real is %.2f , image is %.2f' % (a, b))a = Complex(3.5, -3)a.get_complex()
b = Complex()b.test(3, -2)

输出结果​​​​​​​

complex real is 3.50 , image is -3.00complex real is 3.00 , image is -3.00

6、继承

Python 同样支持类的继承,格式如下:​​​​​​​

class Derivedclassname(Baseclassname):    ...    ...

Baseclassname(基类名)必须与派生类定义在一个作用域内。除了类,还可以用表达式,基类定义在另一个模块中时这一点非常有用:

class Fruit:    def __init__(self,sweet):        self.sweetness = sweet    def describe(self):        print('Our fruit has a sweetness of %.2f'%self.sweetness)
class Apple(Fruit):#单继承,继承fruit类    def __init__(self,sweet,color):        self.color = color        Fruit.__init__(self,sweet)    def describe(self):#改写基类fruit的方法        print('Our apple has a sweetness of {0:.2f}%,and color is {1}'.format(self.sweetness,self.color))apple = Apple(62.2,'red')apple.describe()

输出:

Our apple has a sweetness of 62.20%,and color is red

多继承

Python同样可以继承多个基类:

class Derivedclassname(basename1,basename2,...):    ...    ...    ...

需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,python从左至右搜索,即方法在子类中未找到时,从左到右查找父类中是否包含方法。​​​​​​​

class Fruit:    def __init__(self, sweet):        self.sweetness = sweet            def describe(self):        print('Our fruit has a sweetness of %.2f' % self.sweetness)class Food:    def __init__(self, uprice, num):        self.unit_price = uprice        self.number = num        self.total_price = num * uprice            def cost(self):        print('You need to pay {0:.3} yuan, thank you'.format(self.total_price))class Apple(Fruit, Food):    def __init__(self, sweet, color, uprice, num):        self.color = color        Fruit.__init__(self, sweet)        Food.__init__(self, uprice, num)        def describe(self):        print('Our fruit has a sweetness of {0:.2f}%,and color is {1}'.format(self.sweetness, self.color))apple = Apple(62.2,'red',3.5,21)apple.describe()apple.cost()

输出:

Our fruit has a sweetness of 62.20%,and color is redYou need to pay 73.5 yuan, thank you

7、方法重写

如果父类方法的功能不能满足你的需求,你可以在子类重写你父类的方法,如果想调用已经被覆盖的基类方法,可以用super(子类名,子类实例对象名).父类方法

class Parent_class:     def Method(self):         print ('父类方法')class Child_class(Parent_class): # 定义子类    def Method(self):        print ('子类方法')c = Child_class()                # 子类实例化c.Method()                  # 子类调用重写方法super(Child_class,c).Method()    #用子类对象调用父类已被覆盖的方法

子类继承父类构造函数

如果在子类中需要父类的构造方法就需要显式地调用父类的构造方法,或者不重写父类的构造方法。

class A:    def __init__(self, x, y):        self.x = x        self.y = y        print('pos is ({0},{1})'.format(self.x, self.y))
    def xxx(self):        print('parent now')class B(A):     def xxx(self):        print('child now')b = B(10, 3)b.xxx()

输出

pos is (10,3)child now

如果重写了__init__ 时,实例化子类,就不会调用父类已经定义的 __init__。

如果重写了__init__ 时,要继承父类的构造方法,可以使用 super 关键字super(子类,self).__init__(参数1,参数2,....),或者父类名称.__init__(self,参数1,参数2,...)


8、类的私有属性

两个下划线开头,声明该属性为私有,像__name不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__name。

class JustCounter:     __secretCount = 0  # 私有变量    publicCount = 0  # 公开变量
    def count(self):        self.__secretCount += 1        self.publicCount += 1       print(self.__secretCount)counter = JustCounter()counter.count()counter.count()print(counter.publicCount)print(counter.__secretCount)  # 报错,实例不能访问私有变量
Traceback (most recent call last):File "test.py", line 16, in <module>    print (counter.__secretCount)  # 报错,实例不能访问私有变量AttributeError: 'JustCounter' object has no attribute '__secretCount'

两个下划线开头,声明该方法为私有方法,像__private_method,只能在类的内部调用 ,不能在类的外部调用。self.___private_method。

class Site:    def __init__(self, name, url):        self.name = name  # public        self.__url = url  # private
    def who(self):        print('name  : ', self.name)        print('url : ', self.__url)
    def __foo(self):  # 私有方法        print('这是私有方法')
    def foo(self):  # 公共方法        print('这是公共方法')        self.__foo()x = Site('***', 'www.xxx.com')x.who() # 正常输出x.foo() # 正常输出x.__foo()  # 报错

输出:

'''name  :  ***url :  www.***.com这是公共方法这是私有方法Traceback (most recent call last):  File "F:\Python\Program\test.py", line 61, in <module>    x.__foo()      # 报错AttributeError: 'Site' object has no attribute '__foo''''

类的专有方法

__init__ : 构造函数,在生成对象时调用,类似C++构造函数

__del__: 析构函数,释放对象时使用,类似C++析构函数,常用在释放申请的内存空间

__repr__: 打印,转换。这个个函数就是在打印类的时候,控制类输出的字符串

class Name:    def __init__(self, name):        self.name = nameprint(Name('s'))
'''<__main__.Name object at 0x0000023744AFD248>'''
class Name:      def __init__(self,name):        self.name = name
      def __repr__(self): #控制了在打印类时候的输出          return 'Name({!r})'.format(self.name)print(Name('s'))
'''Name('s')'''

__setitem__ : 每当属性被赋值的时候都会调用该方法,因此不能再该方法内赋值 self.name = value 会死循环

__getitem__: 当访问不存在的属性时会调用该方法

__len__: 获得长度,如果一个类表现得像一个list,要获取有多少个元素,就得用len() 函数。要让len()函数工作正常,类必须提供一个特殊方法__len__(),它返回元素的个数。

class CountList:    def __init__(self, *args):        self.list = [x for x in args]        self.count = self.__len__()
     def __len__(self):         return len(self.list)
     def get_count(self):         return self.counta = CountList(1, 2, 3, 4, 4, 5)print(a.get_count())print(len(a))​​​​​​​

__cmp__: 比较运算

__call__: 函数调用

__add__: 加运算

__sub__: 减运算

class MyClass:
    def __init__(self, height, weight):        self.height = height        self.weight = weight
# 两个对象的长相加,宽不变.返回一个新的类    def __add__(self, others):        return MyClass(self.height + others.height, self.weight + others.weight)
# 两个对象的宽相减,长不变.返回一个新的类    def __sub__(self, others):        return MyClass(self.height - others.height, self.weight - others.weight)
# 说一下自己的参数    def intro(self):        print("高为", self.height, " 重为", self.weight)def main():    a = MyClass(height=10, weight=5)    a.intro()
    b = MyClass(height=20, weight=10)    b.intro()
    c = b - a    c.intro()
    d = a + b    d.intro()if __name__ == '__main__':    main()
'''高为 10  重为 5高为 20  重为 10高为 10  重为 5高为 30  重为 15'''

__mul__: 乘运算

__truediv__: 除运算

__mod__: 求余运算

__pow__: 乘方

同样的。类的专有方法也可以重写

Python面向对象---类的基本使用相关推荐

  1. python面向对象类属性_python面向对象之类属性和类方法案例分析

    本文实例讲述了python面向对象之类属性和类方法.分享给大家供大家参考,具体如下: 目标 类的结构 类属性和实例属性 类方法和静态方法 01. 类的结构 1.1 术语 -- 实例 使用面相对象开发, ...

  2. python面向对象类的成员_Python面向对象中类的成员

    Python面向对象中类的成员总共有三种,分别是字段.方法和属性 Python Version: 3.5+ 字段 普通字段1 2 3 4 5 6 7 8class A: def __init__(se ...

  3. python面向对象类创建人物类包含姓名、职业_Python面向对象思想与应用入门教程【类与对象】...

    本文实例讲述了Python面向对象思想与应用.分享给大家供大家参考,具体如下: 面向对象思想 1.面向对象的设计思想 面向对象是基于万物皆对象这个哲学观点. 2.面向对象和面向过程的区别 面向过程 在 ...

  4. python面向对象 —— 类和对象(一)

    文章目录 一.面向对象的概念 二.类和对象 (一)类 (二)对象 (三)从对象抽象出类 三.创建类 (一)类属性 类变量和实例变量 (二)类的方法 参数 :self 的理解 __init__(初始化方 ...

  5. python面向对象类创建人物类包含姓名、职业_python面向对象类

    面向对象变成介绍 面向过程编程 核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西.主要应用在一旦完成很少修改的地方,如linux ...

  6. python面向对象类_python:面向对象(类)

    #!usr/bin/env python # -*- coding:utf-8 -*- __author__ = "Samson" ###类变量与实例变量 class Role: ...

  7. python面向对象类_python面向对象-类和对象

    一. 类的定义 class类名(): 代码 #定义类 classWasher():defwash(self):print("洗衣服") 注意:类名要满足标识符命名规则,同时遵循大驼 ...

  8. Python面向对象(类的成员之属性)

    day24 类的成员之属性 1 class Foo(): 2 def bar(self): 3 print("bar") 4 5 @property 6 def per(self) ...

  9. python 面向对象 类的内置方法

    判断是不是类cls的对象 class A:passa = A() print(isinstance(a,A)) 判断类sub是不是super的子类 class A:passclass B(A):pas ...

最新文章

  1. “天才少年”曹原再次连发2篇Nature!在魔角石墨烯取得系列新进展
  2. 2016年,新的开始
  3. 汇编语言——十种寻址方式
  4. wxWidgets:wxGridUpdateLocker类用法
  5. Windows程序闪退Windows日志捕获Kernelbase模块错误
  6. yum安装Mariadb,二进制安装Mariadb
  7. 详解如何在vue项目中引入饿了么elementUI组件
  8. 新硬盘挂载-fdisk+mount案例实操
  9. 维度退化(数据仓库)
  10. iOS-CoreText的那些事【电子书的那些事】
  11. Shader预处理宏、内置状态变量、多版本编译等
  12. easytrader 量化交易平台连接同花顺软件使用平安证券
  13. 分享一个不错的个人简历模板下载网站(word文件可编辑的)
  14. 高级计算机使用教程,计算器高级计算使用方法
  15. 清理谷歌浏览器注册表_Win10系统下注册表chrome残留无法删除
  16. portal服务器信息超时,某学校Agile Controller-Campus(V100R003C50SPC300)作为portal服务器用户反映认证通过后一定时间内掉线需重新认证问题排查...
  17. Android之WiFi连接检测
  18. debugview下无法显示kdprint log 的问题
  19. 关于Teacher Forcing、自回归和内部协变量偏移等概念梳理
  20. Python爬虫验证码识别四

热门文章

  1. 强化学习(十四) Actor-Critic
  2. WordCount运行详解
  3. Hadoop学习之MapReduce(四)
  4. Python入门100题 | 第067题
  5. 【Java设计模式】策略模式
  6. 如何制作并更改项目icon文件
  7. 美团点评业务之技术解密,日均请求数十亿次的容器平台
  8. 从技术角度谈一谈,我参与设计开发的手Q春节红包项目--转
  9. 分布式电子邮件系统设计--转载
  10. 【风险管理】假如我是风控经理,会搭建怎样的风控团队