Python Class and Class Single inheritance, python inheritance

Source: Internet
Author: User

Python Class and Class Single inheritance, python inheritance

Python is an object-oriented language, so oop programming is required.

Next, I will summarize my learning experiences and knowledge.

1. Declare a class

 

Class Student (object ):

Pass

 

 

Class is the key word of the declared class, and Student is the class name. Objects in brackets are used only when they are inherited. If they do not inherit other classes, they inherit the object class.

The pass area is the class method and attribute.

 

Private property or method: a private property or method starts with a double underscore (_ XXX). This property can only be accessed within the class itself, but cannot be accessed outside the subclass or class.

Protected attributes or methods: the protected attributes and methods are accessible only through classes and subclasses. The format starts with a single underscore (_ XXXX ).

 

2. Next, we will create a complete Class

 

Class Student (object ):
Name = "" # This is a class attribute
Age = "" # this is also a tired attribute
_ Score = "" # This is the private attribute of the class. It cannot be accessed directly outside the class, but can only be accessed through the internal method of the class.

Def _ init _ (self, name, age, score): # This is the class constructor.
Self. name = name
Self. age = age
Self. _ score = score # This is a private attribute. sub-classes cannot be accessed and can only be used by themselves.

Def print_info (self): # This is a class method.
Print (self. name)
Print (self. age)
Print (self. _ score) # The internal method of the class used to access private attributes
Def _ age_change (self): # This method is a private method of the class, and cannot be accessed from outside, the call method is to create a public method within the class to call the private Method for execution.
Print ("my love ")

Def use_age_change (self): # This method is a public method used to call a private method.
Self. _ age_change ()
Ins_zrs = Student ("zhangrongshun", 18,100) # create an instance
Ins_zrs.print_info () # use an instance to call the Method

 

This is the complete step from class creation to instantiation.

 

Class constructor: used to initialize the properties of an object

 

Below is the tired inheritance. Classes in python support single inheritance and multi-inheritance, but php only supports single inheritance. Anyone who has learned php should know it.

# This class will inherit the above class ----- single inheritance
Class U_student (Student ):
Grade = "" # Add an attribute to the subclass
Def _ init _ (self, name, score, age, grade ):
Student. _ init _ (self, name, score, age) # Student's
Self. grade = grade

Def print_information (self ):
Print (self. name)
Print (self. grade)
Print (self. age)

Def _ p_name (self ):
Print ("li hai ")

D = U_student ("zhangrongshun", 88,22, 6)
D. print_information ()

# Note that private attributes cannot be inherited when inheriting class attributes

 

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.