类和对象---Python

构造类:

class people(object):
    name=""----属性
    age=0
    __weight=0----属性前加”--“表示私有属性,不可用于外部访问
    def __init__(self,n,a,w):---构造函数
        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):---继承people类
    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()
深复制与浅复制:

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)----浅复制,不复制内嵌对象,即box.corner与box2.corner指向同一引用
box3=copy.deepcopy(box)----深复制,会复制对象中引用的对象

原文地址:https://www.cnblogs.com/lwjl/p/4230508.html