• 【python----发轫之始】【面向对象的实例化】


    汽车类型的实例化:

    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)

    学生类型的实例化:

    class Person(object):
    
        def __init__(self, name, gender):
            self.name = name
            self.gender = gender
    
        def speak(self):
            print("hello! 我是{0}同学!".format(self.name))
    
        def relation(self):
            pass
    
    
    class Student(Person):
    
        def __init__(self, name, gender, score, major):
            super(Student, self).__init__(name, gender)
            self.score = score
            self.major = major
            self.__num = "2018014002"
    
        def set_num(self, new_num):
            self.__num = new_num
    
        def speak(self):
            super(Student, self).speak()
            print("我的学号为:{0},很高兴认识大家!".format(self.__num))
    
        def identify_stu(self):
            if self.__num == "2018014002":
                print("您的分组已完成!")
            else:
                print("请稍后,马上为您自动分组....")
    
        def relation(self):
            if issubclass(Student, Person):
                print("我的父类是Person")
            else:
                print("父类正在查询中......")
    
    
    if __name__ == '__main__':
        stu = Student('小明', '', 90, '数学')
        stu.speak()
        stu.identify_stu()
        stu.relation()
        print("**************************************")
        stu1 = Student("小红", '', 100, '英语')
        stu1.set_num('2018040625')
        stu1.speak()
        stu1.identify_stu()

     图形数据多态实例化:

    class Point(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def string(self):
            print("{{X:{0}, Y:{1}}}".format(self.x, self.y), end='')
    
    
    class Circle(Point):
    
        def __init__(self, x, y, radius):
            super(Circle, self).__init__(x, y)
            self.radius = radius
    
        def string(self):
            print("该图形初始化点为:", end='')
            super(Circle, self).string()
            print("{{半径为:{0}}}".format(self.radius))
    
    
    class Size(object):
    
        def __init__(self, w, h):
            self.weight = w
            self.height = h
    
        def string(self):
            print("{{Weight:{0}, Height:{1}}}".format(self.weight, self.height))
    
    
    class Rectangle(Point, Size):
    
        def __init__(self, x, y, w, h):
            Point.__init__(self, x, y)
            Size.__init__(self, w, h)
    
        def string(self):
            print("该图形初始化点为:", end='')
            Point.string(self)
            print("该图形长宽分别为:", end='')
            Size.string(self)
    
    
    if __name__ == '__main__':
        circle = Circle(5, 5, 8)
        circle.string()
        rec = Rectangle(15, 15, 15, 15)
        rec.string()
        rec1 = Rectangle(40, 30, 11, 14)
        rec1.string()

     实例化时处理父类同名的例子:

    class People(object):
    
        def __init__(self, n, a):
            self.name = n
            self.age = a
    
        def speak(self):
            print("我叫{0},今年{1}岁".format(self.name, self.age))
    
    
    class Speaker(object):
    
        def __init__(self, n, c, t):
            self.name = n
            self.job = c
            self.topic = t
    
        def speak(self):
            print("我叫:{0},我是一个:{1},我演讲的主题是:{2}".format(self.name, self.job, self.topic))
    
    
    class Student(Speaker, People):
        pass
    
    
    if __name__ == '__main__':
        s = Student('Job', '演说家', 'python')
        s.speak()
        print("Student是否为Speaker的子类:", issubclass(Student, Speaker))
        print("Student是否为People的子类:", issubclass(Student, People))

    它会先调用先继承的类里面的函数。

  • 相关阅读:
    线程安全
    C++和java中构造函数与析构函数的调用顺序
    mysql数据库的备份与恢复
    Spring Web Flow 2.0 入门详解
    JBOSS 5.0与tomcat 6.0端口设置
    修改mysql root密码
    Subversion Native Library Not Available
    解决Eclipse导入svn项目自动关闭
    将eclipse中项目的Text File Encoding设置成为GBK
    java下实现调用oracle的存储过程和函数
  • 原文地址:https://www.cnblogs.com/Attacking-vincent/p/12974963.html
Copyright © 2020-2023  润新知