Informatika2-2014/Gyakorlat05

A MathWikiből
(Változatok közti eltérés)
8. sor: 8. sor:
 
Egy metódust az alábbi módon definiálunk pythonban:
 
Egy metódust az alábbi módon definiálunk pythonban:
 
<python>
 
<python>
class Foo:
+
class Brick:
 
   def setx(self, x):
 
   def setx(self, x):
 
       self.x = x
 
       self.x = x
17. sor: 17. sor:
 
* Az alábbi módon hozhatunk létre objektumokat és hívhatjuk meg azok metódusait:
 
* Az alábbi módon hozhatunk létre objektumokat és hívhatjuk meg azok metódusait:
 
<Python>
 
<Python>
f = Foo()
+
f = Brick()
 
f.setx(5)
 
f.setx(5)
 
f.bar()
 
f.bar()
Foo.setx(f,5)
+
Brick.setx(f,5)
Foo.bar(f)
+
Brick.bar(f)
 
</Python>
 
</Python>
 
* Fontos, hogy Pythonban futási időben is változhatnak az osztályhoz tartozó változók:
 
* Fontos, hogy Pythonban futási időben is változhatnak az osztályhoz tartozó változók:
30. sor: 30. sor:
 
* Minden egyes objektum mögött a háttérben létezik egy szótár, ami tartalmazza az osztály elemeit:
 
* Minden egyes objektum mögött a háttérben létezik egy szótár, ami tartalmazza az osztály elemeit:
 
<Python>
 
<Python>
g = Foo()
+
g = Brick()
 
vars(g)
 
vars(g)
 
g.__dict__
 
g.__dict__
69. sor: 69. sor:
 
== Feladatok ==
 
== Feladatok ==
 
=== 1. feladat ===
 
=== 1. feladat ===
Your task is to implement the simple elevator in Python using classes. Before implementation, please
+
Ismételd meg a fenti kódrészleteket.
play the demo. It would help you understand the project. We would like you to implement your own
+
“efficiency” strategy for dealing with customers. The default strategy is the simple “start at the bottom,
+
go to the top, then go to the bottom”. Can you write a better strategy, one that is more efficient?
+
+
Project Description / Specification
+
+
1. Create three classes: Building, Elevator, and Customer.
+
2. Equip the building with an elevator. Ask user to customize the number of floors and the
+
number of customers.
+
3. Program should have error checking to make sure the user inputs are valid. For example, if a
+
user gives non-integer inputs, notify the user that the inputs are incorrect and prompt again.
+
4. Each customer starts from a random floor, and has a random destination floor.
+
5. Each customer will use the elevator only once, i.e., when a customer moves out of the elevator,
+
he/she will never use it again.
+
6. When all customers have reached their destination floor, the simulation is finished.
+
7. Part of the grade on this project will be the appropriateness of your classes, methods, and any
+
functions you use. The quality of the code will now matter as well as the performance. No
+
skeleton file is provided; you need to come up with this yourself.
+
8. All classes’ methods require a docstring for a general description of the method.
+
9. Implement both your own strategy and the default strategy and compare. Your strategy does
+
not have to be better (your grade will not depend on it) but the comparison is required.
+
10. Don’t use any global variables.
+
 
=== 2. feladat ===
 
=== 2. feladat ===
* Írj mátrix osztályt az alábbi metófudokkal:
+
* Írj mátrix osztályt az alábbi metódusokkal:
 
<python>
 
<python>
 
   class Matrix:
 
   class Matrix:

A lap 2014. március 11., 09:56-kori változata

Tartalomjegyzék

Osztályok

  • Az osztályok egységbe zárnak és a felesleges információt elrejtik.
  • Az osztályok metódusokat és változókat tartalmaznak. Metódusokat tekintsük osztályokon belüli függvényeknek.
  • Tekintsünk erre úgy, hogy a kód egy részét (mind tárolókat, mind a függvényeket nézve) összefogjuk.
  • Egy objektum egy osztály konkrét megvalósítása.

Egy metódust az alábbi módon definiálunk pythonban:

class Brick:
   def setx(self, x):
      self.x = x
   def bar(self):
      print self.x
  • Itt a lényeges dolog a self változó, ami mindig az első argumentuma a metódusnak. Ez mindig a konkrét megvalósított objektumot jelöli. Tehát külön értékadás nélkül self.x az osztályon (objektumon) belüli x változó, aminek az értékét állítjuk be a setx függvénnyel.
  • Az alábbi módon hozhatunk létre objektumokat és hívhatjuk meg azok metódusait:
f = Brick()
f.setx(5)
f.bar()
Brick.setx(f,5)
Brick.bar(f)
  • Fontos, hogy Pythonban futási időben is változhatnak az osztályhoz tartozó változók:
del f.x
  • Itt tehát magát a változót töröljük.
  • Minden egyes objektum mögött a háttérben létezik egy szótár, ami tartalmazza az osztály elemeit:
g = Brick()
vars(g)
g.__dict__
  • Konstruktornak hívjuk azt a metódust, ami az objektum létrehozásával együtt hívódik meg:
class Test:
    def __init__(self):
        print "Mostmar elhiszed?"
 
x = Test()
  • A destruktor konzisztens módon az objektum törlésével együtt hívódik meg.
>>> class A:
...   pass
... 
>>> A.v1 = 10
>>> x=A() # létrehozok két példányt
>>> y=A()
>>> x.v1  # példányon belül is elérem az osztály szintű változót
10
>>> x.v1 = 34 # itt viszont létrehozok egy példány szintűt aminek a neve felülírja az osztály szintűt
>>> y.v1
10
>>> x.v1
34
>>> A.v1
10
>>> A.v1 = 12 # itt az osztályszintű változót változtatjuk
>>> x.v1 # mivel felülírtuk a v1-t az x példányban így ezen már nincs hatása
34
>>> y.v1
12
>>> A.v1
12

Feladatok

1. feladat

Ismételd meg a fenti kódrészleteket.

2. feladat

  • Írj mátrix osztályt az alábbi metódusokkal:
  class Matrix:
    def const(self,n,x):
      ...
    def zeros(self,n):
      ...
    def printer(self):
      ...
    def set(self,i,j,x):
      ...
    def get(self,i,j):
      ...
    def hasValue(self,i,j):
      ...
    def getLine(self,i):
      ...
    def pow(self):
      ...
    def product(self,v)
  • próbálj meg hibakezelést beépíteni a függvényekbe (try --- except)
Személyes eszközök