Python language Learning (iv) 1.1

Source: Internet
Author: User

Classic class, New class

Classic classes are those that do not inherit, and the new class is inherited. Now it's basically a new class.

#encoding =utf-8

#经典类
Class Dog:
Pass

#继承了object, for the new class
Class Dognew (object):
Pass

#可输出此类是什么类型的类
Print type (DOG)
Print type (dognew)


Class Heibei (Dog):
Pass

Class Bomei (dognew):
Pass

Print type (Heibei)
Print type (Bomei)

Results:

<type ' Classobj ' >
<type ' type ' >
<type ' Classobj ' >
<type ' type ' >

Method: Is the encapsulation of class behavior

Instance method: The Self keyword, which accesses instance properties through self, invokes an instance method

Special methods: Methods that the compiler automatically adds, the Self keyword (such as init)

static method: @staticmethod (Adorner)

Class method: @classmethod (Adorner)

The visibility of the property:

Common Properties: Properties that are used in classes and outside the class

Built-in properties: Attributes that are automatically added by the compiler, such as __dict__

Private properties: Properties that can only be used within the class, __, will be gaiminghuanxing, _, will not be renamed, symbolic meaning greater than the actual meaning.

#encoding =utf-8
class Person (object):
Pass

Class Chinese (person):
#定义类属性
Nation = ' China '

def __init__ (self, name, age, Sex):
#定义实例属性
Self.name = Name
Self._age = Age
Self.__sex = Sex

Zhangsan = Chinese (' Zhangsan ', ' Male ')
Print Zhangsan.name
Print Zhangsan._age
Print Zhangsan._chinese__sex
#print Zhangsan.__sex
Print dir (Chinese)

Results:

Zhangsan
18
Male
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __ module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' __weakref__ ', ' Nation ']

Python language Learning (iv) 1.1

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.