使用类中的静态成员创建对象

本文关键字:静态成员 创建对象 | 更新日期: 2023-09-27 18:30:30

我正在创建一个构造函数,它需要从另一个类中获取所有变量。目前我有:

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 类中的所有变量?

使用类中的静态成员创建对象

在构造函数中:

{
    Person.Adress = newAdress;
}
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