随笔 - 1762  文章 - 0  评论 - 109  阅读 - 431万

python类初探

复制代码
class human:
    is_alive=True
    is_man=True
    def __init__(self,age):
        print('this is __init__() method, which will be executed automaticly!')
        self.nianling=age
        print('the age is: ',self.nianling)
    def setdata(self,name):
        self.name=name
    def printdata(self):
        print(self.name)
h=human(22)
#the running result is: 
'''
this is __init__() method, which will be executed automaticly!
the age is:  22
'''
View Code
复制代码

类的定义格式是:class leiming:

类的__init__(self)方法被任何实例自动调用,我称此方法为类的初始化函数。self是啥,这个参数是Python特有的,当__init__()被自动调用的时候,实例对象作为第一个参数被传递了进去。

复制代码
class dog():
    def __init__(self):
        print('init the class dog')
        print(self)
dog1=dog()
print(dog1)
'''
init the class dog
<__main__.dog object at 0x000000000234F278>
<__main__.dog object at 0x000000000234F278>
可以看到self的地址和dog1对象的地址是一样的。
'''
View Code
复制代码

类中包含属性和方法。所谓属性就是对类的特点进行定义。比如定义一个鸟类,这个类具有属性或者说特点:都有翅膀。

复制代码
class bird:
    def __init__(self):
        print('This is __init__() method, which will be executed automaticly!','\n'
              'All of us have wing!')
h=bird()
#the running result is:
'''
This is __init__() method, which will be executed automaticly! 
All of us have wing!s
'''
View Code
复制代码

类的属性可以定义在类内,也可以定义在类外,比如:

复制代码
class xin:
    pass
xin.name='杨过'
xin.age=22
xin.sex='male'
x=xin()
print('我的名字是:',x.name)
print('我的性别是:',x.sex)
print('我的年龄是:',x.age)
'''
我的名字是: 杨过
我的性别是: male
我的年龄是: 22
'''
View Code
复制代码

类的属性在类内定义,实例的属性通过self.shuxing=...    赋值定义。

复制代码
class sky:
    color='blue'
    def __init__(self,name):
        print('我会被自动执行的哦!')
        self.mingzi=name
    def f1(self,age):
        self.nianling=age
    def display(self):
        print('我的年龄是:',self.nianling)
p1=sky('杨过')
print(sky.color)
print('我的名字是:',p1.mingzi)
p1.f1(23)
p1.display()
'''
我会被自动执行的哦!
blue
我的名字是: 杨过
我的年龄是: 23
'''
View Code
复制代码

每个实例对象都会继承类的属性,并且可以在方法内通过self做赋值产生每个实例自己的属性。

复制代码
class sky:
    color='blue'
    def __init__(self,name):
        print('我会被自动执行的哦!')
        self.mingzi=name
    def f1(self,age):
        self.nianling=age
    def display(self):
        print('我的年龄是:',self.nianling)
p1=sky('杨过')
p2=sky('小龙女')
print(sky.color,p1.color)
print('我的名字是:',p1.mingzi)
print('我的名字是:',p2.mingzi)
p1.f1(23)
p2.f1(22)
p1.display()
p2.display()
'''
我会被自动执行的哦!
我会被自动执行的哦!
blue blue
我的名字是: 杨过
我的名字是: 小龙女
我的年龄是: 23
我的年龄是: 22
'''
View Code
复制代码

继承:

复制代码
class first:
    def me(self):
        print('I am first!')
class second(first):
    def me(self):
        print('I am second!')
        first.me(self)#你(类second)继承了它(类first),你便可以调用它的方法
        print('.....end......')
s1=second()
s1.me()
'''
I am second!
I am first!
.....end......
'''
View Code
复制代码

'类属性'这三个字的理解:

为什么要叫类属性呢,因为这个属性是和类绑定的,并不是和实例绑定的。胎生这个属性是全人类共有的,并不是某个人特殊拥有的属性。

复制代码
class Human:
    taisheng = True
p1=Human()
print(Human.taisheng)
'''
True
'''
View Code
复制代码

类内的初试化方法,每个类实力都可以随意修改里面的参数,这使得代码不健壮,封装性不好。此时把__init__()方法里面的属性之前附加两个下划线就可以阻止实例访问此方法了。

复制代码
class human:
    def __init__(self,name):
        self.name=name
    def walk(self):
        print(self.__name+' is running!')
p1=human('liudehua')
print(p1.name)
'''
实例正常访问__init__()方法中定义的实例属性
liudehua
'''


class human:
    def __init__(self,name):
        self.__name=name
    def walk(self):
        print(self.__name+' is running!')
p1=human('liudehua')
print(p1.__name)
'''
__init__()中定义实例的属性时,属性变量之前附加两个下划线,实例就无法再访问了。
Traceback (most recent call last):
  File "D:/good/s12_1/star/c.py", line 7, in <module>
    print(p1.__name)
AttributeError: 'human' object has no attribute '__name'
'''
View Code
复制代码

如果仍想访问,就再增加个get接口。

复制代码
class human:
    def __init__(self,name):
        self.__name=name
    def get_name(self):
        return self.__name
p1=human('林志玲')
print('One of the first beauty in society is :',p1.get_name())
'''
__init__()中定义实例的属性时,属性变量之前附加两个下划线,实例就无法再访问了。
 但可以再定义一个改变各个实例属性的接口get_name()函数:
One of the first beauty in society is : 林志玲
'''
View Code
复制代码

如果想修改已定义的实例的属性,就再定义个修改的接口:

复制代码
#如果想修改实例的属性,就再定义一个修个的接口:
class human:
    def __init__(self,name):
        self.__name=name
    def get_name(self):
        return self.__name
    def set_name(self,name):
        self.__name=name
p1=human('林志玲')
print(p1.get_name())
p1.set_name('王菲')
print(p1.get_name())

'''
林志玲
王菲
'''
View Code
复制代码

哺乳动物是动物的一种,哺乳动物是动物的子类,子类拥有父类的属性、方法,即继承。同时又可以拥有父类没有的属性和方法,即多态。

复制代码
class human:
    def __init__(self,name):
        self.__name=name
    def walk(self):
        print(self.name+' is running!')
    def get_name(self):
        return self.__name
    def set_name(self,name):
        self.__name=name
class man(human):#此为继承
    def __init__(self,name,has_wife):
        super(man,self).__init__(name)#super(Man, self).__init__(name)等价于调
        # 用了父类Human的构造函数,就不用再复制粘贴一遍self.__name=name了。
        self.has_wife=has_wife
    def smoke(self):
        print('Man like smoking!')
    def mustache(self):
        print('Man have to remove mustache everyday!')
class woman(human):#此为继承
    def __init__(self,name,has_husband):
        super(woman,self).__init__(name)
        self.has_hasband=has_husband
    def shopping(self):
        print('Woman like buying ,buying and buying!')
    def make_up(self):
        print('Woman like making up!')
m1=man('周杰伦','Yes')
m2=woman('林志玲','No')
print(m1.has_wife)
m1.smoke()
m1.mustache()
print(m2.has_hasband)
m2.shopping()
m2.make_up()
'''
Yes
Man like smoking!
Man have to remove mustache everyday!
No
Woman like buying ,buying and buying!
Woman like making up!
'''
View Code
复制代码

 

posted on   一杯明月  阅读(270)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示