class MyArray:
'''保证输入的内容是整型、浮点型'''
def ___isNumber(self, num):
if not isinstance(num, (int,float)):
return False
return True #开始写构造函数,接受可变长度的数组
def __init__(self, *args):
if args == None:
self.__value = []
else:
for a in args:
if not self.___isNumber(a):
print('All elements must be number!')
#self.__value 是一个数组
self.__value = list(args) #打印输出当前的self.__value
def printSelf(self):
#这个self是一个地址
print(self)
#这个self.__value是一个数组
print(self.__value) #重载len(Array)这个方法
def __len__(self):
return len(self.__value) #append方法
def append(self, other):
self.__value.append(other)
#注意:此处不能够直接return self.__value.append(other)
#这个方法执行后没有返回值
return self.__value #重载运算符+
def __add__(self,other):
if self.___isNumber(other):
#如果other 是一个数,则数组里每一个元素都加上other
array = MyArray()
array.__value = [ i + other for i in self.__value]
return array.__value
elif isinstance(other,MyArray):
#如果other 是一个数组,则两个数组对应位置的数相加
if (len(self.__value) == len(other.__value)):
array = MyArray()
array.__value = [i+j for i,j in zip(self.__value,other.__value)]
return array.__value
else:
print('The size must be equal!')
else:
print('Please input a array or a num!') #重载运算符 / 浮点数除法,返回浮点数
def __truediv__(self,other):
if self.___isNumber(other):
if other == 0:
print("Zero cant be this number!")
return
array = MyArray()
array.__value = [i / other for i in self.__value]
return array.__value
else:
print("It is must be a number except zero!") #重载运算符 // 整数除法,返回不大于结果的最大的一个整数
def __floordiv__(self,other):
if isinstance(other,int):
if other == 0:
print("Zero cant be this number!")
return
array = MyArray()
array.__value = [i // other for i in self.__value]
return array.__value
else:
print("Tt is must be a number except zero!") #重载运算符% 取余数
def __mod__(self,other):
if isinstance(other,int):
if other == 0:
print("Zero cant be this number!")
return
array = MyArray()
array.__value = [i % other for i in self.__value]
return array.__value
else:
print("Tt is must be a number!") #根据数组index查看元素
def __getitem__(self,index):
arrayLength = len(self.__value)
if isinstance(index,int) and (0 <= index <= arrayLength):
return self.__value[index]
else:
print("Index must be a Inteager which is less than", arrayLength-1) #查看元素是否在该列表
def __contains__(self,other):
if other in self.__value:
return True
return False #数组比较
def __lt__(self,other):
if not isinstance(other,MyArray):
print("It is must be the type of MyArray")
return False
if self.__value < other.__value:
return True
return False

Python自定义一个数组类,支持数组之间的四则运算和其他常见方法的更多相关文章

  1. 2017.12.13 Java中是怎样通过类名,创建一个这个类的数组

    先在类方法中定义数组的方法: public int[] method6(int[] arr){ for(int i = 0; i<arr.length;i++){ arr[i] = (int)( ...

  2. 用python构建一个多维维数组

    用python构建一个二维数组 解法? 方法1: num_list=[0]*x//表示位创建一个一维数组为num_lis[x],且数组中的每一项都为0 num_list=[[0]*x for i in ...

  3. C++自定义String字符串类,支持子串搜索

    C++自定义String字符串类 实现了各种基本操作,包括重载+号实现String的拼接 findSubStr函数,也就是寻找目标串在String中的位置,用到了KMP字符串搜索算法. #includ ...

  4. c++primer,自定义一个复数类

    #include<iostream> #include<string> #include<vector> #include<algorithm> #in ...

  5. Java自定义一个字典类(Dictionary)

    标准Java库只包含Dictionary的一个变种,名为:Hashtable.(散列表) Java的散列表具有与AssocArray相同的接口(因为两者都是从Dictionary继承来的).但有一个方 ...

  6. java中自定义一个异常类 在某些情况抛出自定的异常 ----------阻断程序

    //=============定义异常类 package org.springblade.flow.engine.errorException; /** * 自定义异常处理写入sap失败 */ pub ...

  7. python 分享一个通过 (key1.key2.key3) 形式获取嵌套字典值的方法

    最近在做接口自动化测试,响应的内容大多数是多层嵌套的json数据,如果一层层的去剥,效率不高,脚本繁重,所以写了一个可以通过(key1.key2.key3)形式获取嵌套字典值的方法,如有不对或者需要优 ...

  8. 12.创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方 法初始化x和y。创建类主类A来测试它。

    package java1; public class Point { int x; int y; Point(int x,int y) { this.x = x; this.y = y; } pub ...

  9. 创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方 法初始化x和y。创建类主类A来测试它

    package com.hanqi.test; public class Point { private int x; private int y; Point(int xx,int yy) { x= ...

随机推荐

  1. I.Algorithm Choosing Mushrooms

    链接:https://ac.nowcoder.com/acm/contest/908/I 题意: Baby bear and his good friends are very fond of mus ...

  2. NET Core 2.0 in Docker on Windows Containers

    安装Docker for Windows https://store.docker.com/editions/community/docker-ce-desktop-windows 要想将一个ASP. ...

  3. sqlserver 数据库 的数据库个数统计 表个数统计 表的数据量统计(转载)

    http://www.cnblogs.com/qinche/archive/2012/08/09/app.html 由于今天要监控数据,急需统计实例中1有多少库2库里有多少表3每个表有多少数据 --将 ...

  4. 解释器模式和php实现

    解释器模式: 给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 角色: 环境角色:定义解释规则的全局信息. 抽象解释器::定义了部分解释具体实现,封装了 ...

  5. Kendo 单页面应用(一)概述

    Kendo 单页面应用(一)概述 Kendo 单页面应用(Single-Page Application,缩写为 SPA)定义了一组类用于简化 Web 应用(Rich Client)开发,最常见的单页 ...

  6. Wrinkles should merely indicate where smiles have been.

    Wrinkles should merely indicate where smiles have been. 皱纹应该只是微笑留下的印记.

  7. GetRelativePath获取相对路径

    public static string GetRelativePath(string baseDirPath, string subFullPath) { // ForceBasePath to a ...

  8. MovieReview—Transformers.The.Last.Knight.(变形金刚5:最后的骑士.)

     Gorgeous Effect & Bad Plot   I can only say that the movie's effects are shocking. However, the ...

  9. 读书笔记2013-2 Linux内核设计与实现A

    读书笔记2013-2 Linux内核设计与实现A <Linux内核设计与实现> 简介 这本书不是想Linux源码剖析那样一行行分析Linux源代码的书,而是从Linux历史,Linux哲学 ...

  10. mysql命令行导出导入,附加数据库

    MySQL命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server ...