Python object-oriented-encapsulation, inheritance, polymorphism

Source: Internet
Author: User

1. Create a class

class ClassName:    ‘‘‘    定义类    ‘‘‘    def __init__(self,name,age):#self代表类的实例,而不是类本身        ‘‘‘        类初始化函数        :param name:姓名        :param age: 年龄        ‘‘‘        self.name=name        self.age=age    def Class_method(self):        ‘‘‘        类中的方法        :return:        ‘‘‘        pass

2. Class instantiation, creating objects of class

c_name1=ClassName(‘zhangsan‘,22)c_name2=ClassName(‘lisi‘,25)

3. Inheritance of Classes

class Child(ClassName):    passclass c(a,b):#python的继承可以继承多个类      pass

4. Private properties and private methods for classes

__private_arrts #在前面加两个斜杠__private_method()

5. Polymorphism refers to functions with different functions can use the same function name

class people(object):    def __init__(self,name,age):        self.name=name        self.age=age    def show(self):        print("姓名:%s,年龄:%s"%(self.name,self.age))class man(people):    def show(self):        print("man的姓名:%s,年龄:%s"%(self.name,self.age))class woman(people):    def show(self):        print("woman的姓名:%s,年龄:%s" % (self.name, self.age))class show_thing():    def test_show(people):#统一调用,传入类的实例        people.show()#调用类的实例的方法m=man(‘张三‘,‘20‘)w=woman(‘李四‘,‘23‘)show_thing.test_show(m)show_thing.test_show(w)>>:man的姓名:张三,年龄:20woman的姓名:李四,年龄:23

6. Properties of built-in classes

print(m.__dict__)#类的属性print(m.__doc__)#类的注释文档print(m.__module__)# __main__#print(m.__dir__())#打印类的所有属性

Python object-oriented-encapsulation, inheritance, polymorphism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.