Get the instance name of this

后端 未结 4 1772
攒了一身酷
攒了一身酷 2021-01-22 08:50

Is this possible!!?!?!?!?

I\'m trying to make a set of classes that model a number of different types of things. Properties of these things change over time, and I want

4条回答
  •  既然无缘
    2021-01-22 09:34

    The normal way of doing this is to store the 'name' as a variable in the instance, for ex:

    // Initialize this like: var cat = new Cat("Tiger");
    class Cat {
        public String Name { get; private set; }
        public Cat(String name) 
        { 
            Name = name; 
        }
    }
    

    And then if you need to get tigers in more than one place, make it into a method that explicitly returns Tigers:

    // This can go into a class of cat-creating static methods.  Or maybe in the Cat class itself.
    public static Cat GiveMeATiger() {
        return new Cat("Tiger");
    }
    

提交回复
热议问题