Python/gloss python parent class

来自菜鸟教程
跳转至:导航、​搜索

<languages />

Python创建父类

创建一个家长班

任何类都可以是父类,因此语法与创建任何其他类相同:

创建一个名为的类 Person ,与 firstname and lastname 属性和 printname 方法:

class Person:
  def __init__(self, fname, lname):
    
  self.firstname = fname
    self.lastname = lname

  
  def printname(self):
    print(self.firstname, 
  self.lastname)

#Use the Person class to create an object, and then 
  execute the printname method:

x = Person("John", "Doe")

  x.printname()