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