Python类

简介:

类(class)

python中的类是描述具有相同属性和方法的对象的集合类的一般形式:

class ClassName:

    '''类的说明'''

    类的内容

类的内容可以写类的全局变量,方法等

例子:

class ren(object):

    '''this is a new class'''

    name = 'meinv'

    sex = 'woman'

#类的实例化

a = ren()

print(type(a))

print(a.name)

print(a.sex)

a.age = 10

print(a.age)

结果:

<class '__main__.ren'>

meinv

woman

10


类的构造器

__init__ 构造函数,在生成对象时调用。由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去。通过定义一个特殊的__init__方法,在创建实例的时候,就把name,score等属性绑上去:

例子:

#所有类都继承object类,不写则默认是继承object类

class Student(object):

    #类的构造器,调用类的时候self可以不指定,python解释器默认将self指定到自身

    def __init__(self, name, score):

        self.name = nam

        self.score = score

例子2:

class Student(object):

def __init__(self, name, score):

self.name = name

self.score = score

s = Student('abc', 123)

print(s.name)

print(s.score)

输出:

abc


123


类的继承

Python的类支持多继承,多继承的格式如下,使用逗号分隔:

例子:

class A:

    ...

class B:

    ...
class C(A, B):

    ...
类的继承中,Python首先使用子类中的方法,如果子类中没有则去父类中寻找。变量也相同,首先寻找子类中的变量,没有找到后才去父类中寻找。

例子2:

class parent:

    name = 'parent'
    age = 100
    def __init__(self):
        print('my name is parent')
    def get_name(self):
        return self.name
    def get_age(self):
        return self.age
class child(parent):
    def __init__(self):
        print('my name is clild')
    def hello(self):
        print('hello child')
a = child()
a.hello()
print(a.get_name())
print(a.get_age())
输出:
my name is clild
hello child
parent
100


类的重写

Super()方法

#在class parent(object):定义父类的时候,一定要写继承object类,不然会报错可以解决

class parent(object):

    name = 'parent'

    age = 100

    def __init__(self):

        print('my name is parent')

    def get_name(self):

        return self.name

    def get_age(self):

        return self.age


class child(parent):

    def __init__(self):

        super(child, self).__init__()

        #parent.__init__()

        #使用父类.方法的方式直接重写,但是类中需要重写的内容较多时不方便

        print('my name is child.')

    def hello(self):

        print('hello child')

a = child()

a.hello()

print(a.get_name())

print(a.get_age())

输出:

my name is parent

my name is child

hello child

parent

100


类的私有变量

单下划线开头表示protected类型变量,在一个模块中以单下划线开头的变量和函数被默认当作内部函数。即保护类型只能允许其本身与子类进行访问。 当使用“from a_module import *”时,不会将以一个下划线开头的对象引入 ;如果使用 import a_module 这样导入模块,仍然可以用 a_module._some_var 这样的形式访问到这样的对象。

双下划线表示私有类型的变,只能允许这个类本身进行访问,连子类也不可以用。

前后双下划线定义的是特列方法。用户控制的命名空间内的变量或是属性,如__init__, __import__。只有当文档有说明时使用,不要自己定义这类变量。 (就是说这些是python内部定义的变量名)

例子

classPersonDemo():

    def __init__(self):

        self.__name=''

        self.__age=0

    def get_name(self):

        return self.__name

    def set_name(self,value):

        self.__name=value

a=PersonDemo()

a.set_name('kwsy')

print a.get_name()


内置类属性

__dict__ : 类的属性(包含一个字典,由类的数据属性组成)

__doc__ :类的文档字符串

__name__: 类名

__module__: 类定义所在的模块(类的全名是'__main__.className',如果类位于一个导入模块mymod中,那么className.__module__ 等于 mymod)

__bases__ : 类的所有父类构成元素(包含了以个由所有父类组成的元组)




本文转自 粗粮面包 51CTO博客,原文链接:http://blog.51cto.com/culiangmianbao/1979467,如需转载请自行联系原作者

相关文章
|
1月前
|
安全 Python
在Python中导入类
在Python中导入类
21 1
|
1天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
22天前
|
Python
Python类(class)中self的理解
Python类(class)中self的理解
16 0
|
22天前
|
Python
Python类与对象:深入解析与应用
本文介绍了Python中的核心概念——类和对象,以及它们在面向对象编程中的应用。类是用户定义的类型,描述具有相同属性和行为的对象集合;对象是类的实例,具备类的属性和方法。文章通过示例讲解了如何定义类、创建及使用对象,包括`__init__`方法、属性访问和方法调用。此外,还阐述了类的继承,允许子类继承父类的属性和方法并进行扩展。掌握这些概念有助于提升Python编程的效率和灵活性。
|
29天前
|
机器学习/深度学习 设计模式 开发者
python类用法(四)
python类用法(四)
17 0
|
29天前
|
Python
python类用法(三)
python类用法(三)
16 0
|
29天前
|
Python
python类用法(二)
python类用法(二)
16 0
|
29天前
|
存储 Java 数据安全/隐私保护
python类用法(一)
python类用法(一)
19 0
|
1月前
|
Python
在Python中,如何使用装饰器重写类的方法?
【2月更文挑战第24天】【2月更文挑战第79篇】在Python中,如何使用装饰器重写类的方法?
|
1月前
|
缓存 数据安全/隐私保护 Python
Python快速入门:类、文件操作、正则表达式
Python快速入门:类、文件操作、正则表达式