C# 如何在已初始化的对象上定义方法?

C# 如何在已初始化的对象上定义方法?,c#,class,methods,C#,Class,Methods,我的意思是,如果我定义了一个类 class Square { public Square(int x, int y) { } } 我做到了 Square s = new Square(5,5); int a = s.Calc.Radios(); 我如何才能在其中执行Calc部分 就像类中的类一样。您需要在类方框中声明calc public calculator calc = new calculator(); 然后您将能够使用s.calc.收音机() 您需要在

我的意思是,如果我定义了一个类

class Square
{
     public Square(int x, int y)
     {
     }
}
我做到了

Square s = new Square(5,5);
int a = s.Calc.Radios();
我如何才能在其中执行
Calc
部分


就像类中的类一样。

您需要在类方框中声明calc

public calculator calc = new calculator();

然后您将能够使用s.calc.收音机()

您需要在类方框中声明calc

public calculator calc = new calculator();
然后您将能够使用s.calc.收音机()

声明一个单独(或嵌套)的类Calc,并在该类的类square中创建一个属性Calc。您可以通过私有集在square的构造函数中初始化属性

class Square
{
  public Calc Calc { get; private set;
  ...
  public Square()
  {
     this.Calc = new Calc();
  }
}
声明一个单独(或嵌套)的类Calc,并在该类的类square中创建一个属性Calc。您可以通过私有集在square的构造函数中初始化属性

class Square
{
  public Calc Calc { get; private set;
  ...
  public Square()
  {
     this.Calc = new Calc();
  }
}
在c#中,不能向初始化对象添加属性

c#是,所以对象不能在运行时被修饰,就像在一些弱类型语言中一样,例如JavaScript


您可以使用扩展方法扩展声明的对象。例如

public static class SomeStaticClass
{
    public Calc Calc(this Square square)
    {
        return new Clac(...
    }
}
您仅限于扩展方法,而不限于属性

只要
SomeStaticClass
在引用中可用,
Square
的实例将有一个
Calc
方法,该方法返回一个
Calc
对象,不管它是什么


如果
Square
不是
密封的
您可以从中继承,以创建一种新类型的
Square
,它具有
Calc
属性,例如

public class SqaureWithCalc : Square
{
    public SquareWithCalc(int x, int y) : base(x, y)
    {
    }

    public Calc Calc { get; set; }
}
所以你可以

var s = new SquareWithCalc(5, 5);

或者,最简单的是,因为您控制代码,所以您可以编辑它。在字里行间,也许你想要

public struct Square
{
    private readonly int x;
    private readonly int y;

    public Square(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int X
    {
        get
        {
            return this.x;
        }
    }

    public int Y
    {
        get
        {
            return this.y;
        }
    }

    public static IEnumerable<Radio> CalculateRadios(Square square)
    {
        // Do stuff,
            //// yield return radio; 
        // in a loop.
    }

    public IEnumerable<Radio> CalculateRadios()
    {
        return CalculateRadios(this);
    }
}
public struct Square
{
私有只读INTX;
私有只读INTY;
公共广场(内x,内y)
{
这个.x=x;
这个。y=y;
}
公共整数X
{
得到
{
归还这个.x;
}
}
公共智力
{
得到
{
把这个还给我;
}
}
公共静态IEnumerable计算器(平方)
{
//做事,
////收益率回传电台;
//在一个循环中。
}
公共IEnumerable CalculateRadios()
{
返回CalculateRadios(本);
}
}
在c#中,不能向初始化对象添加属性

c#是,所以对象不能在运行时被修饰,就像在一些弱类型语言中一样,例如JavaScript


您可以使用扩展方法扩展声明的对象。例如

public static class SomeStaticClass
{
    public Calc Calc(this Square square)
    {
        return new Clac(...
    }
}
您仅限于扩展方法,而不限于属性

只要
SomeStaticClass
在引用中可用,
Square
的实例将有一个
Calc
方法,该方法返回一个
Calc
对象,不管它是什么


如果
Square
不是
密封的
您可以从中继承,以创建一种新类型的
Square
,它具有
Calc
属性,例如

public class SqaureWithCalc : Square
{
    public SquareWithCalc(int x, int y) : base(x, y)
    {
    }

    public Calc Calc { get; set; }
}
所以你可以

var s = new SquareWithCalc(5, 5);

或者,最简单的是,因为您控制代码,所以您可以编辑它。在字里行间,也许你想要

public struct Square
{
    private readonly int x;
    private readonly int y;

    public Square(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int X
    {
        get
        {
            return this.x;
        }
    }

    public int Y
    {
        get
        {
            return this.y;
        }
    }

    public static IEnumerable<Radio> CalculateRadios(Square square)
    {
        // Do stuff,
            //// yield return radio; 
        // in a loop.
    }

    public IEnumerable<Radio> CalculateRadios()
    {
        return CalculateRadios(this);
    }
}
public struct Square
{
私有只读INTX;
私有只读INTY;
公共广场(内x,内y)
{
这个.x=x;
这个。y=y;
}
公共整数X
{
得到
{
归还这个.x;
}
}
公共智力
{
得到
{
把这个还给我;
}
}
公共静态IEnumerable计算器(平方)
{
//做事,
////收益率回传电台;
//在一个循环中。
}
公共IEnumerable CalculateRadios()
{
返回CalculateRadios(本);
}
}

您只需将Calc设置为Square类的属性

不过,另一个评论是,我可能会按照以下思路进行设计:

square.PerformSomeCalcMethod();
在哪里

这将允许您更改此方法的内部工作,而无需更改任何耦合到Calc属性的客户端


看看

您只需将Calc设置为Square类的属性

不过,另一个评论是,我可能会按照以下思路进行设计:

square.PerformSomeCalcMethod();
在哪里

这将允许您更改此方法的内部工作,而无需更改任何耦合到Calc属性的客户端


看看
square
calc
radios
是如何定义的。你的意思是你想拥有你的square.radios?如果我们看到
square
calc
radios
是如何定义的会更好。你的意思是你想拥有你的square.radios?