c# - getter和setter问题 [英] c# - Problem with getter and setter

查看:96
本文介绍了c# - getter和setter问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的代码有问题。有些类不应该写在propertys中,但有些类应该能够写和读它们。



详细问题:

我在Controller中将发票编码为私有集,因此我只能在此类中设置发票。这就是我想要的。

我还想要我可以在控制器中设置模型的属性,但不能在视图中设置。

但是如果我在模型中设置属性不公开,我也无法在控制器中设置属性。



我有4个类,他们看起来像模型 - 视图 - 控制器:



型号:

Hello everyone,

I have a problem with my Code. Some classes shouldn't be possible to write in propertys but some classes should be able to write and read them.

Problem in detail:
I coded the invoice in the Controller as private set, so i can only set the invoice in this class. that is what i want.
I also want that i can set the properties of the model in the controller, but not in the view.
But if i set the properties not public in the model, i cant set the properties in the controller too.

I have got 4 classes they looks like the model-view-controller:

Models:

namespace payOffice.Model
{
    class InvoicePosition
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }
}







namespace payOffice.Model
{
    class Invoice
    {
        public List<InvoicePosition> Positions { get; set; }
        public string Consumer { get; set; }
        public bool Completed { get; set; }
        public int InvoiceNumber { get; set; }

        public Invoice(string consumer)
        {
            Consumer = consumer;
        }
    }
}





控制器:



Controller:

namespace payOffice.Controller
{
    class InvoiceManager
    {
        public Invoice loadedInvoice {get;private set;}

        public InvoiceManager(...){...}

        public void addInvoicePosition(...)
        {
            //here it should be possible:
            loadedInvoice.Positions.Add(...);
        }
    }
}





浏览:



View:

namespace payOffice.View
{
    public partial class MyView : Form
    {
        InvoiceManager invoiceManager;
        public MyView()
        {
            InitializeComponent();
        }

        public void addInvoicePosition(...)
        {
            //its right so:
            invoiceManager.addInvoicePosition(...);

            //that shouldn't be possible!!!:
            invoiceManager.loadedInvoice.Positions.Add(...);

            //but that should be possible in other methods:
            Console.WriteLine(invoiceManager.loadedInvoice.Consumer);
            //and this should be possible in other methods too
            Console.WriteLine(invoiceManager.loadedInvoice.Positions[0].Price);
        }

    }
}





有人知道我怎么做使这成为可能?



Did someone have an idea how i can make this possible?

推荐答案

目前,您的所有属性都是公开的 - 这意味着如果他们知道该类存在,任何人都可以使用它们。



但是没关系,因为你的所有类都是独立的,在不同的命名空间中,没有任何东西来自其他任何东西。

所以即使使用 protected 或 internal 修饰符将无济于事,因为命名空间和缺少派生意味着有效私有无论如何。



基本上,使用你拥有的结构,如果任何类都可以阻止任何类访问属性。



你需要退后一步,看看你想要做什么,并考虑更好的类结构(heck - 任何类结构)和/或更好的封装限制访问s。



到目前为止,你所设计的并不是你能做的任何事情。
At the moment, all of your properties are public - which means anyone can use them if they know teh class exists.

But that's OK, because all your classes are independent, in different namespaces, and nothing is derived from anything else.
So even using protected or internal modifiers on your properties won't help, because the namespaces and lack of derivation mean there are effectively private anyway.

Basically, with the structure you have, you can't prevent any classes from accessing properties if any classes can.

You need to take a step back, look at what you are trying to do, and consider a better class structure (heck - any class structure) and / or better encapsulation to restrict access.

With what you have designed so far, they really isn't anything you can do.


hi, you have to remove either the setter or the getter.

class InvoicePosition
    {
        private string name { get; set; }    //switch the visibility to private, and the name //start with lowerCase 
//same with this variable
        private double price { get; set; }   //same here

//you can use a property named : [Name] to accecs your var [name]
 public string Name
        {
            get { return name; } // this ligne allow you to read the value
            set { name = value; } //// this ligne allow you to write into the variable
        }
 public double Price  
        {
            get { return price; }
        }
// => Price is only accescable for read.

    }




}

if you work with VS, then encapsualte and the code will be generated.

Hope it helps.


这篇关于c# - getter和setter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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