使用其他类的类字段

时间:2012-08-07 00:58:09

标签: c# oop class

我对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 个答案:

答案 0 :(得分: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());
}

如果我误解了你想要做的事,请告诉我。希望这有帮助!

答案 1 :(得分: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

答案 2 :(得分:0)

class2中的

您需要从class1

创建一个对象
public class class2
{
class1 class1 = new class1(); 

public double z = class1.x + 5;
}

答案 3 :(得分: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;
    }
}