1. What is Class Inheritance?
Inheritance is the ability for one class to be related to another class. Python child classes can inherit characteristic data and behavior from a parent class.
Above structure is called Inheritacne hierarchy. This implies that lists inherit important characteristics from sequence, namely the ordering of the underlying data and the operations such as concatenation, repitition, and indexicng. The children all gain from their parent but distinguish themselves by adding additional characteristics.
- IS-A Relationship : The child class inherit important characteristics from parent characteristics.
- HAS-A Relationship : Two classes don't have inheritance relationship.
By organizing classes in this hierarchical fashion, object-orient programming languages allow previously written code to be extended to meet the needs of a new situation.
2. Implementing Logic Gates Class
At the top of the hierarchy, the Logic Gate class represents the most general characteristics of logic gates: namely, a label for the gate and an output line.
2.1 LogicGate Class
We can start to implement the classes by starting with the most general LogicGate. The needs for LogicGate are same as following :
- Label for identification
- A single output line
- The ability to know its output value.
However, in order to produce output, the gate needs to know specifically what that logic is. Because each gate will perform its own logic operation, we don't have to declare performGateLogic function in this class. And this is a very powrful idea in object-oriented programming.(We are writing a method that will use code that does not exist yet.)
class LogicGate:
def __init__(self, n):
self.label = n
self.output = None
def getLabel(self):
return self.label
def getOutput(self):
self.output = self.performGateLogic()
return self.output
2.2 BinaryGate/UnaryGate Class
The next level of subclass breaks the logic gates into two families, those that have one input line and those that have two.
class BinaryGate(LogicGate):
def __init__(self, n):
LogicGate.__init__(self, n)
# Also can super(BinaryGate, self).__init__(n)
self.pinA = None
self.pinB = None
def getPinA(self):
return int(input(f"Enter Pin A input for Gate {self.getLabel()} -->"))
def getPinB(self):
return int(input(f"Enter Pin B input for Gate {self.getLabel()} -->"))
class UnaryGate(LogicGate):
def __init__(self, n):
super(UnaryGate, self).__init__(n)
self.pin = None
def getPin(self):
return int(input(f"Enter Pin input for Gate {self.getLabel()}"))
The constructors in both of these classes start with an explicit call to the constructor of the parent class using the parent's __init__ method. In this case, that means the label for the gate. Then the constructor then goes on to add the input lines.
2.3 And/Or/NotGate Class
The only thing those class needs to add is the specific behavior that performs the boolean operation that was described. This is the place where we can provide he performGateLogic method.
Note that the AndGate class does not provide any new data since it inherits two input lines, one output lines, and a label.
class AndGate(BinaryGate):
def __init__(self, n):
super(AndGate, self).__init__(n)
def performGateLogic(self):
a = self.getpinA()
b = self.getpinB()
if a == 1 and b == 1:
return 1
else :
return 0
3. Implementing Circuits
Inorder to connect gates together, the output of one flows into the input of another. To do this, we will implement a new class called Connector.
With the Connector class, Connectors will have instances of the LogicGate class within them but are not part of the hierarchy. So, we can say that a Connector HAS-A LogicGate.
class Connector:
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
tgate.setNextPin(self)
def getFrom(self):
return self.fromgate
def getTo(self):
return self.togate
- fromgate : Rcognizing that data values will flow from.
- togate : Recognizing that data values will flow into.
- setNextPin : Making connections so that each togate can choose the proper input line for the connection.
4. Implemenation of Logic Gates and Circuits
class LogicGate:
def __init__(self, n):
self.name = n
self.output = None
def getLabel(self):
return self.name
def getOutput(self):
self.output = self.performGateLogic()
return self.output
class BinaryGate(LogicGate):
def __init__(self, n):
super(BinaryGate, self).__init__(n)
self.pinA = None
self.pinB = None
def getPinA(self):
if self.pinA = None:
return int(input(f"Enter Pin A input for gate {self.getLabel()} -->"))
else :
return self.pinA.getFrom().getOutput()
def getPinB(self):
if self.pinB = None:
return int(input(f"Enter Pin B input for gate {self.getLabel()} -->"))
class AndGate(BinaryGate):
def __init__(self, n):
BinaryGate.__init__(self, n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a == 1 and b == 1:
return 1
else:
return 0
class OrGate(BinaryGate):
def __init__(self, n):
BinaryGate.__init__(self, n)
def performGateLogic(self):
a = self.getPinA()
b = self.betPinB()
if a == 1 or b == 1:
return 1
else :
return 0
class UnaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pin = None
def getPin(self):
if self.pin == None:
return int(input("Enter Pin input for gate "+self.getLabel()+"-->"))
else:
return self.pin.getFrom().getOutput()
def setNextPin(self,source):
if self.pin == None:
self.pin = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
class NotGate(UnaryGate):
def __init__(self,n):
UnaryGate.__init__(self,n)
def performGateLogic(self):
if self.getPin():
return 0
else:
return 1
class Connector:
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
tgate.setNextPin(self)
def getFrom(self):
return self.fromgate
def getTo(self):
return self.togate
Source from : https://runestone.academy/ns/books/published/pythonds/Introduction/ObjectOrientedProgramminginPythonDefiningClasses.html
'Language > Python' 카테고리의 다른 글
[os] User Underlying Operating System (1) | 2022.10.03 |
---|---|
[sys] The way to import module not found (0) | 2022.10.03 |
[OOP] Creating Fraction Class using OOP (1) | 2022.09.30 |
[Syntax] Meaning of dot Notation (0) | 2022.09.26 |
[collections] Make frequency table automatically (1) | 2022.09.23 |