创建封装属性 [英] create property fo encapsulation

查看:66
本文介绍了创建封装属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为Perperty创建一个私有字段时,它是强制性的吗?

何时不创建?

when for Perperty created a private field,Do it is compulsor??

and when do not created?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace ApplicationStartSample
{
    public class Configuration
    {
        private Configuration()
        {
        }

        private static Configuration _Current;
        public static Configuration Current
        {
            get
            {
                if (_Current == null)
                    _Current = new Configuration();

                return _Current;
            }
        }

        private const string Path = "Software\\MFT\\Registry Sample";

        public bool EnableWelcomeMessage
        {
            get
            {
                return bool.Parse(Read("EnableWelcomeMessage", "false"));
            }
            set
            {
                Write("EnableWelcomeMessage", value.ToString());
            }
        }

        public string Company                      //why do not create private field?
        {
            get
            {
                return Read("Company", "MFT");
            }
            set
            {
                Write("Company", value);
            }
        }

        public string WelcomeMessage
        {
            get
            {
                return Read("WelcomeMessage", string.Empty);
            }
            set
            {
                Write("WelcomeMessage", value);
            }
        }

        public string Server
        {
            get
            {
                return Read("Server", ".\\Sqldeveloper");
            }
            set
            {
                Write("Server", value);
            }
        }

        public string Database
        {
            get
            {
                return Read("Database", "Shop2");
            }
            set
            {
                Write("Database", value);
            }
        }

private static string Read(string name, string @default)
{
    RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, false);

    if (key == null)
        return @default;

    try
    {
        string result = key.GetValue(name).ToString();
        key.Close();

        return result;
    }
    catch
    {
        return @default;
    }
}

private static void Write(string name, string value)
{
    try
    {
        RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, true);

        if (key == null)
            key = Registry.CurrentUser.CreateSubKey(Path);

        key.SetValue(name, value);
        key.Close();
    }
    catch
    {
    }
}
    }
}

推荐答案

这不是强制性的,但强烈建议使用.

看起来很傻,但是它的作用是定义您的类与外界之间的接口.

如果直接将calls成员定义为Public,则会发生两件事:
1)任何人都可以对其进行读写
2)您无法更改.

第一个可能很重要:如果您拥有该成员的私有身份,并且可以通过公共(或受保护的或内部的)财产提供访问权限,那么您可以拥有一个公共获取者和一个私有设置者:外部世界可以自由访问您提供的信息,但无法更改.这可以帮助维护您的数据完整性,因为它们无法调整"数据.少量的数据,使您的代码做奇怪的事情!

第二个甚至更重要:如果您向外部世界展示内部字段,那么对代码进行的任何更改都必须使这些字段保持不变.您无法更改代码的工作方式.举例来说,您从一个SQLCE数据库开始,并将命令公开.您的程序可以正常工作,并且您的课程也可以使用.如此之多,以至于SQLCE不再足够了:切换到SQL Server.您需要做的就是将SqlCeCommand更改为SqlCommand.完毕!测试您的课程,一切都完美.除了,突然没有其他人的代码可以编译...

使用属性可以封装我们的数据,并在不揭示内部工作原理的情况下,揭示关于类的逻辑上必要的内容.更安全,更清洁,更可靠!
It''s not compulsory, but it is strongly recommended.

It seems silly, but what it does is define the interface between your class and the outside world.

If you define your calls member directly as Public, then two things happen:
1) Anyone can read or write to them
2) You can''t change them.

The first can be important: If you have the member private, and provide access via a public (or protected, or internal) property, then you can have a public getter, and a private setter: the outside world is at liberty to access the info you provide, but it cannot change it. This can help preserve your data integrity, because they can''t "tweak" bits of your data and make your code do strange things!

The second is even more important: If you reveal your internal fields to the outside world, then whatever changes you make to your code must keep those fields the same. You cannot change the way your code works. Say for example, you start with an SQLCE database, and make the command public. Your program works, and your class gets used. So much so, that SQLCE if no longer sufficient: switch to SQL Server. All you need to do is change from SqlCeCommand to SqlCommand. Done! Test your class, everything is perfect. Except, suddenly nobody else''s code compiles...

Properties allow use to encapsulate our data, and reveal what is logically necessary about our classes without revealing the inner workings. Safer, cleaner, and much more reliable!


这篇关于创建封装属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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