python基础——类

本文介绍了面向对象编程的基本概念,包括类的定义、属性与方法的使用、构造函数的作用及其实现方式。此外还探讨了如何限制类的属性,并通过实例演示了对象之间的交互。
''''' 
面对对象 
创建类 
格式: 
class 类名(父类列表):#首字母大写 
    属性 
    行为 
'''  
  
#object:基类,所有类的父类,一般没有合适的父类就写object  
class Human(object):  
    #定义属性(定义变量)(名词性属性)  
    name = " "  
    age = 0  
    height = 0  
    weight = 0  
  
    #定义方法(定义函数)  
    #方法的参数必须以self作为第一个参数  
    ''''' 
    不需要传参,run(self) 
    需要传参,run(sefl,x1,x2,x3)     
    '''  
    def run(self):  
        print('run')  
    def eat(self,food):  
        print('eat' + food)  
  
  
#实例化对象  
per1 = Human()  
print(per1)  
#>>>0x000......内存地址  
#类的种类  
print(type(per1))  
  
#访问属性  
#格式:对象名.属性名  
#赋值:对象名.属性名 = 新值  
per.name = 'Tom'  
per.age = 18  
per.height = 160  
per.weight = 80  
print(per.name,per.age,per.height,per.weight)  
  
#访问方法  
#格式:对象名.方法名(参数列表)  
per.openDoor()  
per.fillElephant()  
per.closeDoor()  
per.eat(apple)  
  
  
class Human(object):  
    def run(self):  
        print('run')  
    def eat(self,food):  
        print('eat' + food)  
  
#定义构造函数  
    def __init__(self,name,age,height,weight):  
        # print(name,age,height,weight)  
        #定义属性  
        self.name = name  
        self.age = age  
        self.height = height  
        self.weight = weight  
  
#构造函数  
''''' 
构造函数: __init__() 
在使用类创建对象的时候自动调用 
注意:如果不显示写出的构造函数,默认自动添加一个空的构造函数 
'''  
  
hu = Human('hmm',20,170,50)  
print(hu.name,hu.age,hu.height,hu.weight)  
hu2 = Human('Ll',21,180,70)  
print(hu2.name,hu2.age,hu2.height,hu2.weight)  
  
  
  
  
''''' 
构造函数 
self代表类的实例,不是类 
哪个对象调用方法,那么该方法中的self就代表那个对象 
self. __class__代表类名(两个下划线_) 
'''  
  
class Human(object):  
    def run(self):  
        print('run')  
    def eat(self,food):  
        print('eat' + food)  
    def say(self):  
#my name is %s.I am %d years old. %(s,d)  
        print("Hello! My name is %s,I am %d years old"%(self.name,self.age))  
    def __init__(self,name,age,height,weight):  
        self.name = name  
        self.age = age  
        self.height = height  
        self.weight = weight  
#self,访问自身属性  
  
hu1 = Human('Tom',20,160,80)  
hu1.say()  
  
#析构函数:__del__()释放对象时自动调用  
    def __del__(self):  
        print('析构函数')  
#函数运行完>>>析构函数  
  
''''' 
重写:__repr__:在机器里调用print 
__str__:在调用print打印对象时自动调用,给用户使用 
不写__str__,print打印类的地址 
'''  
  
#需要打印的内容很多,重写了__str__方法后简化代码  
def __repr__(self):  
    return "%s-%d-%d-%d"%(self.name, self.age, self.height, self.weight)  
  
print(hu1)  
  
      
#私有属性  
... ...  
self.__money = money  
    #通过内部方法修改私有属性money  
    #通过自定义方法实现对私有属性的赋值与取值  
    def setMoney(self,money):  
        #额外的数据过滤  
        if money < 0:  
            money = 0  
        self.__money = money  
    def setMoney(self):  
        return self.__money  
  
#让内部属性不在外部直接被访问  
#属性前加__两个下划线,变成私有属性  
hu.__money = 0  
print(hu.__money)  
hu.run()  
  
##射击,弹夹子弹减少  
class BulletBox(object):  
    def __init__ (self,count):  
        self.bulletCount = count  
  
class Person(object):  
    def __init__(self, gun):  
        self.gun = gun  
  
    def fire(self):  
        self.gun.shoot()  
  
class Gun(object):  
    def __init__(self, bulletBox):  
        self.bulletBox = bulletBox  
  
    def shoot(self):  
        if self.bulletBox.bulletCount == 0:  
            print('没有子弹了')  
        else:  
            self.bulletBox.bulletCount -= 1  
            print('剩余子弹:%d发'%(self.bulletBox.bulletCount))  
#弹夹,含有发子弹  
bulletBox = BulletBox(5)  
#枪,含有弹夹  
gun = Gun(bulletBox)  
#人,使用枪  
per = Person(gun)  
#射击  
per.fire()  
  
#多继承#  
#父类中方法名相同,默认调用括号中排前面的父类的方法。#  
from father import Father  
from mother import Mother  
  
class Child(Father, Mother):  
    def __init__(self, money, faceValue):  
  
        #调用Father里面init方法  
        Father.__init__(self, money)  
        #调用Mother里面init方法  
        Mother.__init__(self, faceValue)  
  
  
#限制类的属性  
#在类中添加属性  
from types import MethodType  
  
class Person(object):  
# 限制实例的属性  
# 只允许给对象添加name, age, weight, height  
# 在定义类的时候,定义一个特殊的属性(__slots__),可以限制动态添加的属性  
    __slots__ = ('name', 'age', 'speak')  
per = Person()  
#动态添加属性,体现了动态语言的灵活性  
per.name = 'tom'  
print(per.name)  
#动态添加函数,调用MethodType  
def say(self):  
    print('my name is ' + self.name)  
per.speak = MethodType(say, per)  
per.speak() 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值