python 面向对象-实例化(对象)

1、构造函数 __init__()

#coding=utf-8
class Student():
    name = ""
    age  = 0
    
    def __init__(self): #构造函数
        pass

    def do_homework(self):
        print(self.name +"do homework")


student1 = Student()
student2 = Student()
student3 = Student()

实例化的时候,python 会自动的调用构造函数,不需要去显示调用,如果非要调,也可以

#coding=utf-8
class Student():
    name = ""
    age  = 0
    
    def __init__(self): #构造函数
        pass

    def do_homework(self):
        print(self.name +"do homework")


student1 = Student()
a = student1.__init__()
print(type(a))
# Project/python_ToolCodes/test6.py"
# <type 'NoneType'>

这个说明 显示调用构造函数没有什么特别的,跟普通函数没区别,且无返回值。如果非要返回

#coding=utf-8
class Student():
    name = ""
    age  = 0
    
    def __init__(self): #构造函数
        return "111"

    def do_homework(self):
        print(self.name +"do homework")


student1 = Student()
a = student1.__init__()
print(type(a))
# Project/python_ToolCodes/test6.py"
# Traceback (most recent call last):
#   File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 13, in <module>
#     student1 = Student()
# TypeError: __init__() should return None

这就跟Java 一样了,构造函数无返回值,如果非要强制返回,那就报错了,因为python 和Java一样 不允许在构造函数中返回

这样也算是啥也不返回,和不写return 是一样的

#coding=utf-8
class Student():
    name = ""
    age  = 0
    
    def __init__(self): #构造函数
        return 

    def do_homework(self):
        print(self.name +"do homework")


student1 = Student()
a = student1.__init__()
print(type(a))
# Project/python_ToolCodes/test6.py"
# <type 'NoneType'>

 2、带参构造函数 __init__(self,name,age):用来初始化对象的属性

#coding=utf-8
class Student():
    name = ""
    age  = 0
    
    def __init__(self,name,age): #构造函数
        self.name = name
        self.age  = age

    def do_homework(self):
        print(self.name +"do homework")


student1 = Student("ansonw",26)
print("name:"+student1.name)
print("age:"+str(student1.age))
# Project/python_ToolCodes/test6.py"
# name:ansonw
# age:26

3、类变量和实例变量(关于类变量的使用方法,似乎与Java 不同

Student.name 和 student1.name
#coding=utf-8
class Student():
    name = "kiki"
    age  = 0
    
    def __init__(self,name,age): #构造函数
        self.name = name
        self.age  = age

    def do_homework(self):
        print(self.name +"do homework")


student1 = Student("ansonw",26)
print("实例变量=>name:"+student1.name)
print("类变量=>name:"+Student.name)
# 实例变量=>name:ansonw
# 类变量=>name:kiki

但是,类是不需要名字和年龄的,所以这里类有名字为"kiki" 是不合理的

4、__dict__python内置变量打印,对象的内容和类的内容

student1.__dict__
对象:
#
coding=utf-8 class Student(): name = "" age = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self): print(self.name +"do homework") student1 = Student("ansonw",26) print(student1.__dict__) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # {'age': 26, 'name': 'ansonw'}
类:
#
coding=utf-8 class Student(): name = "" age = 0 def __init__(self,name,age): #构造函数 self.name = name self.age = age def do_homework(self): print(self.name +"do homework") student1 = Student("ansonw",26) print(Student.__dict__) #[Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" #{'__module__': '__main__', 'do_homework': <function do_homework at 0x10ab0d758>, 'name': '', 'age': 0, '__doc__': None, '__init__': <function __init__ at 0x10ab0d140>}

5、python 中的self 与this

  • Java 中的用this 来指代当前对象,python中可以用this,但是建议使用self
  • 类方法定义时,必须要带上self
  • 与Java一样,如果没有显示重写构造方法,python会有一个默认的无参构造方法
  • def __init__(self,name,age): #构造函数
            self.name = name
            self.age  = age
    
        def do_homework(self):
            print(self.name +"do homework")

     

  • 类方法倍调用时,不需要带self,因为python自动给我们加上了这个入参
  • student1.do_homework()
posted @ 2020-08-05 13:30  XiaoLee-C  阅读(2393)  评论(0编辑  收藏  举报