玩转Python语言之3:初探对象

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 29 22:54:42 2015

@author: Administrator
"""

"""我们已经见过的对象
a=[1,2,3]
b=a
b[0]=2
a[2]=4
print a
print b
"""

"""创建自己的类与对象
class MyClass:
    def __init__(self): 
        print "new object"
        self.name = "myclass"
a = MyClass()
print a.name
"""


"""给类加入方法
class MyClass:
    def __init__(self):
        print "new object"
        self.name = "myclass"
    def setname(self,name):
        self.name = name
    def printname(self):
        print self.name
a = MyClass()
a.setname("iPhone")
a.printname()
"""


"""继承"""
class MyClass:
    def __init__(self):
        print "new object"
        self.name ="myclass"
    def setname(self,name):
        self.name = name
    def printname(self):
        print self.name
class MyNewClass(MyClass):
    def setage(self,age):
        self.age = age
    def printage(self):
        print self.age
a = MyNewClass()
a.name ="Apple"
a.printname()
a.age = 18
a.printage()

 

posted @ 2015-09-29 23:41  Mander_Gao  阅读(143)  评论(0编辑  收藏  举报