Python3.4的面向对象

class Hello:
    # 构造方法 + 传入一个参数
    def __init__(self,name):
        self.name = name

    def sayHello(self):
        print("Hello {0}".format(self.name))

# 继承
class Hi(Hello):
    def __init__(self, name):
        Hello.__init__(self, name)

    def sayHi(self):
        print("Hi {0}".format(self.name))

# 创建一个Hello的实例
h = Hello("张三")
# 实例调用sayHello的方法
h.sayHello()

h1 = Hi("ZHANG SAN")
h1.sayHi()

你可能感兴趣的:(Python,python,面向对象)