我在此页面上看到了一个问题https://www.toptal.com/c-sharp/interview-questions 试着在VS中实现它,这是我的完整代码:
public class TopTalInterviewQuestions
{
//write code to calculate the circumference of the circle, without modifying the Circle class itself.
Circle myCircle = new Circle();
myCircle.??? // here VS does not help me
}
public sealed class Circle
{
//public Circle() { }
private double radius;
public double Calculate(Func<double, double> op)
{
return op(radius);
}
}
为什么我无法实例化它并调用“计算”方法? 请。为初学者解释一下。
答案 0 :(得分:4)
在这堂课中:
public class TopTalInterviewQuestions
{
//write code to calculate the circumference of the circle, without modifying the Circle class itself.
Circle myCircle = new Circle();
myCircle.??? // here VS does not help me
}
当您编写第myCircle.???
行时,您正试图将语句直接放在类声明中。你不能这样做;语句需要进入方法。
尝试这样的事情:
public class TopTalInterviewQuestions
{
//write code to calculate the circumference of the circle, without modifying the Circle class itself.
Circle myCircle = new Circle();
public void MyMethod()
{
myCircle.???
}
}