Python基础常识三

类与对象

1.定义

属性+方法。  如:

>>> class Fruit:
	name = 'apple'
	color = 'red'
	def Print_f(self):
		print(self.name + '\'s color is ' + self.color)

		
>>> f = Fruit()
>>> f.Print_f()
apple's color is red

 类中方法的第一个参数一定是self

类的构造方法__init__():

__init__()在对象实例化时会自动调用,即对象的初始化。

>>> class Fruit:
	def __init__(self,name='apple',color='red'):
		self.name = name
		self.color = color

	def Print_f(self):
		print(self.name + '\'s color is ' + self.color)

>>> f1 = Fruit('orange','orange')
>>> f1.Print_f()
orange's color is orange

 

2.类的三大特征

2.1 继承

将类分为父类(基类)和子类。子类继承父类的全部的非私有功能。

需要注意:在继承中,基类的构造方法不会被自动调用,需要在子类的构造方法中专门调用。

super()大致意思是继承的基类,这样当继承的基类发生变化时,不需要改变类中的变量名。

class Fruit:
    def __init__(self,name,color):
        self.name = name
        self.color = color
        
    def Print_c(self):
        print(self.name + '\'s color is ' + self.color)
	
class Apple(Fruit):
    pass

class Orange(Fruit):
    def __init__(self,name,color,size):
        super().__init__(name,color)
        self.size = size

    def Print_o(self):
        super().Print_c()
        print(self.name + '\'s size is ' + self.size)


>>> a1 = Apple('apple','red')
>>> a1.Print_c()
apple's color is red
>>> o1 = Orange('orange','orange','small')
>>> o1.Print_o()
orange's color is orange
orange's size is small

2.2 多态

子类继承基类的方法时,对于一些方法可以进行重新定义,使得同一方法在两个类的表现不同。

class Fruit:
    def __init__(self,name,color):
        self.name = name
        self.color = color
        
    def Print_c(self):
        print(self.name + '\'s color is ' + self.color)
	

class Orange(Fruit):
    def __init__(self,name,color,size):
        super().__init__(name,color)
        self.size = size

    def Print_c(self):   #和基类中方法重名,是对基类中该方法重新定义
        print(self.name + '\'s color is ' + self.color + '\n' + self.name + '\'s size is ' + self.size)


>>> f1 = Fruit('apple','red')
>>> f1.Print_c()
apple's color is red
>>> o1 = Orange('orange','orange','small')
>>> o1.Print_c()
orange's color is orange
orange's size is small

2.3 封装

封装将类中一些数据隐藏起来,使得不能通过外部访问类中的数据,但是可以在类内部定义方法对数据进行访问。

要让内部属性不被外部访问,可以在属性名前加__,使其变成私有变量,只有内部可以进行访问。函数也可通过__私有化。

class Apple:
    def __init__(self,color,size):
        self.__color = color          #变量私有化
        self.__size = size

    def print_f(self):
        print('apple\'s color:'+ self.__color)
        print('apple\'s size:'+ self.__size)

    def set_size(self,size):    #需要在类中定义改变私有属性的函数,否则无法在外部改变属性
        self.__size = size

    def get_size(self):         #需要在类中定义读取私有属性的函数,否则无法在外部读取属性
        print('apple\'s size:'+ self.__size)


>>> a1 = Apple('red','small')
>>> a1.print_f()
apple's color:red
apple's size:small
>>> a1.set_size('large')
>>> a1.get_size()
apple's size:large
>>> a1.size          #无法在外部读取类中的私有属性
Traceback (most recent call last):
  File "", line 1, in 
    a1.size
AttributeError: 'Apple' object has no attribute 'size'

3.获取对象信息

1.type()

>>> type('haha')

2.isinstance()还可以明确继承关系

>>> o1 = Orange('orange','orange','small')
>>> isinstance(o1,Orange)
True
>>> isinstance(o1,Fruit)
True

3.dir()获取对象的所有属性和方法

>>> dir(Orange)
['Print_c', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

4.专有方法

......


基本知识学的差不多了,就要开始实操了。

缓慢爬行中...

你可能感兴趣的:(菜鸟的Python学习之路,python)