Python Basic Learning Log day6-Object-oriented

Source: Internet
Author: User

What is object-oriented programming

OOP programming is the use of "classes" and objects to create a variety of models to achieve a real-world description.

OOP has maintainability and scalability

Two: Object-oriented has those characteristics

1) Class: A class is an abstraction of an object that has the same properties. Class owns the properties of the class and methods of the class.

2) Object: An object is an instantiated instance of a class. This process is instantiated.

3) Encapsulation Encapsulation: The assignment of data in a class, the internal invocation is transparent to the outside.

4) Inheritance inheritance: A class can be sent to generate subclasses, in which the defined properties and methods of the parent class are automatically inherited by the quilt class

5) Polymorphism polymorphism: Refers to a base class derived from a different subclass, polymorphism allows the object of the subclass as the parent class object to use,

A reference to a parent type points to an object of its subtype, and the method that is called is a method of that subtype.

The code that references and calls the method here is determined before it is compiled, and the object that the reference points to can be dynamically bound during the run.

Three: example of a class definition

#-*-coding:utf-8-*-__author__='Shisanjun'classPerson (object):#class, which defines a class, which is the syntax for defining a class, the person is the class name, and (object) is the style of the new class    def __init__(self,name):#constructors, constructor methods, initial methods, some of the properties to initialize when generating a role are filled in hereSelf.name=name#A member of a class or a property of a class    defSayhi (self):#methods of the class        Print(" in the person", self.name) d=person ("Shi")#an instantiated object, called instance D, is an instantiated object .D.sayhi ()
Instantiation, is the person class as a template, in memory to open a space and assign to a variable name

Note: Self is the instance itself, and the instance itself is automatically passed in by the argument itself when instantiated.

    1. Open up a space in memory pointing to the variable name D.
    2. __init__ (self,name) in person is equivalent to __init__ (d,name), the name value associated with the newly opened D, because you can directly rd.name this to invoke

3.self process

Summarize:

    1. The above d = Dog (' Lichang') action, called "instantiation" of a class, is a virtual abstract class that, through this action, becomes a concrete object, which is called an instance
    2. The class just defined represents the first basic object-oriented feature of encapsulation, which is to encapsulate content into a specific object using a construction method, and then indirectly get the encapsulated content through the object directly or self

Four: Three major characteristics of the class

1) Encapsulation: that is, to encapsulate objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding

Prevent data from being arbitrarily modified
So that external programs do not need to focus on the construction of the object, only through the interface provided by this object directly to access the port

2) Inheritance: The new class that is created is called a subclass or derived class, and the inherited class is called the base class, the parent class.

The process of inheritance is from the general to the special process. To implement inheritance, you can use Inheritance (inheritance) and combination (composition) to implement

There are 2 main implementations of the concept of inheritance: implementation inheritance, interface inheritance.

Implementing inheritance refers to the ability to use the properties and methods of a base class without additional coding

Interface inheritance refers to the ability to use only the names of properties and methods, but the subclass must provide the implementation (subclass refactoring Father method)

3) Polymorphic: Polymorphism is an interface that calls subclasses through the parent class

4) Inherit the sample code

  

#-*-coding:utf-8-*-__author__='Shisanjun'classSchoolmember (object): Menber=0#Public Properties    def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=sex Self.enrool ()#each sub-class is run    defEnrool (self):Print('%s has enroll'%self.name) Schoolmember.menber+=1#cumulative increase, cannot write this.menber+=menber this write will only add 1 when the subclass is called, not to the parent class    defTell (self):#Print all user information, implemented as follows        Print("-----Info%s-----"%self.name) forV,kinchSelf.__dict__. Items ():#returns a dictionary of all member properties of a subclass            Print("%s:\t%s"%(v,k))def __del__(self):Print("%s has del"%self.name) Schoolmember.menber-=1classTeacher (schoolmember):def __init__(self,name,age,sex,salary,course):#schoolmember.__init__ (self,name,age,sex) #经典类写法 ==schoolmember.__init__ (self,name,age,sex)Super (Teacher,self).__init__(Name,age,sex)#The new class-style class notationself.salary=Salary Self.course=CoursedefTeach (self):Print("The teacher Course", Self.course)classStudent (schoolmember):def __init__(self,name,age,sex,tuition,course): Schoolmember.__init__(self,name,age,sex) self.tuition=Tuition Self.course=Coursedefpay_tuition (self):Print("The student pay", self.tuition) T=teacher ("Shi", 23,"F", 15000,"python") s=student ("San", 26,'M', 3000,"python") T.tell () S.tell ()"""C:\Python36\python.exe F:/python Development/day7/class inheritance 2.pyshi has Enrollsan have enroll-----info shi-----name:shiage:23 Sex:fsalary:15000course:python-----Info San-----Name:sanage:26sex:mtuition:3000course:pyth Onshi has Delsan have delprocess finished with exit code 0"""

Summarize:

Python Basic Learning Log day6-object-oriented

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.