C# 使用静态对象

C# 使用静态对象,c#,C#,我的理解是,任何不修改constaining类状态的方法都是静态的主要候选方法,因为它不涉及实例。一个实例将包含类的数据(字段/属性),因此如果我有一个person类,其属性名为Name(只有一个属性),并且我没有修改该属性,那么我的类可以设置为静态,但当然,该函数可以处理另一个对象。当检查一个方法是否应该是静态的时,这是正确的选择吗 静态变量被描述为全局变量,但这与任何公共变量有什么区别?所有变量都在服务器的内存中,并将在重新启动等过程中丢失。然后使用此变量保存昂贵的数据(例如在循环中运行存储

我的理解是,任何不修改constaining类状态的方法都是静态的主要候选方法,因为它不涉及实例。一个实例将包含类的数据(字段/属性),因此如果我有一个person类,其属性名为Name(只有一个属性),并且我没有修改该属性,那么我的类可以设置为静态,但当然,该函数可以处理另一个对象。当检查一个方法是否应该是静态的时,这是正确的选择吗

静态变量被描述为全局变量,但这与任何公共变量有什么区别?所有变量都在服务器的内存中,并将在重新启动等过程中丢失。然后使用此变量保存昂贵的数据(例如在循环中运行存储过程等)

谢谢

“如果我有一个人和一个 属性名(仅此而已) 一个属性),我不会修改 然后可以设置我的类 “作为静态”

这是不对的。person类几乎可以肯定是一个域实体。可以想象,您可能希望在Person实体上有一个静态方法,但我从未遇到过。在您的示例中,您的“Name”属性将有一个私有setter

任何事物的静态特性都应该为您的领域建模,而不是作为语言实现细节

“如果我有一个人和一个 属性名(仅此而已) 一个属性),我不会修改 然后可以设置我的类 “作为静态”

这是不对的。person类几乎可以肯定是一个域实体。可以想象,您可能希望在Person实体上有一个静态方法,但我从未遇到过。在您的示例中,您的“Name”属性将有一个私有setter

任何事物的静态特性都应该为您的领域建模,而不是作为语言实现细节

我的理解是,任何不修改constaining类状态的方法都是静态的主要候选方法,因为它不涉及实例

我不会说它是一个主要的候选人,而只是一个候选人。我个人认为,如果API从逻辑和算法的角度认为方法应该是静态的,那么方法应该是静态的。从技术上讲,任何不改变状态的方法都可以是静态的,但我不认为它一定应该是静态的

我没有修改这个属性,那么我的类可以设置为静态的,但是当然,这个函数可以处理另一个对象

只有在C#中完全没有实例变量的情况下,才能将类设置为静态。静态类的“实例”不存在(也不可能存在),非静态成员也不能存在

现在-为了我认为你真正想要的

静态变量和非静态变量之间的区别与访问方式有关。在类上定义静态字段时,定义的字段将始终有一个单个实例(字段实例)绑定到类本身。另一方面,当您定义一个非静态的普通字段时,可能会有许多字段实例,每个实例都位于类的一个实例中

这就是为什么静态变量通常被描述为全局变量的原因——任何可以访问类的东西都可以访问它的静态变量的一个副本(只要它是可公开访问的)。但是,非静态字段是不同的-您需要对类的特定实例进行引用,以便读取或写入非静态成员

我的理解是,任何不修改constaining类状态的方法都是静态的主要候选方法,因为它不涉及实例

我不会说它是一个主要的候选人,而只是一个候选人。我个人认为,如果API从逻辑和算法的角度认为方法应该是静态的,那么方法应该是静态的。从技术上讲,任何不改变状态的方法都可以是静态的,但我不认为它一定应该是静态的

我没有修改这个属性,那么我的类可以设置为静态的,但是当然,这个函数可以处理另一个对象

只有在C#中完全没有实例变量的情况下,才能将类设置为静态。静态类的“实例”不存在(也不可能存在),非静态成员也不能存在

现在-为了我认为你真正想要的

静态变量和非静态变量之间的区别与访问方式有关。在类上定义静态字段时,定义的字段将始终有一个单个实例(字段实例)绑定到类本身。另一方面,当您定义一个非静态的普通字段时,可能会有许多字段实例,每个实例都位于类的一个实例中

这就是为什么静态变量通常被描述为全局变量的原因——任何可以访问类的东西都可以访问它的静态变量的一个副本(只要它是可公开访问的)。但是,非静态字段是不同的-您需要对类的特定实例进行引用,以便读取或写入非静态成员


.

在C#中没有真正的globl变量,因为该术语在其他语言(如C)中使用。静态对象和全局对象之间的区别在于,静态对象具有与其定义的类相关的权限。这是一件非常好的事情,因为您可以创建只能由一个类的成员访问的私有静态成员。滥用全局变量的最大问题是对单个全局变量的许多依赖性,而单个全局变量可能会演变为
public class Person {
    public static string Name { get; set; }

