实现电路:

实现方法:

class LogicGate(object):
def __init__(self, n):
self.name = n
self.output = None def get_label(self):
return self.name def get_output(self):
self.output = self.perform_gate_logic()
return self.output class BinaryGate(LogicGate):
def __init__(self, n):
super().__init__(n)
self.pinA = None
self.pinB = None def get_pinA(self):
if self.pinA == None:
return int(input("输入PinA的值 " + self.get_label() + "-->"))
else:
return self.pinA.get_from().get_output() def get_pinB(self):
if self.pinB == None:
return int(input("输入PinB的值 " + self.get_label() + "-->"))
else:
return self.pinB.get_from().get_output() def set_next_pin(self, source):
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
print("无法连接:这个逻辑门没有空引脚") class AndGate(BinaryGate):
def __init__(self, n):
super().__init__(n) def perform_gate_logic(self):
a = self.get_pinA()
b = self.get_pinB()
return a & b class OrGate(BinaryGate):
def __init__(self, n):
super().__init__(n) def perform_gate_logic(self):
a = self.get_pinA()
b = self.get_pinB()
return a | b class UnaryGate(LogicGate):
def __init__(self, n):
super().__init__(n)
self.pin = None def get_pin(self):
if self.pin == None:
return int(input("输入Pin的值 " + self.get_label() + "-->"))
else:
return self.pin.get_from().get_output() def set_next_pin(self, source):
if self.pin == None:
self.pin = source
else:
print("无法连接:这个逻辑门没有空引脚") class NotGate(UnaryGate):
def __init__(self, n):
super().__init__(n) def perform_gate_logic(self):
a = self.get_pin()
return 0 if a else 1 class Connector(object):
def __init__(self, fgate, tgate):
self.from_gate = fgate
self.to_gate = tgate
tgate.set_next_pin(self) def get_from(self):
return self.from_gate def get_to(self):
return self.to_gate def main():
g1 = AndGate("U")
g2 = AndGate("U")
g3 = OrGate("U3")
g4 = NotGate("U")
c1 = Connector(g1, g2)
c2 = Connector(g2, g3)
c3 = Connector(g3, g4)
print(g4.get_output()) main()

python类模拟电路实现的更多相关文章

  1. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  2. 用python实现模拟登录人人网

    用python实现模拟登录人人网 字数4068 阅读1762 评论19 喜欢46 我决定从头说起.懂的人可以快速略过前面理论看最后几张图. web基础知识 从OSI参考模型(从低到高:物理层,数据链路 ...

  3. 【Python&数据结构】 抽象数据类型 Python类机制和异常

    这篇是<数据结构与算法Python语言描述>的笔记,但是大头在Python类机制和面向对象编程的说明上面.我也不知道该放什么分类了..总之之前也没怎么认真接触过基于类而不是独立函数的Pyt ...

  4. 初级模拟电路:4-1 BJT交流分析概述

    回到目录 BJT晶体管的交流分析(也叫小信号分析)是模拟电路中的一个难点,也可以说是模电中的一个分水岭.如果你能够把BJT交流分析的原理全都搞懂,那之后的学习就是一马平川了.后面的大部分内容,诸如:场 ...

  5. 细说python类3——类的创建过程

    细说python类3——类的创建过程 https://blog.csdn.net/u010576100/article/details/50595143 2016年01月27日 18:37:24 u0 ...

  6. python类的静态方法和类方法区别

    先看语法,python 类语法中有三种方法,实例方法,静态方法,类方法. # coding:utf-8 class Foo(object): """类三种方法语法形式&q ...

  7. python 类(object)的内置函数

    python 类(object)的内置函数 # python 类(object)的内置函数 ### 首先 #### 以__双下划线开头的内置函数 __ #### __往往会在某些时候被自动调用,例如之 ...

  8. python类、对象

    python类.对象 学习完本篇,你将会深入掌握 什么是类,对象 了解类和对象之间的关系 能独立创建一个People(人类),属性有姓名.性别.年龄.提供一个有参的构造方法,编写一个show方法,输出 ...

  9. Python类中super()和__init__()的关系

    Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...

随机推荐

  1. [20191011]拆分rowid 2.txt

    [20191011]拆分rowid 2.txt --//有了链接http://blog.itpub.net/267265/viewspace-2659612/=>[20191011]bash任意 ...

  2. 用python绘画一些简单图片

    python画笑脸 程序源代码 import turtle #画脸 t = turtle.Pen() t.speed(15) #t.circle(150) #t.color('orange') t.f ...

  3. 2018年最新Java面试题及答案整理

    基础篇 基本功 面向对象特征 封装,继承,多态和抽象 封装封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰符: pub ...

  4. dockerfile和资源限制(五)

    镜像生成途径 dockerfile 基于容器制作 什么是dockerfile dockerfile说白就是用来构建docker 镜像的源码,大家看到源码俩字不用惊慌,所为的dockerfile源码只是 ...

  5. python session保持登录,新增地址,并删除,由观察可知,address_id决定删除的内容;

    import requests,reheaders={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) Ap ...

  6. c/c++ 混合编程.so

    CC = gccC++ = g++LINK = g++ LIBS = -lz -lm -lpcre#must add -fPIC optionCCFLAGS = $(COMPILER_FLAGS) - ...

  7. Django之ContentType,GenericRelation, GenericForeignKey

    contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中. models.py文件的表结构写好后,通过makemigr ...

  8. 2019.6.11_MySQL进阶一:索引

    所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找.MySQL索引的建立对于MySQL的高效运行是很重要的,索引可 ...

  9. 当usbnet打印 kevent * may have been dropped(转)

    http://patchwork.ozlabs.org/patch/815639/ Every once in a while when my system is under a bit of str ...

  10. 基于Django的Rest Framework框架的频率组件

    0|1一.频率组件的作用 在我们平常浏览网站的时候会发现,一个功能你点击很多次后,系统会让你休息会在点击,这其实就是频率控制,主要作用是限制你在一定时间内提交请求的次数,减少服务器的压力. modle ...