Python class and object
# Python继承
class Person(object):
"""人""" def __init__(self, name, age):
self._name = name
self._age = age @property
def name(self):
return self._name @property
def age(self):
return self._age @age.setter
def age(self, age):
self._age = age def play(self):
print('%s正在愉快的玩耍.' % self._name) def watch_av(self):
if self._age >= 18:
print('%s正在观看动作片.' % self._name)
else:
print('%s只能观看《熊出没》.' % self._name) class Student(Person):
"""学生""" def __init__(self, name, age, grade):
super().__init__(name, age)
self._grade = grade @property
def grade(self):
return self._grade @grade.setter
def grade(self, grade):
self._grade = grade def study(self, course):
print('%s的%s正在学习%s.' % (self._grade, self._name, course)) class Teacher(Person):
"""老师""" def __init__(self, name, age, title):
super().__init__(name, age)
self._title = title @property
def title(self):
return self._title @title.setter
def title(self, title):
self._title = title def teach(self, course):
print('%s%s正在讲%s.' % (self._name, self._title, course)) def main():
stu = Student('王二小', 15, '初三')
stu.study('数学')
stu.watch_av()
t = Teacher('Anthony', 38, '老叫兽')
t.teach('Python程序设计')
t.watch_av() if __name__ == '__main__':
main()
Python class and object的更多相关文章
- Debug 路漫漫-11:Python: TypeError: 'generator' object is not subscriptable
调试程序,出现以下错误: Python: TypeError: 'generator' object is not subscriptable “在Python中,这种一边循环一边计算的机制,称为生成 ...
- python pip 'nonetype' object has no attribute 'bytes'
python pip 'nonetype' object has no attribute 'bytes' 更新 pip for Windows : python -m pip install -U ...
- python everything is object
python面向对象非常彻底,即使过程式的代码风格,python在运作的时候也是面向对象的.everything is object. 差异 在面向对象的理念上,python和非常工程化的面向对象语言 ...
- Python TypeError: 'module' object is not callable 原因分析
今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...
- Python 中的object takes no parameters错误
Python是一门面向对象的语言,中我们首先创建一个类: class Student(object): def _init_(self,name,score): self.name = name se ...
- 【Python深入】Python中继承object和不继承object的区别
python中定义class的时候,有object和没有object的不同?例如: class Solution(object): class Solution(): 这俩的区别在于—————— 在p ...
- python: "TypeError: 'type' object is not subscriptable"
目前stackoverflow找到两种情况的解决办法: 1.TypeError: 'type' object is not subscriptable when indexing in to a di ...
- Python: TypeError: 'dict' object is not callable
问题: TypeError: 'dict' object is not callable 原因: dict()是python的一个内建函数,如果将dict自定义为一个python字典,在之后想调用 ...
- python 中的object与type的关系
object 和 type的关系很像鸡和蛋的关系,先有object还是先有type没法说,obejct和type是共生的关系,必须同时出现的. 在看下去之前,也要请先明白,在Python里面,所有的东 ...
- 【Python】unicode' object is not callable
在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.
随机推荐
- Ubuntu 源 (ros)
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://archive. ...
- ansible 问题记录(2)
普通用户执行ansible,但是在远程需要root权限,这个时候执行ansible命令报如下错误: 经分析是由于sudo的时候,普通用户没有在sudoer文件 2.在playbook里面使用sudo认 ...
- uiautomator代码例子--java
在androidtest下创建文件Ui2Test.java package com.example.myapplication; import android.app.Instrumentation; ...
- IDEA使用(03)_git撤回(已经commit未push的)操作
1.问题来源 日常工作中会遇到 commit 到本地仓库的代码,因为一些原因,需要撤销后再提交到本地,或者需要整合多次 commit,然后 push 到远程仓库. 2.IDEA解决方案 I.在idea ...
- vue---vue2.x自定义plugin,给vue添加全局方法,原型上增加全局方法
1. 自定义plugin.js export default{ install(Vue,options); { Vue.prototype.toStringTwo=(str)=>( ('0000 ...
- android studio 错误汇总以及解决办法
android studio 错误汇总以及解决办法 参考 https://www.jianshu.com/p/7c7de6562231 问题1. Error:Execution failed for ...
- 012-多线程-JUC集合-Queue-SynchronousQueue和LinkedTransferQueue
一.SynchronousQueue概述 SynchronousQueue是一个不存储元素的队列.每一个put操作必须等待一个take操作,否则不能继续添加元素. 它支持公平访问队列.默认情况下线程采 ...
- jquery图片播放插件Fancybox使用详解
今天给大家介绍的jquery图片播放插件叫Fancybox,相比LightBox来说,Fancybox相对庞大点,配置也更丰富一些,相信你会喜欢的. Fancybox的项目主页地址:http://fa ...
- DTCMS会员中心添加新页面
首先将模板文件spend_list.html放到模板文件夹 URL配置里 将需要在此页面传递的参数,在“添加表达式”中配置好 链接写法: 继承类名DTcms.Web.UI.Page.spend_lis ...
- c#关于Dictionary中自定义Key
Dictionary 描述 字典 Dictionary 通过 Hash 桶算法进行O(1)查找数据,在 Hash 碰撞达到一定次数后会自动进行 Resize,也会在数组大小不足的时候会自动进行Resi ...