    public Person(string name) { Name = name; }

    public override string ToString() {
        return Name;
    }
}

Person dan = new Person("Dan";
Person john = new Person("John";

// outputs "John"
Console.WriteLine(john);

// outputs "John" again, since Dan doesn't have a name property all his own
// (and neither does John, for that matter)
Console.WriteLine(dan);
public class Person {
    public string Name { get; set; }
    public static House House { get; set; }

    public Person(string name, House house) {
        Name = name;
        House = house;
    }

    public override string ToString() {
        return String.Concat(Name, ", ", House);
    }
}

public class House {
    public double SquareFootage { get; set; }

    public House(double sqft) { SquareFootage = sqft; }

    public override string ToString() {
        return String.Format("House - {0} sq. ft.", SquareFootage);
    }
}

House danAndKatsHouse = new House(1000.0);
House johnsHouse = new House(2000.0);

Person dan = new Person("Dan", danAndKatsHouse);

// outputs "Dan, House - 1000 sq. ft." as expected
Console.WriteLine(dan);

Person kat = new Person("Kat", danAndKatsHouse);

// outputs "Kat, House - 1000 sq. ft.", again as expected
Console.WriteLine(kat);

Person john = new Person("John", johnsHouse);

// outputs "John, House - 2000 sq. ft.", so far so good...
Console.WriteLine(john);

// but what's this? suddenly dan and kat's house has changed?
// outputs "Dan, House - 2000 sq. ft."
Console.WriteLine(dan);
House danAndKatsHouse = new House(1000.0);

// (after making Person.House property non-static)
Person dan = new Person("Dan", danAndKatsHouse);
Person kat = new Person("Kat", danAndKatsHouse);

dan.House.SquareFootage = 1500.0;

// outputs "1500"
Console.WriteLine(kat.House.SquareFootage);
House smallerHouse = new House(1000.0);
House biggerHouse = new House(2000.0);

Person dan = new Person("Dan", smallerHouse);
Person john = new Person("John", biggerHouse);
Person bill = new Person("Bill", smallerHouse);

bill.House.SquareFootage = 1250.0;

// yikes, Dan's house just changed...
Console.WriteLine(dan);
public class Person {
    public int Id { get; private set; }
    public string Name { get; set; }
    public int HouseId { get; set; }

    private static int LastIdValue { get; set; }
    private static Dictionary<int, Person> People { get; set; }

    static Person() {
        LastIdValue = 0;
        People = new Dictionary<int, Person>();
    }

    // make the constructor private to disallow direct instantiation
    private Person(int id, string name, int houseId) {
        Id = id;
        Name = name;
        HouseId = houseId;
    }

    // only permit construction through this function, which inserts
    // the new Person into the static dictionary before returning it
    static public Person NewPerson(string name, int houseId) {
        Person p = new Person(LastIdValue++, name, houseId);
        People.Add(p.Id, p);
        return p;
    }

    static public Person getPersonById(int id) {
        Person p = null;
        return People.TryGetValue(id, out p) ? p : null;
    }
}

public class House {
    public int Id { get; private set; }
    public int SquareFootage { get; set; }

    private static int LastIdValue { get; set; }
    private static Dictionary<int, House> Houses { get; set; }

    static House() {
        LastIdValue = 0;
        Houses = new Dictionary<int, House>();
    }

    // make the constructor private to disallow direct instantiation
    private House(int id, int sqft) {
        Id = id;
        SquareFootage = sqft;
    }

    // only permit construction through this function, which inserts
    // the new House into the static dictionary before returning it
    static public House NewHouse(int sqft) {
        House h = new House(LastIdValue++, sqft);
        Houses.Add(h.Id, h);
        return h;
    }

    static public House getHouseById(int id) {
        House h = null;
        return Houses.TryGetValue(id, out h) ? h : null;
    }
}

House firstHouse = House.NewHouse(1000.0);
House secondHouse = House.NewHouse(2000.0);

Person dan = Person.NewPerson("Dan", firstHouse.Id);
Person kat = Person.NewPerson("Kat", firstHouse.Id);
Person john = Person.NewPerson("John", secondHouse.Id);

House dansHouse = House.getHouseById(dan.HouseId);
House katsHouse = House.getHouseById(kat.HouseId);

// this prints
if (katsHouse == dansHouse) { Console.WriteLine("Dan and Kat live in the same house."); }

// this also prints
if (dansHouse == firstHouse) { Console.WriteLine("Dan and Kat live in the first house."); }

// this does not print
if (dansHouse == secondHouse) { Console.WriteLine("Dan and Kat live in the second house."); }
public class Person {
    public static string Name { get; set; }
}