栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中的类

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中的类

class People:#类名首字母必须大写
    say='hello'#类属性,每个对象都有类属性
    def __init__(self,name,sex):#初始化方法,第一个必须有self参数,表示创建的实例本身,不必为self传参
        self.name=name#初始化变量,两个实例属性
        self.sex=sex
    def print_name(self):#类方法(成员方法),第一个参数必须为self
        if self.name=='':
            print('请添加姓名')
        else:
            print(self.name)
if __name__=='__main__':
    people1=People('lisa','woman')#定义对象,并初始化(传入除self之外的参数)
    people2=People('','man')
    people1.print_name()#对象调用类中的方法
    people2.print_name()
    print(people1.say)#对象调用类属性
    print(People.say)#类调用类属性
    people1.say='你好'#对象定义一个与类属性同名的属性
    print(people1.say)#屏蔽类属性
    print(People.say)#但未改变类属性的值
    del people1.say#删除与类属性同名的对象属性
    print(people1.say)#又重新访问类属性
>>>lisa
>>>请添加姓名
>>>hello
>>>hello
>>>你好
>>>hello
>>>hello

一、类的内置方法
命名的时候在前后加入两个下划线:__init__
1、__init__
生成对象时,系统默认执行,使实例从一开始就拥有类模板所具有的属性
2、__str__
二、私有化
1、成员私有化,self.__name='lisa',属性名前加两个下划线,禁止在外部访问

class People:#类名首字母必须大写
    say='hello'#类属性,每个对象都有类属性
    def __init__(self):#初始化方法,第一个必须有self参数,表示创建的实例本身,不必为self传参
        self.__name='lisa'#初始化变量,两个实例属性
        self.sex='woman'
    def print_name(self):#类方法(成员方法),第一个参数必须为self
        if self.name=='':
            print('请添加姓名')
        else:
            print(self.name)
if __name__=='__main__':
    people1=People()
    people1.__name
>>>Traceback (most recent call last):
  File "F:/pycharm project/data_processing/python_/class_.py", line 37, in 
    people1.__name
AttributeError: 'People' object has no attribute '__name'

2、方法私有化,def __print_name(self)',方法名前加两个下划线,禁止在外部访问

class People:#类名首字母必须大写
    say='hello'#类属性,每个对象都有类属性
    def __init__(self):#初始化方法,第一个必须有self参数,表示创建的实例本身,不必为self传参
        self.name='lisa'#初始化变量,两个实例属性
        self.sex='woman'
    def __print_name(self):#类方法(成员方法),第一个参数必须为self
        if self.name=='':
            print('请添加姓名')
        else:
            print(self.name)
if __name__=='__main__':
    people1=People()
    people1.__print_name()
>>>Traceback (most recent call last):
  File "F:/pycharm project/data_processing/python_/class_.py", line 37, in 
    people1.__print_name
AttributeError: 'People' object has no attribute '__print_name'

三、类的继承
1、

#父类
class People:#类名首字母必须大写
    say='hello'#类属性,每个对象都有类属性
    def __init__(self,name,sex):#初始化方法,第一个必须有self参数,表示创建的实例本身,不必为self传参
        self.name=name#初始化变量,两个实例属性
        self.sex=sex
    def print_name(self):#类方法(成员方法),第一个参数必须为self
        if self.name=='':
            print('请添加姓名')
        else:
            print(self.name)
#子类
class Student(People):
    pass

if __name__=='__main__':
    people1=Student('lisa','woman')#实例化一个子类对象
    print(people1.name,people1.sex)#子类对象继承父类的初始化属性

    people1.print_name()#继承父类的方法
>>>lisa woman
>>>lisa

2、子类中创建与父类同名的方法(重写父类方法)

#父类
class People:#类名首字母必须大写
    say='hello'#类属性,每个对象都有类属性
    def __init__(self,name,sex):#初始化方法,第一个必须有self参数,表示创建的实例本身,不必为self传参
        self.name=name#初始化变量,两个实例属性
        self.sex=sex
    def print_name(self):#类方法(成员方法),第一个参数必须为self
        if self.name=='':
            print('请添加姓名')
        else:
            print(self.name)
#子类
class Student(People):
    def print_name(self):#子类中定义一个与父类的同名方法
        if self.name=='':
            print('please add name')
        else:
            print('the people name is:',self.name)

if __name__=='__main__':
    people1=Student('lisa','woman')#实例化一个子类对象
    people1.print_name()#调用子类方法
>>>the people name is: lisa
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530141.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号