一、#方法# 两种语言使用的对比
1.1 python:
因为python是支持面向对象编程的,所以我们可以先定义类,在类中绑定属性(数据),再通过数据封装(定义访问数据的函数),也即类的方法。最终实例化后实现了方法的使用。
- 定义Person类
- 绑定firstname和lastname属性
- 定义类中的函数printme,打印firstname和lastname
- 实例化后,调用printme方法
#默认继承object类
class Person(object):
#通过__init__方法强制绑定必要的属性,self表示创建实例的本身
def __init__(self,firstname,lastname):
self.firstname=firstname
self.lastname=lastname
def printme(self):
print("person name: "+self.firstname+" "+self.lastname)
#初始化实例并调用方法
p=Person('michael','hu')
p.printme()
【 python代码1 】
1.2 golang:
由于golang没有类概念,不能使用面向对象的方法。所以先要定义结构体(可理解为python中通过init绑定的一组数据属性的集合),再定义方法(比函数多了接收者)
- 定义结构体Person,包含firstname,lastname属性
- 定义Pri