zoukankan      html  css  js  c++  java
  • 26-Python3 面向对象

    26-Python3 面向对象

    '''
    面向对象技术简介
    '''
    
    '''
    类定义
    '''
    
    '''
    类对象
    '''
    class MyClass:
        i = 12345
        def f(self):
            return 'hello,runoob'
    
    x = MyClass()  #实例化类
    print('类的属性为:',x.i) #访问类的属性
    print('类的方法为:',x.f()) #访问类的方法
    
    
    class Complex:
        def __init__(self,realpart,imagpart):
            self.r = realpart
            self.i = imagpart
    x = Complex(3.0,-4.5)
    print(x.r,x.i)
    
    
    
    '''
    self代表类的实例,而非类
    '''
    class Test:
        def prt(self):
            print(self)
            print(self.__class__)
    t = Test()
    t.prt()
    
    '''
    类的方法
    '''
    class people:
        name = ''
        age = 0
        __weight =0
        def __init__(self,n,a,w):
            self.name = n
            self.age = a
            self.__weight = w
        def speak(self):
            print('{}说:我{}岁了!'.format(self.name,self.age))
    
    p = people('Runoob',10,30)
    p.speak()
    
    '''
    继承
    '''
    class People:
        name = ''
        age = 0
        __weight = 0
        def __init__(self,n,a,w):
            self.name = n
            self.age = a
            self.__weight = w
        def speak(self):
            print('{}说:我今年{}岁了'.format(self.name,self.age))
    
    class Student(People):
        grade = ''
        def __init__(self,n,a,w,g):
            People.__init__(self,n,a,w)
            self.grade = g
        def speak(self):
            print('{}说:我今年{}岁了,在读{}年级'.format(self.name,self.age,self.grade))
    
    s = Student('Joo',23,90,'6')
    s.speak()
    
    
    
    
    
    '''
    多继承,困了,累累,写遍下面读代码
    '''
    class people3:
        name = ''
        age = 0
        __weight = 0
        def __init__(self,n,a,w):
            self.name = n
            self.age = a
            self.__weight = w
        def speak(self):
            print('{}说:我{}了'.format(self.name,self.age))
    
    
    #单继承
    class student3(people3):
        grade = ''
        def __init__(self,n,a,w,g):
            people3.__init__(self,n,a,w)
            self.grade = g
        def speak(self):
            print('{}说:我{}岁了,在读{}年级'.format(self.name,self.age,self.grade))
    
    #另外一个类
    class speaker:
        name = ''
        toptic = ''
        def __init__(self,n,t):
            self.name = n
            self.toptic = t
        def speak(self):
            print('我叫{},我是一个演说家,我今天演讲的主题为{}'.format(self.name,self.toptic))
    
    #多重继承
    class sample(speaker,student3):
        a = ''
        def __init__(self,n,a,w,g,t):
            student3.__init__(self,n,a,w,g)
            speaker.__init__(self,n,t)
    
    
    # class sample(student3,speaker):
    #     a = ''
    #     def __init__(self,n,a,w,g,t):
    #         student3.__init__(self,n,a,w,g)
    #         speaker.__init__(self,n,t)
    
    
    test = sample('Tim',25,80,4,'Python')
    test.speak()#方法名相同,默认调用的是在括号中排前的父类的方法
    
    
    ##再重新敲一遍下面的代码
    class people:
        name = ''
        age = 0
        __weight = 0
        def __init__(self,n,a,w):
            self.name = n
            self.age = a
            self.__weight = w
        def speak(self):
            print('{}说:我今年{}'.format(self.name,self.age))
    
    class student(people):
        grade = ''
        def __init__(self,n,a,w,g):
            people.__init__(self,n,a,w)
            self.grade = g
        def speak(self):
            print('{}说,我{}岁了,在读{}年级'.format(self.name,self.age,self.grade))
    class speaker:
        name = ''
        toptic = ''
        def __init__(self,n,t):
            self.name = n
            self.toptic = t
        def speak(self):
            print('{}说,我是一个演说家,我演讲读主题为{}'.format(self.name,self.toptic))
    
    class sample(speaker,student):
        a = ''
        def __init__(self,n,a,w,g,t):
            student.__init__(self,n,a,w,g)
            speaker.__init__(self,n,t)
    
    test = sample('Tim',25,80,4,'Python')
    test.speak()
    
    '''
    方法重写
    '''
    class Parent:
        def myMethod(self):
            print('调用父类方法')
    
    class Child(Parent):
        def myMethod(self):
            print('调用子类方法')
    
    c = Child()
    c.myMethod()
    super(Child,c).myMethod()
    '''
    类属性与方法
    '''
    ##类的私有属性
    class JustCounter:
        __secretCount = 0
        publicCount = 0
        def count(self):
            self.__secretCount +=1
            self.publicCount +=1
            print(self.__secretCount)
    counter = JustCounter()
    counter.count()
    counter.count()
    print(counter.publicCount)
    # print(counter.__secretCount)  #报错,实例不能访问私有变量
    
    
    ##类的私有方法
    
    class Site:
        def __init__(self,name,url):
            self.name = name
            self.url = url
        def who(self):
            print('name:',self.name)
            print('url:',self.url)
        def __foo(self):
            print('这是私有方法')
        def foo(self):
            print('这是公有方法')
            self.__foo()
    x = Site('菜鸟教程','www.runoob.com')
    x.who()
    x.foo()
    # x.__foo()  #会报错的
    
    ##类的专有方法
    pass
    
    
    ##运算符重载
    
    class Vector:
        def __init__(self,a,b):
            self.a = a
            self.b = b
        def __str__(self):
            return 'Vector({},{})'.format(self.a,self.b)
        def __add__(self, other):
            return Vector(self.a+other.a,self.b+other.b)
    
    v1 = Vector(2,10)
    v2 = Vector(5,-2)
    print(v1 + v2)
  • 相关阅读:
    删除lv
    nohop以及后台运行的相关集合
    linux 上安装pstree
    python中的异常
    ansible批量验证密码
    Linux显示不了中文
    zabbix修改和查看登录密码
    chkconfig --add失败的处理方法
    vivado2016.2下系统自带DDR3 ip例程仿真运行
    分享我们必须知道的高速GTX技术
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/9988405.html
Copyright © 2011-2022 走看看