The life of the short Python class of 123

Source: Internet
Author: User

In Python, classes are also defined at the beginning of class. We define an animal class that has a name and age, a Java variable with an instance variable and a local variable, a variable in the method is a local variable, and a variable in the class is an instance variable. So how are the classes and their properties in Python defined and used?

Class Animal ():p

Properties of the class

We know that classes have attributes, and in Java it is common to define attributes in a class, whereas in Python you can add attributes directly to an instance.

>>> a = Animal ()
>>> a.name = ' dog '
>>> Print A.name
Dog
>>> A
<__main__. Animal instance at 0x0000000002c5e708>

But this property only works on this instance variable, and when we create a new instance again, there is no such attribute.

>>> B = Animal () >>> print b.nametraceback (most recent call last):  File "<pyshell#16>", line 1 , in <module>    

So how do you create a class property? You can define a variable directly inside.

You can change this property value after instantiating the object.

Private properties of the class

in Java We know that you can control access restrictions through private, modify variables, or modify methods so that they can only be used inside a class, so how do you do it in Python? Define a private variable by adding ' __ ' before the variable.

>>> class Student ():d EF __init__ (self,name): Self.__name = name>>> s = Student (' Bob ') >>> S.nametraceback (most recent):  File "<pyshell#69>", line 1, in <module>    S.nameattributeerror:student instance have no attribute ' name ' >>> s.__nametraceback (most recent call last): 
   
    file "<pyshell#70>", line 1, in <module>    s.__name
   

Since private variables cannot be accessed directly, how do we use them? At the time, the Setter,getter method was used to add setter and getter methods to private variables within the class so we could access and modify them. There's no description here.

Construction Method

There is a default constructor in Java, and you can create a constructor based on what we need, and create objects that meet our needs as needed when instantiating an object. There are also construction methods in Python. Using __init__, pass in the self and the required variables.

>>> class Animal ():d EF __init__ (self,name,age): Self.name = Nameself.age = age>>> A = Animal () Traceback (most recent):  File "<pyshell#34>", line 1, in <module>    a = Animal () TypeError: __init__ () ta Kes exactly 3 arguments (1 given) >>> a = Animal (' dog ', 23)

It is worth noting that when we write the constructor method of the class, we must instantiate the object by using the constructor method, or we will report an error.

Class method

in a class, there are not only attributes but also methods, and class methods are defined directly in the class.

>>> class Animal ():d EF __init__ (self,name,age): Self.name = Nameself.age = Age        

Object-oriented three major features, encapsulation, inheritance, polymorphism, encapsulation Needless to say, in many places have manifested. Let's look at the inheritance and polymorphism in Python.

>>> class Animal (): 
    def __init__ (Self,name, Age):
        self.name = name
        self.age = Age
        def printname (self,sex):
        print (self.name,sex)

        
>>> a = Animal (' dog ')
>>> a.printname (' AA ')
(' Dog ', ' AA ')
>>> class Dog (Animal):
    pass

>>> B = Dog ()

Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <m Odule>
    b = Dog ()
TypeError: __init__ () takes exactly 3 arguments (1 given)
\
>>& Gt b = Dog (' 2 ha ', +)
Syntaxerror:invalid syntax
>>> b = Dog (' Hashiqi ', ')
>>> b
<__main_ _. Dog instance at 0x0000000002c64848>
>>>

We define a dog class that inherits from animal, and when we instantiate it we find that its constructor is also inherited from the parent class, which needs to pass in the name and age, but what if we want it to have its own construction method? Override the __init__ method.

This embodies the object-oriented inheritance and polymorphism.

The life of the short Python class of 123

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.