zoukankan      html  css  js  c++  java
  • Pyhton入门 笔记 第三天 面向对象 类

    类的定义

    1)class Student():           #定义类  class

        name=''

        age=0

        def print_file(self):      #定义类中的函数称为方法,没有参数时要用self

            print('name:' + self.name)      #使用变量时也要加self

            print('age:' +str(self.age))

    student = Student()       #类的实例化,赋值给变量

    student.print_file()        #调用类的方法

    2)如果要对实例化类传入参数的话,要通过__init__构造函数来实现

    class Student():           #定义类  class

        name=''

        age=0     # name,age为类变量

        def __init__(self,name,age):    #构造函数中当要传递参数时要通过self.来传递。传递后的self.name为实例变量。其中构造函数中的self参数不是关键字,可以为this来代替,当然下面的实例变量也要变为thins.name

            self.name=name

            self.age=age

            print('student')

        def print_file(self):      #定义类中的函数称为方法,没有参数时要用self

            print('name:' + self.name)      #使用变量时也要加self

            print('age:' +str(self.age))

    student1 = Student('小明',18)       #类的实例化,赋值给变量,实例化时会自动调用__init__构造函数

    print(student1.name)

    __dict__字典变量

    print(student1.__dict__)可以打印出student1对象下所有的参数字典

     

    3)在构造函数或实例方法中访问类变量

    class Tngh():

        name="dddd"

        age= 0

        tt="红色"

        def __init__(self,name,age)

            self.name=name

            self.age=age

            print(Tngh.tt)    #通过类访问

            print(self.__class__.tt)   #通过sefl下class访问

    4)类方法

    class Tngh():

        name="小晨"

        age= 0

        sum1=0

        @classmethod   # 对类方法的定义一定通过·装饰器@classmethod   同时类方法的参数为(cls)  cls也可改为别的

        def puls_sum(cls)

        print(cls.sum1)     #类方法中对类变量的操作更简单。

    Tngh.puls_sum()    #类方法的调用

        

     5)静态方法

    class Tngh():

        sum=0

        @staticmethod    #静态方法也要通过装饰器@staticmethod来定义

        def add(x,y):

            print(Tngh.sum)  #静态方法也可以直接对类变量进行操作

            pass

    student=Tngh()

    student.add(1,2)   #对象对静态方法的调用

    Tngh.add(1,2)    #类对静态方法的调用

    6)可见性

        对类变量和类方法的私有化,在变量名和方法名前加__可使变量和方法私有化,如__name,__puls()。当私有化后,在类外就不可以读取和重写。

    7)类的可继承

    class Human():

    class Tngh(Human):  #表示类Tngh继承类Human

    8)类元素的继承

    class Human():

        sum=0

        def __init__(self,name,age):

            self.name=name

            self.age=age

        def get_name(self):

            print(self.name)

    class Tngh(Human):  #类Tngh继承父类Human

        def __init__(self,shool,name,age):  #子类Tngh的构造函数的参数为shool。但因为继承了父类Human,所以也要加入父类的参数name和age。所以子类的构造函数的参数为shool,name,age

            self.shool=shool

            Human.__init__(self,name,age) #第一种调用父类的构造函数(不常用)。把值传过去。

            super(Tngh,self).__init__(name,age)  #第二种调用父类的构造函数(常用) super不至可以调用父类的构造函数,也可以在实例方法中调用父类的实例方法。

        def do_homework(self):

           super(Tngh,self).get_name()   #子类实例方法调用父类的get_name()实例方法

            print('english homework')

    student=Tngh('人民小学',‘小明’,18)

  • 相关阅读:
    HearthBuddy投降插件2019-11-01的使用
    正则表达式在线分析 regex online analyzer
    Tips to write better Conditionals in JavaScript
    The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
    Cannot capture jmeter traffic in fiddler
    JMETER + POST + anti-forgery token
    input type color
    HearthBuddy修改系统时间
    What are all the possible values for HTTP “Content-Type” header?
    UDK性能优化
  • 原文地址:https://www.cnblogs.com/tngh/p/9313679.html
Copyright © 2011-2022 走看看