Python (four, object-oriented programming)

Source: Internet
Author: User

Outline: The basic idea of object-oriented

Class-to-object relationships

Structure and members of a class

Magic functions in Python

  Complete design and instantiation of classes by functional requirements

All objects

BIFs:

Type () return data type  

ID () View object ID

Dir () View variables and functions under Objects

Issubclass (), Isinstance (), Super (), class, instance, tuning parent class

Hasattr (), GetAttr (), SetAttr (), Delattr (), class properties

Globals (), locals (), Global and local namespaces

Import (), Reload (), module imports and overloads

To create a class:

class keyword

Specify inheritance

Defining members of a class

Data members

class variables

Instance Object

Method members

Class method

Instance Object

Instantiating classes

class creation and initialization

Example:

Complete the design of a student class

Can see the total number of students, all students name, Graduation score standard, the number of students who have graduated

To achieve the test method, if the score is greater than 60 points for passing the exam, the score is credited to the student's total

If the student's cumulative score reaches 1000 minutes, the student will graduate

To achieve a method of sorting, to return to the student whether graduated, or how many points to graduate

The ability to query the number of all graduated students

Implement the ability to query the number of all students

Implements the ability to query all student names

Class Student ():
Student_total=0
Student_list=[]
Student_graduated=0
pass_score=1000

def __init__ (Self,name,age,gender):
Self.name=name
Self.age=age
Self.genger=gender
Self.score=0

Student.student_total+=1
Student.student_list.append (name)


def exam (self,score):
If score<60:
return ' failed '
Else:
Self.score+=score
Str1= ' Good, your score is now ' +str (self.score)

If Self.score>=student.pass_score:
Student.student_gra Duated+=1

Return str1

#david =student (' David ', ', ' Male ')
#type (David)

def check (self,): #只要是对象的方法, start with self
If Self.score<student.pass_score:
Return ' You had ', Student.pass_score-self.score, ' to achieve '
Else
Return ' You have graduated '

@classmethod
Def get_student_graduated (CLS,): #cls?
Return student.student_graduated

@classmethod
Def get_student_total (CLS,): #cls?
Return student.get_student_total

@classmethod
Def get_student_list (CLS,): #cls?
Return student.get_student_list


@staticmethod
Def static1 ():
Return ' This is a static method form Student,it are can be called by both the class and the instance '
Xiaoming=student (' xiaoming ', ' Male ')
Xiaoming=student (' ximing ', ' Male ')


Student.get_student_list ()

#有问题, did not achieve the desired effect

<bound method Student.get_student_list of <class ' __main__. Student ' >>

static method (Normal method):

Use the @staticmethod adorner when defining

Static methods have no parameter restrictions and do not require instance arguments self and class parameters CLS CLS is shorthand for class

The static method can be accessed through the class name, or the instance accesses

Private control double underline "__"

  

Python (four, object-oriented programming)

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.