1

我完全无法访问内部类中的外部类属性......即使我在内部类中创建外部类的对象* ,这在组合设计中毫无意义* ..即使那样我也无法访问它们..是有一种方法可以访问这些外部类属性吗?

情景是有一些跑车,只有在有想要购买的客户存在的情况下才会建造!..

namespace composition{
public class CustomCar
{
    #region Attributes
    private string name;
    private string plateno;
    private double cost;
    private CarCustomer _customer = new CarCustomer();

    #endregion

    #region properties

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public double Cost
    {
        get { return cost; }
        set { cost = value; }
    }


    public string PlateNo
    {
        get { return plateno; }
        set { plateno = value; }
    }



    public CarCustomer Customer
    {
        get { return _customer; }
        set { _customer = value; }
    }


    #endregion

    #region methods

    public CustomCar()
    {
        Console.WriteLine("I am in custom car");
    }



    public CustomCar(string s1, string pno, double c, string s2, double n, double bc)
    {
        this.Name = s1;
        this.PlateNo = pno;
        this.Cost = c;
        this.Customer.Name1 = s2;
        this.Customer.Nic1 = n;
        this.Customer.BargainCost = bc;
    }

    public double finalCost()
    {
        if (this.Customer.BargainCost < 10000)
        {
            double FinalCost = (this.Cost - this.Customer.BargainCost);
            return FinalCost;
        }
        else
        {
            return this.Cost;
        }

    }

    public void show()
    {
        Console.WriteLine(this.name + this.PlateNo + this.Customer.Name1 + this.Customer.Nic1);
    }

    #endregion


    public class CarCustomer
    {
        private string name1;
        private double Nic;
        private double bargainCost;


        public double BargainCost
        {
            get { return bargainCost; }
            set { bargainCost = value; }
        }

        public double Nic1
        {
            get { return Nic; }
            set { Nic = value; }
        }


        public string Name1
        {
            get { return name1; }
            set { name1 = value; }
        }

        public CarCustomer()
        {
            Console.WriteLine("I have a customer");
        }

        public CarCustomer(string n1, double i1, double bc)
        {
            this.Name1 = n1;
            this.Nic = i1;
            this.BargainCost = bc;
        }

        public void showCustomer()
        {
            Console.WriteLine("Customer name:   " + Name1);
            Console.WriteLine("Customer NIC:    " + Nic1);
        }
    }
}
}
4

2 回答 2

1

没有什么可以阻止您在 CarCustomer 中引用 CustomCar 对象。然后,这将为您提供对象之间的一对一引用。您是否在 CustomCar 的构造函数中创建此对象取决于您

public CustomCar(arguments)
{
    this.Customer.CustomCar = this;
}

或者您可以将其设置在属性访问器的集合中,由您决定。尝试这个

public class CustomCar
{
    private string name;
    private string plateno;
    private double cost;
    private CarCustomer _customer = new CarCustomer();

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public double Cost
    {
        get { return cost; }
        set { cost = value; }
    }


    public string PlateNo
    {
        get { return plateno; }
        set { plateno = value; }
    }



    public CarCustomer Customer
    {
        get { return _customer; }
        set { _customer = value; }
    }

    public CustomCar()
    {
        Console.WriteLine("I am in custom car");
    }



    public CustomCar(string name, string pno, double c, string customerName, double n, double bc)
    {
        this.Name = name;
        this.PlateNo = pno;
        this.Cost = c;
        this.Customer.Name1 = customerName;
        this.Customer.Nic1 = n;
        this.Customer.BargainCost = bc;
        this.Customer.Car = this;
    }

    public double finalCost()
    {
        if (this.Customer.BargainCost < 10000)
        {
            double FinalCost = (this.Cost - this.Customer.BargainCost);
            return FinalCost;
        }
        else
        {
            return this.Cost;
        }

    }

    public void show()
    {
        Console.WriteLine(this.name + this.PlateNo + this.Customer.Name1 + this.Customer.Nic1);
    }
}

    public class CarCustomer
    {
        private string name1;
        private double Nic;
        private double bargainCost;
        private CustomCar customer;

        public double BargainCost
        {
            get { return bargainCost; }
            set { bargainCost = value; }
        }

        public double Nic1
        {
            get { return Nic; }
            set { Nic = value; }
        }       


        public string Name1
        {
            get { return name1; }
            set { name1 = value; }
        }

        public CustomCar Car
        {
            get{return customer;}
            set{customer = value;}
        }

        public CarCustomer()
        {
            Console.WriteLine("I have a customer");
        }

        public CarCustomer(string n1, double i1, double bc)
        {
            this.Name1 = n1;
            this.Nic = i1;
            this.BargainCost = bc;                  
        }

        public void showCustomer()
        {
            Console.WriteLine("Customer name:   " + Name1);
            Console.WriteLine("Customer NIC:    " + Nic1);
        }
    }
于 2012-06-06T10:13:59.583 回答
1

当然你不能访问它们。您已将其保护级别设置为private。为了从外部资源获取它们,它们的保护级别必须与所需的访问级别一致。在这种情况下,您应该能够将修饰符更改为protected并能够访问它们。

但是,看看你的类设计,我认为使用自动 getter/setter 语法会更好。你没有在你的属性定义中做任何特别的事情,所以摆脱私有变量并将你的属性更改为这样是有意义的:

public string Name { get; set; }
public double Cost { get; set; }
public string PlateNo { get; set; }
public CarCustomer Customer  { get; set; }

您仍然可以通过属性公开访问变量,并且不会有额外变量的所有混乱。

于 2012-06-06T10:25:31.777 回答