2012-04-10 46 views
-1

我正在創建一個構造函數,它需要從另一個類中獲取所有變量。目前我有:使用類中的靜態成員創建對象

public class Person 
{ 
    public Person(Address.AddressDetails newAddress , string newTitle, string  newForname, string newSurname, string newTel, 
         string newMob, string newEmail) 
     { 
     } 
     static public int ID; 
     static public string Title; 
     static public string Decorations; 
     static public string Forename; 
     static public string Surname; 
     static public string Telephone; 
     static public string Mobile; 
     static public string Email; 
     static public Address.AddressDetails Address; 
} 

Address.AddressDetails中的變量都是靜態的。 我該如何讓Person.Address代表AddressDetails類中的所有變量?

+1

的所有成員? – 2012-04-10 09:50:28

+0

擁有所有這些靜態公共領域是非常糟糕的設計。他們將在所有「Person」實例中共享 - 這是什麼原因? – Oded 2012-04-10 09:50:58

+0

從其他靜態字段複製也有點奇怪。 – 2012-04-10 09:51:42

回答

-1

在構造函數中:

{ 
    Person.Adress = newAdress; 
} 
-1
public class Person 
{ 
    public static Assign(Address.AddressDetails newAddress , string newTitle, string  newForname, string newSurname, string newTel, 
         string newMob, string newEmail) 
    { 
     Address = newAddress; 
     Email = newEmail; 
     .... 
     ... 

    } 

    static private int ID; 
    static private string Title; 
    static private string Decorations; 
    static private string Forename; 
    static private string Surname; 
    static private string Telephone; 
    static private string Mobile; 
    static private string Email; 
    static private Address.AddressDetails Address;  

} 

這樣的事情。

  • 做出static public方法來分配值
  • 讓你爲什麼要使用所有靜態域結合常規的構造方法的類private
+0

@downvoter:謹慎解釋? – Tigran 2012-04-10 10:03:13