2016-12-07 7 views
0

私はこれらのクラスと、このデータベースのファイルデータを格納する関数を書きました。私はファイル所有者#とファイル番号#を持つファイルを与えられます。私は余分なファイル を、一致するファイルの所有者と番号でデータベースから移動し、指定された所有者のファイル所有者リストに入れたいと思います。それはNoneを返しますが、所有者がすでにそのファイル番号#のファイルを持っている場合はDuplicateIdErrorを発生させます。 所有者またはファイルがデータベースに存在しない場合、MissingIdErrorが発生します。 私の質問は、どのように私は現在よ、私はDatabaseクラスに私の機能(ファイルクラスおよび所有者クラスに別のクラスから複数のインスタンス変数やメソッドを呼び出すのですか?複数のインスタンス変数とメソッドを関数に呼び出す方法は?

class File:   
    self.file_num 
class Owners: 
    self.owner_id   
    self.owner_list 
class Database: 
    def loan_book(self, owner_id, file_num): 
     extra_file = file # Calls file from File Class? 
     for i in self.owner_id: # Calls from Owners Class? 
      for j in self.owner_list: # Calls from Owners Class? 
       if extra_file == owner_id and file_num: 
        raise DuplicateIdError 
       elif extra_file != owner_id and file_num: 
        extra_file.append(self.owner_list) 
       else:   
        raise MissingIdError 

答えて

1

あなたがのインスタンスを持っているのでOwner sおよびFile秒、あなたは__init__()別名、あなたのクラスで初期化が必要になります。あなたのデータベースは、所有者のリストへの参照を保持している。そして、あなたは正しいものを見つけるために、それらの所有者を反復処理することができます。

class File: 
    def __init__(self, num):   
     self.num = num 

class Owner: 
    def __init__(self, id, files=[]): 
     self.id = id 
     self.files = files 

    def add_file(self, file_num): 
     extraFile = File(file_num) 
     self.files.append(extraFile) 

class Database: 
    def __init__(self, owners=[]): 
     self.owners = owners 

    def loan_book(self, owner_id, file_num): 
     for owner in owners: 
      if owner.id == owner_id: 
       for file in owner.files: 
        if file.id == file_num: 
         raise DuplicateIdError 
       owner.add_file(file_num) 
     raise MissingIdError 

次に、これらを利用するために、あなたは、このようなのような何かができる:

f = File(1) 
owner1 = Owner(1, [f]) 
owner2 = Owner(2) 

db = Database([owner1, owner2]) 
db.loan_book(2, 1) # Adds file with id 1 to owner2's list 
db.loan_book(1, 1) # Raises DuplicateIdError 
db.loan_book(3, 1) # Raises MissingIdError 
1

を別のクラスでは、変数、インスタンスとメソッドにアクセスする方法を理解するためには、しかし、このquestion and answers in stackoverflow

を読むことができます、これはそれを行う方法の基本的な例です。

class A: 
    VAR_A = "i'm var_a" 
    def __init__(self): 
     self.a = "I'm in class A" 

    def method_a(self): 
     return "i'm method_a" 

class B: 
    VAR_B = "i'm var_b" 
    def __init__(self): 
     self.b = "I'm in class B" 

    def method_b(self): 
     return "i'm method_b" 

class C: 
    def __init__(self): 
     # Initialise class/object A 
     A.__init__(self) 
     # create an instance variable which hold A() object 
     self.class_a = A() 
     # Initialiser class/object B 
     B.__init__(self) 
     # create an instance variable which hold B() object 
     self.class_b = B() 

    def my_function(self): 
     # calling a which is an instance variable of the class A 
     print("Expected output: I'm in class A\t\tGot: ", self.class_a.a) 
     # calling VAR_A which is A's class attribute 
     print("Expected output: i'm var_a\t\tGot: ", self.class_a.VAR_A) 
     # calling method_a which is an A's method 
     print("Expected output: i'm method_a\t\tGot: ", self.class_a.method_a()) 

     # calling b which is an instance variable of the class B   
     print("Expected output: I'm in class B\t\tGot: ", self.class_b.b) 
     # calling VAR_B which is B's class attribute 
     print("Expected output: i'm var_b\t\tGot: ", self.class_b.VAR_B) 
     # calling method_b which is an B's method 
     print("Expected output: i'm method_b\t\tGot: ", self.class_b.method_b()) 

# Run the script 
if __name__ == '__main__': 
    app = C() 
    app.my_function() 

出力:

Expected output: I'm in class A  Got: I'm in class A 
Expected output: i'm var_a   Got: i'm var_a 
Expected output: i'm method_a  Got: i'm method_a 
Expected output: I'm in class B  Got: I'm in class B 
Expected output: i'm var_b   Got: i'm var_b 
Expected output: i'm method_b  Got: i'm method_b 
関連する問題