zoukankan      html  css  js  c++  java
  • Python_面向对象

    '''

    1、面向对象概念
    2、类型定义
    2.0、继承(多态)
    2.1、类属性(类变量)定义
    2.2、实例属性(实例变量)定义
    2.3、构造方法的定义以及作用
    2.4、实例方法的定义【参数】
    2.5、私有化属性 【实现原理】
    2.6、__slots__属性声明限制
    2.7、类的专有方法的重写(定制类)【init、str、len、next、enter、exit、del】
    3、类型实例(生产对象)
    3.1、(封装)
    3.2、对象的信息获取(dir、hasattr、getattr、setattr、type、isinatance)【多态】
    3.3、*对象的属性使用(增加、删除、修改、查询)
    3.4、*实例属性与类属性的区别与联系
    3.5、实例方法的调用
    3.6、类属性的使用
    '''

    #例子一

    class Cat(object):
        '''这是个猫类型的声明
        包含属性有(name,age,color,pz)
        拥有的方法有(move、eat、speak)'''
        #__slots__=('name','age','color','pz')
        def __init__(self,name,age,color,pz):
            self.name,self.age,self.color,self.pz=name,age,color,pz
        def move(self,*node):
            for x in node:
                print('%s位置->'%x ,end='')
            print()
        def eat(self,food):
            print('我在吃%s'%food)
        def speak(self,number):
            print(''*number)
    
    a=Cat('花花',8,'','龙猫')
    a.name='小花'
    print(a.name)
    dir(a)
    print(type(a))
    print(isinstance(a,Cat))
    a.move('钟楼','小寨','高新','长安')
    a.eat('')
    a.speak(5)

    #例子2向量类型的简单声明

    class V(object):
        def __init__(self,*p):
            self.value=[]
            for x in p:
                if type(x)==type(1) or type(x)==type(0.1):
                    self.value.append(x)
                elif x.isdigit():self.value.append(int(x))
    
        def __add__(self,other):
            a=len(self.value)
            b=len(other.value)
            l=eval(str(other.value))  #eval是取出字符串里面的内容
            mix=self.value  
            if a>b:
                a=b
                l=eval(str(self.value))
                mix=other.value
            for i in range(a):
                l[i]=l[i]+mix[i]
    #        l=V(*l)     #实例化,
            return l
        def __str__(self):
            return str(self.value)
    a=V(1,2,3,4,5,6,7)
    b=V(1,2,3)
    c=a+b
    print(c)
    print(c.value)
  • 相关阅读:
    git的使用
    对大学学习的一些看法
    远程连接mysql失败情况总结
    缓存穿透、缓存击穿、缓存雪崩
    Hello Redis
    Celery的简单使用
    git操作
    码云、github同时配置ssh key,解决冲突问题
    阿里云短信验证码服务
    Vue中img标签的src属性绑定的坑
  • 原文地址:https://www.cnblogs.com/hellangels333/p/8167718.html
Copyright © 2011-2022 走看看