Circle.py
import math
class Circle:
# Construct a circle object
def __init__(self, radius = 1):
self.radius = radius
def getPerimeter(self):
return 2 * self.radius * math.pi
def getArea(self):
return self.radius * self.radius * math.pi
def setRadius(self, radius):
self.radius = radius
ckp7.1-7.8
'''
ckp7.1
An object represents an entity in the real world that can be distinctly identified. For example, a
student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has
a unique identity, state, and behavior.
The relationship between classes and objects is analogous to that between an apple-pie recipe
and apple pies.
A Python class uses variables to store data fields and defines methods to perform actions.
A class is a contract—also sometimes called a template or blueprint—that defines what an
object’s data fields and methods will be.
An object is an instance of a class, and you can create many instances of a class. Creating
an instance of a class is referred to as instantiation. The terms object and instance are often
used interchangeably. An object is an instance and an instance is an object.
ckp7.2
Python uses the following syntax to define a class:
class ClassName:
initializer
methods
ckp7.3
The syntax for a constructor is:
ClassName(arguments)
ckp7.4
__init__
ckp7.5
All methods, including the initializer, have the first parameter self. This parameter refers
to the object that invokes the method. The self parameter in the _ _init_ _ method is
automatically set to reference the object that was just created. You can specify any name for this
parameter, but by convention self is usually used.
ckp7.6
ClassName(arguments)
It creates an object in the memory for the class.
It invokes the class’s _ _init_ _ method to initialize the object.
ckp7.7
In addition to using variables to store data fields and define methods, a class provides a special
method, __init__. This method, known as an initializer, is invoked to initialize a new object’s
state when it is created. An initializer can perform any action, but initializers are designed to perform
initializing actions, such as creating an object’s data fields with initial values.
ckp7.8
Usually you create an object and assign it to a variable. Later you can use the variable to
reference the object. Occasionally an object does not need to be referenced later. In this
case, you can create an object without explicitly assigning it to a variable.
'''
ckp7.9
class A:
def __init__(self, i):
self.i = i
def main():
a =A (20)
print(a.i)
main()
'''
A()里缺少了i的值,或者可以 def __init__(self, i=1),即给i赋一个初始值
'''
ckp7.10
'''
class A:
def __init__(self):
radius = 3
# 需要初始化
'''
class A:
def __init__(self):
self.radius = 3
def setRadius(self, radius):
self.radius = radius
# 初始化的地方需要加一个self
ckp7.11
class Count:
def __init__(self, count = 0):
self.count = count
def main():
c = Count()
times = 0
for i in range(100):
increment(c, times)
print("count is", c.count)
print("times is", times)
def increment(c, times):
c.count += 1
times += 1
main() # Call the main function
ckp7.12
class Count:
def __init__(self, count = 0):
self.count = count
def main():
c = Count()
n = 1
m(c, n)
print("count is", c.count)
print("n is", n)
def m(c, n):
c = Count(5)
n = 3
main() # Call the main function
ckp7.13
class A:
def __init__(self, i):
self.__i = i
def getI(self):
return self.__i
def main():
a = A(5)
print(a.getI())
main() # Call the main function
# 不可以直接调用__的内容,它是私有的,要使用get的方法
ckp7.14
def main():
a = A()
a.print()
class A:
def __init__(self, newS = "Welcome"):
self.__s = newS
def print(self):
print(self.__s)
main() # Call the main function
ckp7.15
class A:
def __init__(self, on):
self.__on = not on
def isOn(self):
return self.__on
def main():
a = A(False)
print(a.isOn())
main() # Call the main function
exe7.1
class Rectangle:
def __init__(self, width = 1, height = 2):
self.width = width
self.height = height
def getPerimeter(self):
return 2 * (self.width + self.height)
def getArea(self):
return self.width * self.height
def main():
a1 = Rectangle(4, 40)
a2 = Rectangle(3.5, 35.7)
print("width=", a1.width, "height=",a1.height, "Area=", a1.getArea(), "Perimeter=", a2.getPerimeter())
print(a2.width, a2.height, a2.getArea(), a2.getPerimeter())
main()
exe7.2
class Stock:
def __init__(self, previousClosingPrice, currentPrice, symbol = "", name = ""):
self.__symbol = symbol
self.__name = name
self.__previousClosingPrice = previousClosingPrice
self.__currentPrice = currentPrice
def getname(self):
return self.__name
def getsymbol(self):
return self.__symbol
def getpreviousClosingPrice(self):
return self.__previousClosingPrice
def getcurrentPrice(self):
return self.__currentPrice
def setpreviousClosingPrice(self, previousClosingPrice):
self.__previousClosingPrice = previousClosingPrice
def setcurrentPrice(self,currentPrice):
self.__currentPrice = currentPrice
def getChangePercent(self, previousClosingPrice, currentPrice):
return (self.__previousClosingPrice / self.__currentPrice)
def main():
Stock1 = Stock(previousClosingPrice = 20.5, currentPrice = 20.35,
symbol = "INTC", name = "Inter Corporation")
print("ChangePercent = ", format(Stock1.getChangePercent(20.5, 20.35) * 100, ".2f"), "%")
print("name =", Stock1.getname())
print("symbol =", Stock1.getsymbol())
main()
list7.2_TestCircle
from Circle import Circle
def main():
# Create a circle with radius 1
circle1 = Circle()
print("The area of the circle of radius",
circle1.radius, "is", circle1.getArea())
# Create a circle with radius 25
circle2 = Circle(25)
print("The area of the circle of radius",
circle2.radius, "is", circle2.getArea())
# Create a circle with radius 125
circle3 = Circle(125)
print("The area of the circle of radius",
circle3.radius, "is", circle3.getArea())
# Modify circle radius
circle2.radius = 100 # or circle2.setRadius(100)
print("The area of the circle of radius",
circle2.radius, "is", circle2.getArea())
main() # Call the main function
list7.4_TestTV
from TV import TV
def main():
tv1 =TV()
tv1.turnOn()
tv1.setChannel(30)
tv1.setVolume(3)
tv2 = TV()
tv2.turnOn()
tv2.channelUp()
tv2.channelUp()
tv2.volumeUp()
print("tv1's channel is", tv1.getChannel(),
"and volume level is", tv1.getVolumeLevel())
print("tv2's channel is", tv2.getChannel(),
"and volume level is", tv2.getVolumeLevel())
main() # Call the main function
list7.5_TestPassMutableObject
from Circle import Circle
def main():
# Create a Circle object with radius 1
myCircle = Circle()
# Print areas for radius 1, 2, 3, 4, and 5
n = 5
printAreas(myCircle, n)
# Display myCircle.radius and times
print("\nRadius is", myCircle.radius)
print("n is", n)
# Print a table of areas for radius
def printAreas(c, times):
print("Radius \t\tArea")
while times >= 1:
print(c.radius, "\t\t", c.getArea())
c.radius = c.radius + 1
times = times - 1
main() # Call the main function
list7.6_CircleWithPrivateRadius
import math
class Circle:
# Construct a circle object
def __init__(self, radius = 1):
self.__radius = radius
def getRadius(self):
return self.__radius
def getPerimeter(self):
return 2 * self.__radius * math.pi
def getArea(self):
return self.__radius * self.__radius * math.pi
TV.py
class TV:
def __init__(self):
self.channel = 1
self.volumeLevel = 1
self.on = False
def turnOn(self):
self.on = True
def turnOff(self):
self.on = False
def getChannel(self):
return self.channel
def setChannel(self, channel):
if self.on and 1 <= self.channel <= 120:
self.channel = channel
def getVolumeLevel(self):
return self.volumeLevel
def setVolume(self, volumeLevel):
if self.on and \
1 <= self.volumeLevel <= 7:
self.volumeLevel = volumeLevel
def channelUp(self):
if self.on and self.channel < 120:
self.channel += 1
def channelDown(self):
if self.on and self.channel > 1:
self.channel -= 1
def volumeUp(self):
if self.on and self.volumeLevel < 7:
self.volumeLevel += 1
def volumeDown(self):
if self.on and self.volumeLevel > 1:
self.volumeLevel -= 1