1.面向对象三大特性,各有什么用处,说说你的理解
封装:将某个程序的属性和方法封装到抽象的类中,可以随时调用
多态:让代码多次使用,相同的代码可以实现不同的结果
继承:继承写好的代码,让新的代码实现有原代码的功能
2.面向过程编程与面向对象编程的区别?
面向对象:根据方法和对象写代码
面向过程:一步一步根据自己的需求写代码
3.python中经典类和新式类的区别?
新式类class(object)
经典类class()
4.请简单解释Python中 staticmethod(静态方法)和 classmethod(类方法)
静态方法不能改变类属性或者是类方法
类方法可以
5.示例, 现有如下代码, 会输出什么:
class People(object):
__name = "luffy"
__age = 18
p1 = People()
print(p1.__name, p1.__age)
程序将会报错,因为隐藏属性不能直接被访问
6.下面这段代码的输出结果将是什么?请解释。
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)
(1, 1, 1)
(1, 2, 1)
(3, 2, 3)
7.“Joker在BMW 4S店买了一俩BMW X7”,根据业务描述,声明相关类
class Person:
def __init__(self, name):
self.name = name
def buy(self,car):
print '%s 宝马4s店买%s' % (self.name,car)
joker = Person('joker')
joker.buy('bmw X7')
8.编写程序, A 继承了 B, 俩个类都实现了 handle 方法, 在 A 中的 handle 方法中调用 B 的 handle 方法
class B:
def handle(self):
print('BBBBBBBBBBBBBBBBBBBBBBBB')
class A(B):
def handle(self):
self.handle
print('AAAAAAAAAAAAAAAAAAAAAAAAA')
a = A()
a.handle()
9.编写一个学生类,产生一堆学生对象
要求:有一个计数器(属性),统计总共实例了多少个对象
class student(object):
count = 0
def __init__(self,name,age):
self.name = name
self.age = age
student.count += 1
@staticmethod
def count_student():
print 'there have %s students' % student.count
tom = student('weston',18)
harry = student('harry',19)
lily = student('lily',20)
student.count_student()
10.编写程序, 编写一个学生类, 要求有一个计数器的属性, 统计总共实例化了多少个学生
class student(object):
count = 0
def __init__(self,name,age):
self.name = name
self.age = age
student.count += 1
@staticmethod
def count_student():
print 'there have %s students' % student.count
tom = student('weston',18)
harry = student('harry',19)
lily = student('lily',20)
student.count_student()