ckp12.1
'''
先把父类导入进来,再在自己定义的类里包含进这个类名
super()可以继承父类的变量和方法
super().__init__()
'''
ckp12.2
class A:
def __init__(self, i = 0):
self.i = i
class B(A):
def __init__(self, j = 1):
super().__init__()
self.j = j
def main():
b = B()
print(b.i)
print(b.j)
main()
ckp12.3-12.4
'''
ckp12.3
True
ckp12.4
Python allows you to derive a subclass from several classed.
This capability is known as multiple inheritance.
class Subclass(SuperClass1, SuperClass2, ...):
initializer
methods
'''
ckp12.5
'''
(a) You can override a nonprivate method defined in a superclass.
True
(b) You can override a private method defined in a superclass.
False
(c) You can override the initializer defined in a superclass.
True
(d) When constructing an object from a subclass, its superclass’s initializer is automatically invoked.
True
'''
ckp12.6
class A:
def __init__(self, i = 0):
self.i = i
def m1(self):
self.i += 1
class B(A):
def __init__(self, j = 0):
super().__init__(3)
self.j = j
def m1(self):
self.i += 1
def main():
b = B()
b.m1()
print(b.i)
print(b.j)
main() # Call the main function
ckp12.7
'''
(a) Every object is an instance of the object class.
True
(b) If a class does not extend a superclass explicitly, it extends object by default.
True
'''
ckp12.8
class A:
def __init__(self, i = 0):
self.i = i
def m1(self):
self.i += 1
def __str__(self):
return str(self.i)
x = A(8)
print(x)
ckp12.9
class A:
def __new__(self):
print("A's __new__() invoked")
def __init__(self):
print("A's __init__() invoked")
class B(A):
def __new__(self):
print("B's __new__() invoked")
def __init__(self):
print("B's __init__() invoked")
def main():
b = B()
a = A()
main() # Call the main function
ckp12.10
class A:
def __new__(self):
self.__init__(self)
print("A's __new__() invoked")
def __init__(self):
print("A's __init__() invoked")
class B(A):
def __new__(self):
self.__init__(self)
print("B's __new__() invoked")
def __init__(self):
print("B's __init__() invoked")
def main():
b = B()
a = A()
main() # Call the main function
ckp12.11
class A:
def __init__(self):
print("A's __init__() invoked")
class B(A):
def __init__(self):
print("B's __init__() invoked")
def main():
b = B()
a = A()
main() # Call the main function
ckp12.12
class A:
def __init__(self, i):
self.i = i
def __str__(self):
return "A"
class B(A):
def __init__(self, i, j):
super().__init__(i)
self.j = j
def main():
b = B(1, 2)
a = A(1)
print(a)
print(b)
main() # Call the main function
ckp12.13
class A:
def __init__(self, i):
self.i = i
def __str__(self):
return "A"
def __eq__(self, other):
return self.i == other.i
def main():
x = A(1)
y = A(1)
print(x == y)
main() # Call the main function
ckp12.14
'''
encapsulation
In an object oriented python program, you can restrict access to methods and variables.
This can prevent the data from being modified by accident and is known as encapsulation.
inheritance
Inheritance extends the power of the object-oriented paradigm by adding an important and
powerful feature for reusing software. Suppose that you want to define classes to model
circles, rectangles, and triangles. These classes have many common features. What is the best
way to design these classes to avoid redundancy and make the system easy to comprehend
and maintain? The answer is to use inheritance.
polymorphism
Polymorphism means that an object of a subclass can be passed to a parameter of a
superclass type. A method may be implemented in several classes along the
inheritance chain. Python decides which method is invoked at runtime. This is known
as dynamic binding.
'''
ckp12.15
'''
class Person:
def getInfo(self):
return "Person"
def printPerson(self):
print(self.getInfo())
class Student(Person):
def getInfo(self):
return "Student"
Person().printPerson()
Student().printPerson()
'''
class Person:
def __getInfo(self):
return "Person"
def printPerson(self):
print(self.__getInfo())
class Student(Person):
def __getInfo(self):
return "Student"
Person().printPerson()
Student().printPerson()
ckp12.16
(a) Is goldenDelicious an instance of Fruit?
yes
(b) Is goldenDelicious an instance of Orange?
no
(c) Is goldenDelicious an instance of Apple?
yes
(d) Is goldenDelicious an instance of GoldenDelicious?
yes
(e) Is goldenDelicious an instance of McIntosh?
no
(f) Is orange an instance of Orange?
yes
(g) Is orange an instance of Fruit?
yes
(h) Is orange an instance of Apple?
no
(i) Suppose the method makeAppleCider is defined in the Apple class. Can
goldenDelicious invoke this method? Can orange invoke this method?
yes.
no.
(j) Suppose the method makeOrangeJuice is defined in the Orange class. Can
orange invoke this method? Can goldenDelicious invoke this method?
yes.
no.
'''
CircleFromGeometricObject.py
from GeometricObject import GeometricObject
import math # math.pi is used in the class
class Circle(GeometricObject):
def __init__(self, radius):
super().__init__()
self.__radius = radius
def getRadius(self):
return self.__radius
def setRadius(self, radius):
self.__radius = radius
def getArea(self):
return self.__radius * self.__radius * math.pi
def getDiameter(self):
return 2 * self.__radius
def getPerimeter(self):
return 2 * self.__radius * math.pi
def printCircle(self):
print(self.__str__() + " radius: " + str(self.__radius))
GeometricObject.py
class GeometricObject:
def __init__(self, color="green", filled=True):
self.__color = color
self.__filled = filled
def getColor(self):
return self.__color
def setColor(self, color):
self.__color = color
def isFilled(self):
return self.__filled
def setFilled(self, filled):
self.__filled = filled
def __str__(self):
return "color: " + self.__color + \
" and filled: " + str(self.__filled)
RectangleFromGeometricObject.py
from GeometricObject import GeometricObject
class Rectangle(GeometricObject):
def __init__(self, width = 1, height = 1):
super().__init__()
self.__width = width
self.__height = height
def getWidth(self):
return self.__width
def setWidth(self, width):
self.__width = width
def getHeight(self):
return self.__height
def setHeight(self, height):
self.__height = self.__height
def getArea(self):
return self.__width * self.__height
def getPerimeter(self):
return 2 * (self.__width + self.__height)
list12.4
from CircleFromGeometricObject import Circle
from RectangleFromGeometricObject import Rectangle
def main():
circle = Circle(1.5)
print("A circle", circle)
print("The radius is", circle.getRadius())
print("The area is", circle.getArea())
print("The diameter is", circle.getDiameter())
rectangle = Rectangle(2, 4)
print("\nA rectangle", rectangle)
print("The area is", rectangle.getArea())
print("The perimeter is", rectangle.getPerimeter())
main() # Call the main function
list12.5
from CircleFromGeometricObject import Circle
from RectangleFromGeometricObject import Rectangle
def main():
# Display circle and rectangle properties
c = Circle(4)
r = Rectangle(1, 3)
displayObject(c)
displayObject(r)
print("Are the circle and rectangle the same size?",
isSameArea(c, r))
# Display geometric object properties
def displayObject(g):
print(g.__str__())
# Compare the areas of two geometric objects
def isSameArea(g1, g2):
return g1.getArea() == g2.getArea()
main() # Call the main function
list12.6
class Student:
def __str__(self):
return "Student"
def printStudent(self):
print(self.__str__())
class GraduateStudent(Student):
def __str__(self):
return "Graduate Student"
a = Student()
b = GraduateStudent()
a.printStudent()
b.printStudent()