Python基础-OOP

2019-07-16  本文已影响0人  Yuanshuo
The core values of Chinese socialism

OOP

# 定义学生类,和几个学生

class Student():
    name = "Yuanshuo"
    age = 22
    
    def sayHi(self):
        print("Life is fantastic!")
        
        return None
    
# 实例化
Yuanshuo = Student()
print(Yuanshuo)
<__main__.Student object at 0x10fa46198>

self

# self 举例

Yuanshuo = Student()

# Yuanshuo调用sayHi()没有输入参数
# 因为默认实例作为第一个参数传入
Yuanshuo.sayHi()
Life is fantastic!

类的变量作用域的问题

class Student():
    # name, age 是类的变量
    # 注意类的变量的定义位置和方法
    # 不需要前缀
    name = "Yuanshuo"
    age = 19
    
    def sayHi(self):
        print("My name is {}, I am {} years old.".format(self.name, self.age))
        return None

# 示例说明,实例变量可以借用类的变量
Yuanshuo = Student()
Yuanshuo.sayHi()
    
My name is Yuanshuo, I am 19 years old.
class Student():
    # name, age 是类的变量
    # 注意类的变量的定义位置和方法
    # 不需要前缀
    name = "Yuanshuo"
    age = 19
    
    def sayHi(self, n, a):
        self.name = n
        self.age = a
        print("My name is {}, I am {} years old.".format(self.name, self.age))
        return None

# 示例说明,
Yuanshuo = Student()
Yuanshuo.sayHi("Dapeng", 17)

# 如果访问实例的属性没有定义,则自动访问类的属性
# 如果类也没有定义,报错
print(Yuanshuo.wife())
My name is Dapeng, I am 17 years old.



---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-5-4649390c3408> in <module>
     18 # 如果访问实例的属性没有定义,则自动访问类的属性
     19 # 如果类也没有定义,报错
---> 20 print(Yuanshuo.wife())


AttributeError: 'Student' object has no attribute 'wife'

访问类的属性

class Student():
    # name, age 是类的变量
    # 注意类的变量的定义位置和方法
    # 不需要前缀
    name = "Yuanshuo"
    age = 19
    
    def sayHi(self):
        print("My name is {}, I am {} years old.".format(self.name, self.age))
        return None

    # SOS是类的方法
    # 如何访问类的变量
    # 但是调用需要用到特殊方法
    def SOS():
        # 类方法中不允许访问实例的任何内容
        # 如果访问类的内容,注意两种用法
        print("My name is {}, I am {} years old.".format(Student.name, __class__.age))
        return None
        
        
# 体验类的方法
Yuanshuo = Student()
Yuanshuo.sayHi()

# 调用类方法的例子
Student.SOS()
My name is Yuanshuo, I am 19 years old.
My name is Yuanshuo, I am 19 years old.

构造函数

class Student():
    name = "NoName"
    age = 0
    
    # 构造函数名称固定,写法相对固定
    def __init__(self):
        print("我是构造函数")
        
Yuanshuo = Student()
print("----------")
print(Yuanshuo.name)
print(Yuanshuo.age)

我是构造函数
----------
NoName
0

面向对象的三大特征

继承

# 所有类必须有父类
# 默认是object
class Person1():
    pass

class Person2(object):
    pass

class Person():
    name = "NoName"
    age = 0
    
# 父类写在类定义的时候的括号里
class Teacher(Person):
    pass

t = Teacher()
print(t.name)
NoName
class Bird():
    fly = "We can fly."
    def flying(self):
        print("Flying!")
        
class BirdMan(Person, Bird):
    pass

bm = BirdMan()
bm.flying()
print(bm.name)
Flying!
NoName

issubclass检测是否是子类

- 可以用来检测两个类的父子关系
# 利用上面定义的Bird, Person, BridMan, Teacher检测父子关系

print(issubclass(BirdMan,Bird))
print(issubclass(BirdMan,Person))
print(issubclass(BirdMan,Teacher))
print(issubclass(Teacher,Person))
True
True
False
True

构造函数的继承

class Person:
    def __init__(self, name, age):
        print("Person=({},{})".format(name, age))
        
class Teacher(Person):
    pass

t = Teacher("Yuanshuo", 19)
t = Teacher()

Person=(Yuanshuo,19)



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-33-42c7a2d76a67> in <module>
      7 
      8 t = Teacher("Yuanshuo", 19)
----> 9 t = Teacher()


TypeError: __init__() missing 2 required positional arguments: 'name' and 'age'
上一篇 下一篇

猜你喜欢

热点阅读