python类模拟电路实现
实现电路:

实现方法:
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类模拟电路实现的更多相关文章
- Python requests模拟登录
Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...
- 用python实现模拟登录人人网
用python实现模拟登录人人网 字数4068 阅读1762 评论19 喜欢46 我决定从头说起.懂的人可以快速略过前面理论看最后几张图. web基础知识 从OSI参考模型(从低到高:物理层,数据链路 ...
- 【Python&数据结构】 抽象数据类型 Python类机制和异常
这篇是<数据结构与算法Python语言描述>的笔记,但是大头在Python类机制和面向对象编程的说明上面.我也不知道该放什么分类了..总之之前也没怎么认真接触过基于类而不是独立函数的Pyt ...
- 初级模拟电路:4-1 BJT交流分析概述
回到目录 BJT晶体管的交流分析(也叫小信号分析)是模拟电路中的一个难点,也可以说是模电中的一个分水岭.如果你能够把BJT交流分析的原理全都搞懂,那之后的学习就是一马平川了.后面的大部分内容,诸如:场 ...
- 细说python类3——类的创建过程
细说python类3——类的创建过程 https://blog.csdn.net/u010576100/article/details/50595143 2016年01月27日 18:37:24 u0 ...
- python类的静态方法和类方法区别
先看语法,python 类语法中有三种方法,实例方法,静态方法,类方法. # coding:utf-8 class Foo(object): """类三种方法语法形式&q ...
- python 类(object)的内置函数
python 类(object)的内置函数 # python 类(object)的内置函数 ### 首先 #### 以__双下划线开头的内置函数 __ #### __往往会在某些时候被自动调用,例如之 ...
- python类、对象
python类.对象 学习完本篇,你将会深入掌握 什么是类,对象 了解类和对象之间的关系 能独立创建一个People(人类),属性有姓名.性别.年龄.提供一个有参的构造方法,编写一个show方法,输出 ...
- Python类中super()和__init__()的关系
Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...
随机推荐
- 【微信小程序】e.currentTarget和e.target
什么是事件 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 事件可以绑定在组件上,当达到触发事件,就会执行逻辑层中对应的事件处理函数. 事件对象可以携带额外信息,如 id ...
- jenkins在搭建中常见的问题
1.window环境读取不到chrom.exe System.setProperty("webdriver.chrome.driver","chromedriver.ex ...
- webpack中使用jquery
首先我只有一个js文件,只用在那个文件中引入,因此 一 下载包 npm install jquery --save 二 在使用的jquery的文件顶部输入js代码 import $ from 'jq ...
- nginx location 配置详解
指令作用 匹配指定的请求uri(请求uri不包含查询字符串,如http://localhost:8080/test?id=10,请求uri是/test) 语法形式 location [ = | ~ | ...
- jQuery—获取表单标签的数据值
获取设置input标签的值 <input class="form-control" type="text" id="username" ...
- MySQL 主从复制(实时热备)原理与配置
MySQL是现在普遍使用的数据库,但是如果宕机了必然会造成数据丢失.为了保证MySQL数据库的可靠性,就要会一些提高可靠性的技术.MySQL主从复制可以做到实时热备数据.本文介绍MySQL主从复制原理 ...
- ts开发环境搭建
ts为typescript的缩写,是javascript的超集. npm源改为国内 由于 Node 的官方模块仓库网速太慢,模块仓库需要切换到阿里的源. npm config set registry ...
- 如何让table中td与四周有间距
如何让table中td与四周有间距 方法一 在td下再添加一个会计元素 <tr> <td>第2节</td> <td>语文</td> < ...
- 05justify-content
display: flex; 的默认轴是x轴 justify-content: 设置主轴上的子元素排列的方式 所以在使用之前要确定好哪一个是主轴 /* justify-content:flex-sta ...
- luoguP1972 [SDOI2009]HH的项链
经典颜色问题推荐博文 https://www.cnblogs.com/tyner/p/11519506.html https://www.cnblogs.com/tyner/p/11616770.ht ...