"python36--Object"

Source: Internet
Author: User

1. Object = attribute + method

2. Instantiating objects

#class name first letter capitalizationclassTurle:#Propertiescolor ='Green'Weight= 10Legs= 4Shell=True Mouth='Big Mouth'    #Method    defClimb (Selt):Print('I am trying very hard to climb forward ... ')    defRun (self):Print('I'm running fast ... ')    defBite (self):Print('bite you to death!! ')    defEat (self):Print('some eat, really satisfied. ')    defSleep (self):Print('sleepy, Sleep, goodnight. Zzzz')>>> TT = Turle ()#instantiating an object>>>tt.climb () I'm trying very hard to climb forward ... >>>Tt.run () I'm running fast ... >>>tt.bite () bite you to death!! >>>tt.eat () Some eat, really satisfied. 

3. oo (object-oriented) features:

--Encapsulation (attributes are encapsulated by the object)

>>> List1 = [2,1,7,5,3]>>> list1.sort ()  # Positive order >>>  list1[1, 2, 3, 5, 7]>>> list1.append (9)>>> list1[1, 2, 3, 5, 7, 9] Columns The methods and variables inside the table object are not clear, but only provide a name after encapsulation, we only need the name of the method, call the name can be

--Inheritance

# class MyList methods and properties that inherit list class Mylist (list):     Pass>>> list2 = Mylist ()>>> list2.append (3)>>> list2.append (5 )>>> list2.append (7)>>> list2[3, 5, 7]>>>--- List2 inherits the MyList list object

-Polymorphic: Different objects respond differently to the same method

>>>classA:defFun (self):Print('I'm a little a. ')        >>>classB:defFun (self):Print('I'm little b. ')        >>> A =A ()>>> B =B ()>>>A.fun () I'm little A. >>>B.fun () I'm little b. >>>The different behavior of the fun () object used by a two different objects

4. The properties and methods in the object, what is the actual programming?

Variables (attributes), Functions (methods)

5. What is the relationship between class and object?

The relationship between a class and an object is like a relationship between a mold and an item made with this mold. A class gives a uniform definition of all its objects, and each of his objects is an entity that conforms to that definition, so that the relationship between classes and objects is abstract and concrete.

6, the definition of the class may sometimes be less "quasi-object", sometimes abstract some, for example, we define a rectangle, then you will add those properties and methods for this?

Properties such as: length and width height, method such as: Calculate perimeter, area

7, the object-oriented characteristics of several:

Encapsulation: Working details on externally hidden objects

Inheritance: The mechanism by which subclasses automatically share data and methods between parent classes

Polymorphic: You can invoke the same method on different classes of objects, producing different results

Exercises:

1. Try to define a person class and generate class instance objects by following these prompts

Property: Name (the default name is "Turtle")

Method: Print Name

Hint: a reference to a property in a method should be added to self, such as Self.name

class Person :     ' Dusty '    def Print_name (self):         Print (self.name)>>> p = person ()>>> p.print_name () Dusty

2. Try defining a rectangle and generating the class instance object by following the prompts below

Properties: Length and width

Method: Set the length and->setrect (self), get the long and wide->getrect (self), get the area->getarea (self)

Hint: The form of a reference to a property in a method should be added to self, such as: Self.width

classRectanle:length= 5width= 4defSetRect (self):Print('Please enter the length and width of the rectangle ...') Self.length= Float (Input ('Length:')) Self.width= Float (Input ('Width:'))    defGetRect (self):Print('The length of this rectangle is:%.2f, Width is:%.2f'%(self.length,self.width))defGetarea (self):returnSelf.length *Self.widthr=Rectanle () r.getrect () R.setrect () R.getrect () R.getarea ()

"python36--Object"

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.