Learn Python the hard way, ex45 对象、类、以及从属关系
2024-09-05 20:26:57
#!/usr/bin/python
#coding:utf-8
# animal is-a object(yes,sort of sonfusing)look at the extra credit
class Animal(objeck):
pass #Dog 类 继承 Animal
class Dog(Animal):
def __init__(self,name):
#对 对象Dog赋值 name
self.name =name
#Cat 类 继承 Animal
class Cat(Animal):
def __init__(self,name):
#对 对象 Cat 赋值 name
self.name=name #建立类 Person
class Person(objeck):
def __init__(self,name):
##对 对象 Person 赋值 name
self.name = name #Person has-a pet of some kind
self.pet = none #类 Employee 继承 Person
class Employee(Person):
def __init__(self,name,salary)
#? what is this strange magic?
#使用超级继承 super ,对 Employee 增加初始化 init
super(Employee,self).__init__(name)
# 赋值 salary
self.salary= salary #建 类 Fish
class Fish(objeck):
pass #Salmon 类 继承 Fish
class Salmon(Fish):
pass
#Halibut 类 继承 Fish
class Halibut(Fish):
pass #rover is-a Dog
rover = Dog('rover')
#satan is-a Cat
satan = Cat('satan')
#mary is-a Person
mary = Person('mary')
#mary has-a pet,pet(name) is satan
mary.pet = satan
#frank is-a Employee frank.salary = 12000
frank = Employee('frank',12000)
#fank has-a pet,pet(name) = rover
frank.pet rover
#flipper is-a Fish
flipper =Fish()
#crouse is-a Salmon
crouse =Salmon()
#harry is-a Halbut
harry = Halibut()
没有写别的
最新文章
- css 隔行变色,表单布局
- [Linux]服务管理:rpm包, 源码包
- no2.crossdomain.xml批量读取(待完善)
- HDU 5317 RGCDQ
- bootstrap完整导航栏
- 河南多校联合训练 南阳理工 1261 音痴又音痴的LT
- AVL树的插入与删除
- ul 和 ol 标签的相关设置
- IIS 常用命令
- 云服务jdk 升级为 OpenJDK11
- BZOJ.4589.Hard Nim(FWT)
- LeetCode OJ 94. Binary Tree Inorder Traversal
- centos 7 下 TFTP服务器安装
- Django实现微电影网站
- (原创)hibernate 一对多建表实例详解 附上各个注释的含义
- 请编写一个C函数,将一个字符串逆序
- iOS与硬件通讯(socket,data拼接,发送指令,解析指令)
- Jmeter性能指标分析
- 关于kafka的一些问题理解
- URIEncoding与useBodyEncodingForURI 在tomcat中文乱码处理上的区别