Python核心编程笔记(类)

时间:2021-09-01 19:42:33

Python并不强求你以面向对象的方式编程(与Java不同)

# coding=utf8

class FooClass(object):

    version = 0.1

    def __init__(self, nm='John Doe'):
        self.name = nm
        print('Created a class instance for', nm)

    def showname(self):
        print('Your name is ', self.name)
        print('My name is ', self.__class__.__name__)

    def shower(self):
        print(self.version)

    def addMe2Me(self, x):
        return (x + x)

foo1 = FooClass()
foo1.showname()
print(foo1.shower())
print(foo1.addMe2Me(5))
print(foo1.addMe2Me('xyz'))

foo2 = FooClass('Jane Smith')
foo2.showname()