4

我对 C# 和 OOP 很陌生,所以请多多包涵。

我有两个具有不同名称空间的类:

namespace Class1

public class class1
{
 public double x;
 public double y;
}

...

namespace Class2

public class class2
{
 public double z = x + 5;
}

我创建了一个名为 add 的项目,并有一个带有按钮的表单。该按钮将添加 x、y 和 z。我的问题是:

如何在 class2 中使用字段 x 以及如何在按钮单击事件下使用字段 x、y 和 z?

4

4 回答 4

2

您可能希望在其构造函数class2中包含一个实例:class1

public class class2
{
 private readonly class1 _c1;
 public class2(class1 c1) { _c1 = c1; }

 public double z = _c1.x + 5;
}

至于如何在表单中通过按钮单击事件使用字段 x、y 和 z,您只需访问 class1 和 class2 实例上的公共字段 x、y 和 z:

protected void button_click(){
 class1 c1 = new class1();
 c1.x = 10;
 c1.y = 20;
 class2 c2 = new class2(c1);

 //do something with c1 and c2 now...
 Console.WriteLine("{0} {1} {2}", c1.x.ToString(), c1.y.ToString(), c2.z.ToString());
}

如果我误解了您要执行的操作,请告诉我。希望这可以帮助!

于 2012-08-07T01:05:09.390 回答
1

您不使用类字段(除非它们是静态的,但在您的情况下它们不是),而是使用对象字段。这是一个示例,说明如何实现您想要的。

public class1 {
  public double Radius;

  // Function to calculate the area
  public double Area(double Rad) {
    this.Radius = Rad;
    return Math.PI * Math.Pow(this.Radius, 2);
  }

}

public class2 {
  public double Depth;

  // Function to calculate the volume of a cylinder
  public double Volume(double Rad, double Dep) {
    this.Depth = Dep;

    // Create an instance of Class1 and use it to calculate the Volume
    var Obj1 = new class1();
    return Obj1.Area(Rad) * this.Depth;
  }
}

如何在按钮单击事件中使用上述内容

// Let's calculate an Area from a Radius
double SomeRadius = 1.234;

MyObj1 = new class1();
double Area = MyObj1.Area(SomeRadius);

double StoredRadius = MyObj1.Radius; // This will give you back the Radius stored by MyObj1, which is the same you passed to Area() function

// Now let's calculate a Volume, using the Radius we indicated earlier and a Depth
double SomeDepth = 4.567;
MyObj2 = new class2();
double Volume = MyObj2.Volume(SomeRadius, SomeDepth);

double StoredDepth = MyObj2.Depth; // This will give you back the Depth stored by MyObj2, which is the same you passed to Volume() function
于 2012-08-07T01:04:35.503 回答
0

这里的每个人都为您提供了访问字段的正确答案,但在面积和体积的情况下,您是在程序上解决问题,而不是 OO 方式。这是一个示例,向您展示如何访问内部字段,以及解决此类问题的 OO 方法:

public abstract class Shape
{
    public abstract double Area();
    public abstract double Perimeter();
}

public class Circle : Shape
{
    public double Radius;

    public override double Perimeter()
    {
        return 2 * Radius * Math.PI;
    }

    public override double Area()
    {
        return Radius * Radius * Math.PI;
    }
}

public class Square : Shape
{
    public double Side;

    public override double Perimeter()
    {
        return Side * 4;
    }

    public override double Area()
    {
        return Side * Side;
    }
}

public abstract class Solid
{
    public abstract double Volume();
}

public abstract class CircleBaseSolid : Solid
{
    protected Circle Base = new Circle();

    public double Radius
    {
        get { return Base.Radius; }
        set { Base.Radius = value; }
    }

    public double Height;
}

public class Cylinder : CircleBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height;
    }
}

public class Cone : CircleBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height / 3;
    }
}

public abstract class SquareBaseSolid : Solid
{
    protected Square Base = new Square();

    public double Side
    {
        get { return Base.Side; }
        set { Base.Side = value; }
    }

    public double Height;
}

public class SquareParallelepiped : SquareBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height;
    }
}

public class SquarePyramid : SquareBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height / 3;
    }
}
于 2012-08-07T08:20:23.633 回答
0

class2你需要从class1

public class class2
{
class1 class1 = new class1(); 

public double z = class1.x + 5;
}
于 2012-08-07T01:06:33.777 回答