Python面向对象编程基础

时间:2022-12-20 14:44:06


#coding=utf-8

class hello:
    def __init__(self,NAME):
        self.name=NAME
    def sayhello(self):
        print("Hello, {0}".format(self.name))  #拼接字符串,也可以用print("Hello "+self.name)

#类的继承
class hi(hello): #父类用括号括起来
    def sayhi(self):
        print("Hi, {0}".format(self.name))

h=hello("张三")
h.sayhello();

h=hi("李四")
h.sayhi()

运行结果:

Hello, 张三
Hi, 李四