如何在方法中设置属性? [英] how to set propertise in the method?

查看:87
本文介绍了如何在方法中设置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在方法中设置属性

我该怎么做?



i want to set propertiesin the method
how can i do that?

namespace OOP
{


    enum  FurColor
    {
         black,
         white,
         broun

    };
    class Lion:ILion
    {
 

        public FurColor furcolor
        {
            get;
            set;
        }


        #endregion




       #region Methods



//here Getting furcolor and will update  the enum  FurColor value 
        public void ChangeFurColor(FurColor furcolor)
        {

        }

推荐答案

在你的方法 ChangeFurColor 你可以写这个.furcolor = furcolor ;;这是写这个时的一个案例。是绝对必要的;并且它回答了你的问题。



然而,问题本身毫无意义,因为你不需要这种方法。您的属性 furcolor 是公共的,只读/只,因此用户可以在没有此方法帮助的情况下分配它。顺便说一下,重命名为大写: FurColor ,不用担心,即使这与类型名称相同也是正确的;根本不是问题,而不是坏的风格。



-SA
In you method ChangeFurColor you can write this.furcolor = furcolor;; and this is one of the cases when writing "this." is absolutely necessary; and it answers your question.

However, the question itself makes no sense, because you don't need this method. You property furcolor is public and read/only, so the user can assign it without the help of this method. By the way, rename it to capitalize: FurColor, and don't worry, it will be correct even though this is the same as the type name; not a problem at all, and not the bad style.

—SA


一个重要的类设计的一个方面是控制对属性的访问:在很多情况下,您确实希望类的任何用户(使用者)能够设置和获取属性的值。



但是,通常情况下,你想控制设置属性,确保只能通过显式调用方法来改变它班级。为什么?好吧,除了其他原因,代码的不同部分可能存在对Property的值的复杂依赖性。您可能希望该属性有效地作为只读属性运行,同时仍然允许一个访问点来设置值。



使用自动获取/设置C#3.0为我们带来的设施,用公共getter和私有setter编写一个Property非常容易。如果您不熟悉自动实现的属性,请参阅:[ ^ ]。



例如:
An important aspect of Class design is controlling access to Properties: there may well be many cases where you do want any user (consumer) of your Class to be able to both set, and get, the value of Properties.

But, often, it's the case you want to control setting the Property, making sure it can only be changed through explicitly calling a method of the Class. Why ? Well, there may be complex dependencies on the value of the Property in different parts of your code, among other reasons. You may wish the Property to function, effectively, as a read-only Property while still allowing one point of access to set the value.

Using the "automatic" get/set facilities that C# 3.0 brought us, it's very easy to write a Property with a public getter and a private setter. If you are unfamiliar with auto-implemented Properties see: [^].

An example:
namespace OOP
{
    public enum FurColor { black, white, brown };

    public interface ILion
    {
        void ChangeFurColor(FurColor theFurColor);
    }

    public class Lion: ILion
    {
        // setter can be private only if the
        // CurrentFurColor Property is not
        // specified in the ILion interface
        public FurColor CurrentFurColor { get; private set; }

        // required interface implementation
        public void ChangeFurColor(FurColor newFurColor)
        {
            CurrentFurColor = newFurColor;
        }
    }
}

有人说(我忘了我读到的地方)属性用于提供接口类:这与变量(字段)的作用形成对比,变量(字段)是类的实现的一部分。



最后,我'我想表明我的坚定信念,即对于类,属性,变量等使用相同的名称,或者甚至使用具有不同字母大小写的相同名称是灾难的秘诀......即使.NET编译器可能会祝福你!



想一想你编写代码一年后看你的代码是多么可读:当你编写代码时查看名为'FurColor的枚举,名为'furcolor的属性,以及在方法的参数列表中,名为'furcolor的参数。更好的是,考虑一下其他人如何维护/更改你的代码(因为你们富有而且很有名),他们将能够理解代码的每一行的意图:)



冒着说教的风险,我鼓励你养成在代码现在中创建强大描述性名称的习惯,并开发一致的命名模式这使您的代码易于理解,这有助于您直观地识别......类,结构,枚举,变量,方法,属性,参数等之间的差异。

It has been said (I forget where I read it) that Properties are used to provide the interface to Classes: that's in contrast to the role of variables (fields) which are parts of the implementation of a Class.

Finally, I'd like to state my strong belief that using the same names for Classes, Properties, variables, etc., or even using the same names with different letter-case is a recipe for disaster ... even though the .NET compiler may bless you !

Think about how "readable" your code is going to be when you look at it a year after you wrote it: when you see an enum named 'FurColor, a Property named 'furcolor, and, in the parameter list of a method, a parameter named 'furcolor. Better yet, think about how when someone else has to maintain/change your code (because you are rich and famous), they will be able to understand the intent of each line of your code :)

At the risk of sounding "preachy," I would encourage you to develop a habit of creating "strong" descriptive names in your code now, and developing a consistent pattern of naming that makes your code easy to understand, that helps you recognize ... visually ... what the differences are between class, struct, enum, variable, method, property, parameter, etc.


这篇关于如何在方法中设置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