Construct class:
class people (object):
Name= ""----property
Age=0
__weight=0----attribute with "--" indicates a private property and is not available for external access
def __init__ (SELF,N,A,W):---Constructors
Self.name=n
Self.age=a
Self.__weight=w
def speak (self):
Print ("My name is%s. I am%d years old "% (self.name,self.age))
Class student (people):---Inheriting people classes
Grade=0
def __init__ (self, n, A, w,g):
People.__init__ (self, n, a, W)
Self.grade=g
def speak (self):
Print ("My name is%s. I am%d years old,my score is%d "% (Self.name,self.age,self.grade))
S=student (' Tom ', 12,40,100)
S.speak ()
Deep copy with shallow copy:
Import Copy
Class Point ():
"" "Attributes:x,y" "" "
Class Retangle ():
"" "Attributes:width,height,corner" "" "
Box=retangle ()
box.width=100
box.height=200
Box.corner=point ()
Box.corner.x=0
Box.corner.y=0
Box2=copy.copy (box)----shallow copy, do not copy inline objects, that is, Box.corner and Box2.corner point to the same reference
Box3=copy.deepcopy (box)----deep copy, copying objects referenced in the object
Classes and Objects---Python