私人访问与公共

时间:2016-05-11 06:39:05

标签: c# encapsulation

我的封装问题,我不知道问题出在哪里。为什么线条在创建后会被更改?这告诉我在我的" line-class"并需要封装。建议将不胜感激。 致电" pa.X = 4"和" startpos.Y = 7"这不应该改变我的路线,但确实如此。我希望程序完成后所有的行都不变。

Dotclass:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dot
{
    class Dot
    {
        private int x;
        private int y;

        public Dot()
        {
            this.X = 0;
            this.Y = 0;

        }
        public Dot(int x, int y)
        {
            this.X = x;
            this.Y = y; 
        }

        public int X
        {
            get
            { return x; }
            set { x = value; }
        }

        public int Y
        {
            get { return y; }
            set { y = value; }
        }
    }
}

LINECLASS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dot
{
    class Line
    {
        private Dot startdot;
        private Dot enddot;
        private double length;

        public Line(Dot pa, Dot pb)
        {
            this.startdot = pa;
            this.enddot = pb;            
        }

        public double Size()
        {
            double a = (double)(enddot.X - startdot.X); 
            double b = (double)(enddot.Y - startdot.Y);

            return length = Math.Sqrt(a * a + b * b);            
        }

        public Dot Position()
        {
            return startdot;
        }

    }
}

主:

    using System;
    namespace Dot
    {
        class Program
        {
            public static void Main()
            {
                Dot pa = new Dot();
                Dot pb = new Dot(-10, -10);
                Console.WriteLine("Dot pa, position = (" + pa.X + ", " + pa.Y + ")");
                Console.WriteLine("Dot pb, position = (" + pb.X + ", " + pb.Y + ")");
                Line line = new Line(pa, pb);
                Print("Run 1 off line", line);
                pa.X = 4;
                Print("Run 2 off line", line);
                Dot startpos = line.Position();
                startpos.Y = 7;
                Print("Run 3 off line", line);
            }
            private static void print(string text, Line line)
            {
                double length = line.Size();
                Dot startPos = line.Position();
                Console.WriteLine("\n" + text);
                Console.WriteLine("Längd = {0 :f4} length", length);
                Console.WriteLine("Position = ({0},{1})", startPos.X, startPos.Y);
                Console.ReadLine();
            }
        }
    }

1 个答案:

答案 0 :(得分:4)

  

为什么在创建线后可以更改线?

原因是class Dot引用类型,您需要类型struct

  // please, notice "struct"
  public struct Dot {
    // you don't need separate fields, but properties
    public int X {get; set;}
    public int Y {get; set;}

    public Dot(int x, int y) { 
      X = x;
      Y = y;
    }
  }

  ....

编辑:我建议将public Dot Position()转换为属性

 class Line {
   ...
   public Dot Position {
     get {
       return startdot;
     }
     set {
       startdot = value;
     }
   } 
 } 

所以你可以“控制角度”:

line.Position = new Dot(line.Position.X, 5);