[Python] base class objects &

one type

  1, abstract descriptions of the same class of things, abstract concepts
  2, grammar class definition:
    class keyword - naming rules: large hump StudentInfo; (small hump studentInfo)

    class class name:
      Pass
  3, Properties: understood as the term is used to describe the class (name, age, color, height, ...)
  4, method: also known function, to understand the behavior of the function class can be achieved (eating, sleeping, play Peas, ...)

Second, objects: real, a particular instance of the class
  1, the object: also called Example
  2, the creation of objects: class is called instantiation


  3. The method of Object ()
    Gets the value of properties of an object: object attribute
    assigns them: = new Object Attribute Value

  4, to dynamically assign attribute object
    class is not defined in this attribute, the assignment of which to the dynamic object in an object, the object can only use this property

  5, self themselves, itself
    who calls who is self, self refers to the object of the current call

Third, create an object

  1, the object creation: p1 = the Person ()
    (1) open memory, storage objects
    (2) automatically call the init method

  2, instance variables
    to initialize instance variables, initialization of the object property
    instance variables: Variables Object -> Object Properties

    All objects owned by the instance variables, each object has a simultaneously and target
    self variable name -> represents the instance variables (instance properties)

  3, init
    will generally be initialized (properties of the object) in the instance variable init method

1  # 2 and auto roads, road vehicle is calculated up finish time 
2  # Analysis: time = distance / speed 
3  # from: the length of the road ---> length (noun) 
4  # speed: vehicle speed - -> speed (noun) 
5  #            function: Compute the upstream highway vehicles finish time 
. 6  class road:
 . 7      DEF  the __init__ (Self, length, name):
 . 8          the self.name = name
 . 9          self.length = length
 10  
. 11      DEF GET_TIME (Self, car_obj):   # parameter: the nature of the object variable car_obj car 
12 is          T = self.length / car_obj.v
 13 is          Print( " % S% s car traveling on the road is completely process time .2f% " % (car_obj.brand, the self.name, T))
 14  
15  
16  class Car:
 . 17      DEF  the __init__ (Self, V, Brand) :   # attribute v, indicates the speed 
18 is          self.v = V
 . 19          self.brand = brand   # car brands 
20 is  
21 is      DEF GET_TIME (Self, road_obj):   # parameter: the object road 
22 is          T = road_obj.length / self.v
 23 is          Print ( " % s% s car traveling on the road is completely process time .2f% " %(self.brand, road_obj.name, T))
 24  
25  
26 is as road1 Road = (100, " broad road " )
 27 CAR1 = Car (300, " Alto " )
 28  
29 road1.get_time (CAR1)   # Alto broad road car in on the road the entire distance of travel time is 0.33 
30 car1.get_time (road1)   # Alto broad road car driving on the road the entire distance of time is 0.33

Guess you like

Origin www.cnblogs.com/Tree0108/p/12111956.html