网站建设公司每年可以做多少个网站人际网络营销2900
1,面向对象三大特性,各有什么用处,说说你的理解。
1 2 3 4 5 |
|
2,类的属性和对象的属性有什么区别?
1 2 3 |
|
3,面向过程编程与面向对象编程的区别与应用场景?
1 2 3 4 5 |
|
4,类和对象在内存中是如何保存的。
1 |
|
5,什么是绑定到对象的方法、绑定到类的方法、解除绑定的函数、如何定义,如何调用,给谁用?有什么特性
1 2 3 4 5 6 |
|
6,使用实例进行 获取、设置、删除 数据, 分别会触发类的什么私有方法
|
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
7,python中经典类和新式类的区别
1 2 3 |
|
8,如下示例, 请用面向对象的形式优化以下代码
1 2 3 4 5 6 7 8 9 10 11 12 |
|
9,示例1, 现有如下代码, 会输出什么:
1 2 3 4 5 6 |
|
class People(object):__name = "luffy"__age = 18p1 = People()# print(p1.__name, p1.__age)# 报错:AttributeError: 'People' object has no attribute '__name'print(p1.__dict__) #{}print(People.__dict__)# {'__module__': '__main__', '_People__name': 'luffy',# '_People__age': 18, '__dict__': <attribute '__dict__' of 'People' objects>,# '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}print(p1._People__name,p1._People__age) # luffy 18
10,示例2, 现有如下代码, 会输出什么:
1 2 3 4 5 6 7 8 9 10 |
|
1 2 |
|
11,请简单解释Python中 staticmethod(静态方法)和 classmethod(类方法), 并分别补充代码执行下列方法。
静态方法:非绑定方法,类和对象都可以调用
类方法:绑定给类的方法啊,类调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
12,请执行以下代码,解释错误原因,并修正错误。
错误原因:@property可以将函数属性转化为数据属性
1 2 3 4 5 6 7 8 9 10 11 |
|
报错内容:
1 2 3 4 |
|
改正如下:
1 2 3 4 5 6 7 8 9 10 11 |
|
因为有了property,用户就可以直接输入得到结果,对于使用者来说,感知不到其属性,这就是使用property的方便之处,此方法必须有一个返回值。return 或者print都可以。
为什么要用property?
一个类的函数定义成特性以后,对象再去使用的时候obj.name,根本无法察觉自己的name是执行了一个函数然后计算出来的,这种特性的使用方式遵循了统一访问的原则
13,下面这段代码的输出结果将是什么?请解释。
class Parent(object):x = 1class Child1(Parent):passclass Child2(Parent):passprint(Parent.x, Child1.x, Child2.x)Child1.x = 2print(Parent.x, Child1.x, Child2.x)Parent.x = 3print(Parent.x, Child1.x, Child2.x)# 1 1 1 继承自父类的类属性x,所以都一样,指向同一块内存地址# 1 2 1 更改Child1,Child1的x指向了新的内存地址# 3 2 3 更改Parent,Parent的x指向了新的内存地址
14,多重继承的执行顺序,请解答以下输出结果是什么?并解释。
super()表示的是 子类的mro()列表中的下一个print(G.mro())[<class '__main__.G'>, <class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]print(F.mro())[<class '__main__.F'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>]
class A(object):def __init__(self):print('A')super(A, self).__init__()class B(object):def __init__(self):print('B')super(B, self).__init__()class C(A):def __init__(self):print('C')super(C, self).__init__()class D(A):def __init__(self):print('D')super(D, self).__init__()class E(B, C):def __init__(self):print('E')super(E, self).__init__()class F(C, B, D):def __init__(self):print('F')super(F, self).__init__()class G(D, B):def __init__(self):print('G')super(G, self).__init__()if __name__ == '__main__':g = G()f = F()# G
# D
# A
# B
#
# F
# C
# B
# D
# A
15,请编写一段符合多态特性的代码.
class Animal:def __init__(self, name):self.name = nameclass People(Animal):def talk(self):print('%s is talking' % self.name)class Dog(Animal):def talk(self):print('%s is talking' % self.name)def func(animal):animal.talk()# p=People('alice')
# d=Dog('wang')
# func(p)
# func(d)
'''
alice is talking
wang is talking
'''
16,很多同学都是学会了面向对象的语法,却依然写不出面向对象的程序,原因是什么呢?原因就是因为你还没掌握一门面向对象设计利器,即领域建模,请解释下什么是领域建模,以及如何通过其设计面向对象的程序?http://www.cnblogs.com/alex3714/articles/5188179.html 此blog最后面有详解
1 2 3 4 5 6 7 8 9 10 11 |
|
17,请写一个小游戏,人狗大站,2个角色,人和狗,游戏开始后,生成2个人,3条狗,互相混战,人被狗咬了会掉血,狗被人打了也掉血,狗和人的攻击力,具备的功能都不一样。注意,请按题14领域建模的方式来设计类。
+ View Code
18,编写程序, 在元类中控制把自定义类的数据属性都变成大写.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
19,编写程序, 在元类中控制自定义的类无需init方法.
class Mymeta(type):def __new__(cls,class_name,class_bases,class_dic):update_dic = {}for i in class_dic:if not callable(class_dic[i]) and not i.startswith('__'):update_dic[i.upper()]=class_dic[i]else:update_dic[i]=class_dic[i]return type.__new__(cls,class_name,class_bases,update_dic)def __call__(self, *args, **kwargs):obj=object.__new__(self)if args:raise TypeError('must be keyword argument')for i in kwargs:obj.__dict__[i]=kwargs[i]return objclass Chinese(metaclass=Mymeta):country='china'tag='legend of the dragon'def talk(self):print('%s is talking'%self.name)# print(Chinese.__dict__)
# ch=Chinese(name='alice',age=18)
# ch.talk()
# print(ch.__dict__)
20,编写程序, 编写一个学生类, 要求有一个计数器的属性, 统计总共实例化了多少个学生.
class Student:__count = 0def __init__(self, name, age):self.name = nameself.age = ageStudent.__count += 1@propertydef talk(self):print('%s is talking' % self.name)@staticmethoddef tell_count():print('总共实例化了 %s 人' % Student.__count)# s1 = Student('alice', 18)
# s2 = Student('alex', 20)
# s3 = Student('egon', 28)
# Student.tell_count()
# s1.tell_count()
# s1.talk
# s2.talk
结果:
1 2 3 4 5 6 7 8 9 |
|
# _*_ coding: utf-8 _*_
'''
练习1:编写一个学生类,产生一堆学生对象, (5分钟)
要求:
有一个计数器(属性),统计总共实例了多少个对象
'''
class Student:count = 0def __init__(self,name,sex,age):self.name = nameself.sex = sexself.age = ageStudent.count +=1def learn(self):print("%s is learning"%self.name)stu1 = Student('james','male',32)
print(stu1.count)
print(stu1.__dict__)
stu2 = Student('enbede','male',23)
print(stu2.count)
print(stu2.__dict__)
print(Student.count)
print(Student.__dict__)
# 结果:
# 1
# {'name': 'james', 'sex': 'male', 'age': 32}
# 2
# {'name': 'enbede', 'sex': 'male', 'age': 23}
# 2
# {'__module__': '__main__', 'count': 2, '__init__': <function Student.__init__ at 0x000001E5DA9759D8>, 'learn': <function Student.learn at 0x000001E5DA975A60>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
#
# Process finished with exit code 0
21,编写程序, A 继承了 B, 俩个类都实现了 handle 方法, 在 A 中的 handle 方法中调用 B 的 handle 方法
class B:def handle(self):print('from B handle')class A(B):def handle(self):super().handle()# print('from A handle')
# a=A()
# a.handle()
22,编写程序, 如下有三点要求:
- 自定义用户信息数据结构, 写入文件, 然后读取出内容, 利用json模块进行数据的序列化和反序列化
- 定义用户类,定义方法db,例如 执行obj.db可以拿到用户数据结构
- 在该类中实现登录、退出方法, 登录成功将状态(status)修改为True, 退出将状态修改为False(退出要判断是否处于登录状态).密码输入错误三次将设置锁定时间(下次登录如果和当前时间比较大于10秒即不允许登录)
import json
import time
class User:def __init__(self, name, password):self.name = nameself.password = passwordself.status = Falseself.timeout = 0@propertydef db(self):with open(self.name+'.txt', 'r', encoding='utf-8') as f:data = json.load(f)return datadef save(self):obj={}obj[self.name]={'password':self.password,'status':self.status,'timeout':self.timeout}with open(self.name+'.txt', 'w', encoding='utf-8') as f:json.dump(obj,f)def login(self):with open(self.name+'.txt', 'r+', encoding='utf-8') as f:data = json.load(f)count = 0while count < 3:password = input('password>>:').strip()if password != data[self.name]['password']:count += 1continueelse:if data[self.name]['timeout'] != 0:if time.time() - data[self.name]['timeout'] > 10:print('不允许登录了!超时')breakelse:data[self.name]['status'] = Truef.seek(0)f.truncate()json.dump(data, f)print('----welcome----')breakelse:data[self.name]['status'] = Truef.seek(0)f.truncate()json.dump(data, f)print('----welcome----')breakelse:data[self.name]['timeout']=time.time()f.seek(0)f.truncate()json.dump(data,f)def quit(self):with open(self.name+'.txt', 'r+', encoding='utf-8') as f:data = json.load(f)if data[self.name]['status'] == True:data[self.name]['status'] = Falsef.seek(0)f.truncate()json.dump(data, f)else:print('您是退出状态!')# alex=User('alex','123')
# egon=User('egon','456')
# # alex.save()
# # egon.save()
# # print(alex.db)
# # print(egon.db)
# # alex.login()
# alex.quit()
# # egon.quit()
# print(alex.db)
# print(egon.db)# alex.login()
# egon.login()
23,用面向对象的形式编写一个老师角色, 并实现以下功能, 获取老师列表, 创建老师、删除老师、创建成功之后通过 pickle 序列化保存到文件里,并在下一次重启程序时能读取到创建的老师, 例如程序目录结构如下.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
24,根据23 题, 再编写一个班级类, 实现以下功能, 创建班级, 删除班级, 获取班级列表、创建成功之后通过 pickle 序列化保存到文件里,并在下一次重启程序时能读取到创建的班级.
25,根据 23题, 编写课程类, 实现以下功能, 创建课程(创建要求如上), 删除课程, 获取课程列表
26,根据23 题, 编写学校类, 实现以下功能, 创建学校, 删除学校, 获取学校列表
27,通过23题, 它们雷同的功能, 是否可以通过继承的方式进行一些优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
28:编写一个学生类,产生一堆学生对象
要求:有一个计数器(属性),统计总共实例了多少个对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
29:模仿王者荣耀定义两个英雄类
要求:
- 英雄需要有昵称、攻击力、生命值等属性;
- 实例化出两个英雄对象;
- 英雄之间可以互殴,被殴打的一方掉血,血量小于0则判定为死亡。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|