使用其他类的类字段

本文关键字:字段 其他 | 更新日期: 2024-10-21 09:26:48

我对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。我的问题是:

如何使用类2中的字段x,以及如何使用按钮单击事件下的字段x、y和z?

使用其他类的类字段

您可能希望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());
}

如果我误解了你的意图,请告诉我。希望这能有所帮助!

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

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

class2中,您需要从class1 生成一个对象

public class class2
{
class1 class1 = new class1(); 
public double z = class1.x + 5;
}

这里的每个人都为您提供了访问字段的正确答案,但在面积和体积的情况下,您正在以过程而非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;
    }
}