Single-Inheritance for Python classes and classes

Source: Internet
Author: User

Python is an object-oriented language, so OOP programming is a must.

Below, I will summarize my study experience and learn knowledge.

1. Declaration of a class

Class Student (object):

Pass

Class is the keyword that declares the class, and Student is the class name. In parentheses, object is used for inheritance, and if no other class is inherited, the object class is inherited.

The pass area is the method and property of the class

Private property or method: A private property or method that begins with a double underscore (_ _xxx), which can only be accessed within the class itself, or outside of the class.

Protected properties or methods: Protected properties and methods are accessible only through classes and subclasses, in a format that begins with a single underscore (_xxxx).

2. Below, let's create a complete class

Class Student (object):
Name= "" # This is the property of the class
Age= "" # This is also a tired property
__score= "" # This is a private property of the class and cannot be accessed directly outside of the class, only through the internal methods of the class.

def __init__ (Self,name,age,score): # This is how the class is constructed
Self.name=name
Self.age=age
Self.__score=score # This is a private attribute, the subclass is not accessible and can only be used by itself

def print_info (self): #这个是类的方法
Print (Self.name)
Print (Self.age)
Print (Self.__score) # This is the internal method of using the class to access the private property
def __age_change (self):              # This method is a private method of the class, which cannot be accessed externally, and is invoked in the inside of the class to create a public method to invoke the private method execution
   Print ("My Love")

def use_age_change (self): # This method is the public method used to invoke the private method
Self.__age_change ()
Ins_zrs=student ("Zhangrongshun", 18,100)   # instance Creation
Ins_zrs.print_info () # Invoke a method with an instance

This is the complete step of class creation to instantiation.

The function of a class's constructor is to initialize the properties of the object.

The following is the tired inheritance, Python's class support single inheritance and multiple inheritance, but PHP only support single inheritance, students who have learned PHP should know it.

#这个类将会继承上面的类-----Single Inheritance
Class U_student (Student):
Grade= "" # Adds a new attribute to the subclass
def __init__ (Self,name,score,age,grade):
student.__init__ (Self,name,score,age) # This is Student.
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 properties cannot be inherited when inheriting the properties of a class

Single-Inheritance for Python classes and classes

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.