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. Codeforces 1175F(哈希后暴力)

    要点 官解使用的哈希,给每个数一个二维键值,这样每个排列就有唯一的键值,再预求一下所给数列的前缀键值,暴力寻找有多少个答案即可. #include <cstdio> #include &l ...

  2. 【手撸一个ORM】第八步、查询工具类

    一.实体查询 using MyOrm.Commons; using MyOrm.DbParameters; using MyOrm.Expressions; using MyOrm.Mappers; ...

  3. P2737 [USACO4.1]麦香牛块Beef McNuggets 数学题 + 放缩思想

    https://www.luogu.org/problem/show?pid=2737#sub 先说一个结论:对于两个数p, q,且gcd(p, q) = 1(这个很重要,是条件来的).他们不能组合成 ...

  4. CQRS之旅——前言(翻译)

    探索CQRS和Event Sourcing 本项目聚焦在使用命令和查询分离模式和事件溯源(CQRS+Event Sourcing)构建一个具有高扩展,高可用和高维护性的应用程序. 本项目定位为一个学习 ...

  5. Java 数字数组随机数工具类 NumberUtils、ArrayUtils、RandomUtils用法

    commons-lang3-3-3.8.1 //----------------------------------------------------------------------- /** ...

  6. java构造方法之我见

    java中构造方法是作为除了成员方法之外的一种特殊方法,方法名与类名相同.一般类中如果没有明确定义构造方法时,编译器默认为无参构造方法.当我们调用new方法创建对象就是通过构造方法完成的.因此,当有对 ...

  7. JSON 序列化格式

    一.C#处理简单json数据json数据: 复制代码代码如下: {"result":"0","res_info":"ok" ...

  8. POJ 1185 炮兵阵地 (状压DP,轮廓线DP)

    题意: 给一个n*m的矩阵,每个格子中有'P'或者'H',分别表示平地和高原,平地可以摆放大炮,而大炮的攻击范围在4个方向都是2格(除了自身位置),攻击范围内不能有其他炮,问最多能放多少个炮?(n&l ...

  9. 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...

  10. SAP公有云和私有云解决方案概述

    SAP公有云解决方案见下图最右侧,比较著名的有SAP SuccessFactors和SAP Cloud for Customer(C4C)等,作为SAP软件即服务(SaaS)的解决方案. 而最左侧的S ...