Home > other >  Python beginners of object-oriented method
Python beginners of object-oriented method

Time:09-24

Python beginners consult

In object-oriented solution to a problem process
For example:
Someone wants to go to the high-speed take high-speed rail from Beijing to Shanghai
High-speed rail is different shifts, time
The starting point (home, in a company)
Shanghai off the plane to the destination)
The object-oriented process how

CodePudding user response:

General reference code
 
# - * - encoding utf8 - * -

The class Place (object) :
Def __init__ (self, the name, address) :
The self. The name=name
The self. The address=address

Def __repr__ (self) :
Return u '% s' (% s) % (self. The name, the self. The address)

The class TripMode (object) :
Def __init__ (self) :
The self. The name='
The self. The schedule='
Self. Time='
Self. Start='
The self. The end='

Def __repr__ (self) :
Return u '% s % s (% s time,) from % s to % s' % (self. The name, the self. The schedule, the self. The time and the self. The start, the self. The end)

The class "Train" (TripMode) :
Def __init__ (self, the name, the schedule, time, start, end) :
The self. The name=name
The self. The schedule=schedule
Self. Time=time
Self. Start=start
The self. The end=end

The class People (object) :
Def __init__ (self, name) :
The self. The name=name

Def go (self, from_place to_place, by) :
From: print (' % s, % s, ride: % s, final destination: % s' % (self. The name, from_place, by to_place))

People=people (u 'Ming')
From_place=Place (u 'home' u 'Beijing XXX road 100)
To_place=Place (u), u 'in huangpu yuyuan garden old street no. 279)
Trip_mode="Train" (u 'high-speed' u 'G9999', u '9', u ' 'to Beijing railway station, u' Shanghai hongqiao station)
People go (from_place to_place, trip_mode)

People. The name=u 'daming'
From_place. Name=u 'companies'
From_place. Address=u '200 Beijing XXX road'
Trip_mode. Schedule=u 'G8888'
Trip_mode. Time=u 'if'
Trip_mode. Start=u 'Beijing west railway station'
Trip_mode. End=u 'Shanghai railway station'
People go (from_place to_place, trip_mode)


Xiao Ming, from my home (Beijing XXX road no. 100), the ride: high-speed G9999 (time of mine, from Beijing to Shanghai hongqiao station), the final destination: chenghuang temple (huangpu district of Shanghai yuyuan garden street no. 279)
Daming, from: company (Beijing XXX road no. 200), a high-speed G8888 (time ruler, set off from Beijing west station to Shanghai railway station), the final destination: chenghuang temple (huangpu district of Shanghai yuyuan garden street no. 279)
  • Related