Python learning Object-oriented composition

Source: Internet
Author: User

Object-oriented namespaces
Methods in a class
Static properties in a class
Properties of the Object
Combined application of Class


Object-oriented namespaces:
Class A:
country= ' China '
A=a ()
A.name= ' Alex '
The print (a.name) result is that Alex wants to add properties that you can add
B=a ()
Print (B.name) result is a property without name

The object has an address book to invoke the method of the class, showing different memory, but the actual call memory is the same
Objects and classes invoke dynamic properties with different addresses, actually
Static properties are exactly the same address
a.country= ' China '
Print (A.country) China
Print (A.country) India
Print (B.country) India
For static properties about the class:
If the class. property is called by a property in the class
Object. Property first from its own memory space to find the name, found the use of their own, did not find the use of classes, the class did not error
About the dynamic properties of a class:
This method has the ability to exist in the class, and there is no memory in the object
When an object invokes a method in a class, it relies on an address book to find the corresponding method in the class.
About the properties of an object
object's properties exist in the object's namespace
Can only be called and modified by an object
cannot be called by class


Object-oriented combination:
Can be decoupled, you can use other classes of objects to do properties

Class Ban:
def __init__ (Self,name,ke,jiang):
Self.name=name
Self.ke=ke.name
Self.jiang=jiang.name
Class Student:
Quanxian= ' Xuesheng '
def __init__ (self,name,ban,ke,grade=100):
Self.name=name
Self.ban=ban.name
Self.ke=ke.name
Self.jiang = Ban.jiang
Self.grade=grade
Class Jiang:
Quanxian= ' Jiaoshi '
def __init__ (Self,name,sex,ke,bang):
Self.name = Name
Self.sex = Sex
self.ke = Ke.name
Self.bang=bang

Python learning Object-oriented composition

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.