ITxiaobaibai 2020-05-27
class Vehicle(object): trans_type = ‘SUV‘ def __init__(self, size, speed): self.size = size self.__speed = speed def show_info(self): print("我所属的类型为:{0},速度为:{1}km/h,体积为:{2}".format(self.trans_type, self.__speed, self.size)) def move(self): print("我已向前移动了50米!") def set_speed(self, new_speed): self.__speed = new_speed def get_speed(self): print("我的时速为:{0}".format(self.__speed)) def speed_up(self): self.__speed += 10 print("我的速度由{0}km/h提升到了{1}km/h".format(self.__speed - 10, self.__speed)) def speed_down(self): self.__speed -= 15 print("我的速度由{0}km/h下降到了{1}km/h".format(self.__speed + 15, self.__speed)) def transport_identify(name): if isinstance(name, Vehicle): print("类型匹配!") else: print("类型不匹配!") if __name__ == ‘__main__‘: v = (3.6, 1.9, 1.75) car = Vehicle(v, 20) car.show_info() car.set_speed(40) car.get_speed() car.speed_up() car.speed_down() transport_identify(car)
是一道经常出现在前端面试时的问题。如果只是简单的了解new关键字是实例化构造函数获取对象,是万万不能够的。更深入的层级发生了什么呢?同时面试官想从这道题里面考察什么呢?下面胡哥为各位小伙伴一一来解密。