<python>class
2017-01-14 16:13
232 查看
之前在学习神经网络,在调用现成的包时有时想对它进行一些修改,这就要用到Python的类继承,所以先来学下Python中类的定义和继承
class Example(): def __init__(self,parameter1,parameter2,...,parametern): self.parameter1 = parameter1 self.parameter2 = parameter2 ……………… //这一项是在之后调用class时将传递过来的参数赋给class内的元素 //self代表这个对象 def function1(self): print(self.parameter1) //之后定义的函数都是以self为参数,如果要调用self内的变量,就用self.parameter的形式 A = Example(a1,a2,...,an) //之后如果需要创造一个example类的函数,就用这种形式来调用,并传递给它初始值 print(A.parameter1) //如果要使用A内的变量,就用这种格式 A.function1() //要调用A内的函数,就用这种方式 一个具体的例子: class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def fill_gas_tank(): print("We have filled the gas tank.") my_new_car = Car('audi', 'a4', 2016) print(my_new_car.get_descriptive_name()) 如果是继承类,这个直接拿个例子来说吧 class ElectricCar(Car): //把父类名当参数 //如果不需要新增attribute的话 def __init__(self,make,model,year): super(ElectricCar,self).__init__(make,model,year) //如果需要新增attribute的话,就把attribute加到init的参数里,然后用常规方法添在super下面就好 def __init__(self,make,model,year,money): super(ElectricCar,self).__init__(make,model,year) self.money = money //如果需要override父类中的method,就定义一个同名的函数 def fill_gas_tank(): print("This car doesn't need a gas tank!")
相关文章推荐
- <<DEITEL - Python How to Program 2002>>Special attributes of an object of a class.0.0.0.3
- <深入理解Python中文版>读书笔记
- python面向对象编程-Class and Methods-<ThinkPython>
- <转>Python 多线程的单cpu与cpu上的多线程的区别
- <<Python基础教程>>学习笔记 | 第04章 | 字典
- <<Python基础教程>>学习笔记 | 第03章 | 字符串
- <<Python基础教程>>学习笔记 | 第12章 | 图形用户界面
- <Python>字典,可爱的字典
- <T> where T : class,new ()
- <<Python基础教程>>学习笔记 | 第06章 | 抽象
- <Java Class> 1.1数组初始化
- Python脚本控制的WebDriver 常用操作 <十> 层级定位
- <www.pythonchallenge.com>----Lv2
- <转>Python运行的17个时新手常见错误小结
- <转> amazon的一个大牛谈各种语言 c c++ java python ruby lisp perl
- <原>@Selector应用-不是对应的class却能调用其class方法
- <Think Python>中斐波那契使用memo实现的章节
- <实战> 分析PermGen上存放的被Classloader所加载的类实践 推荐
- <Python+Openpyxl>用Openpyxl做两个Excel文件的比对
- <<Python基础教程>>学习笔记 | 第09章 | 魔法方法、属性和迭代器