This question already has an answer here: Difference between shadowing and overriding in C#? 5 answers When you mark a class member as virtual it can be overrided in the sub classes. If you want to change a method (which was declared virtual in base class) in sub class you can use both new and override keywords but there is a difference between them . When using new : if you cast an obj
这个问题在这里已经有了答案: 在C#中阴影和重写之间的区别? 5个答案 当你将一个类成员标记为virtual它可以在子类中被覆盖。 如果您想在子类中更改方法(在基类中声明为虚拟方法),则可以同时使用new关键字和override关键字,但它们之间存在差异 。 使用new时 :如果您将子类的对象作为基类转换,然后调用该方法,则将运行基类实现 。 使用覆盖时 :如果您将子类的对象作为基类转换,然后调用该方法,则子类实现
I have my Beverage class, which has some getters/setters to work with the size of the beverage. This program has to do with the decorator pattern, so I want to combine the behavior of some methods equally named. My intent is to have a method with body that allows me to get the size of the beverage, but then, I want to be able to override that behavior on child classes. In sum, I want a metho
我有我的饮料课,有一些获得者/设置者可以使用饮料的大小。 这个程序与装饰器模式有关,所以我想结合一些同样命名的方法的行为。 我的意图是有一个方法,可以让我得到饮料的大小,但是,我希望能够覆盖子类的行为。 总之,我想要一个方法: 如果没有重写,只是表现为父类中的方法 如果重写,就像编码一样 我所做的是创建一个名为getSizeOfBeverage的方法,其行为与我的“旧”getSize一样,并使getSize“new”方法为抽象的
I am new to TDD and I am using Moq as my mocking framework. I am trying to check if a method has been called in my class. The class is not implementing any Interface. var mockFooSaverService = new Mock<FooSaverService>(); mockFooSaverService.Verify(service => service.Save(mockNewFoo.Object)); to make this work I found here that I have to make the Save() method as a Virtual method.
我是TDD的新手,我使用Moq作为我的嘲笑框架。 我正在尝试检查我的课程中是否有方法。 该类没有实现任何接口。 var mockFooSaverService = new Mock<FooSaverService>(); mockFooSaverService.Verify(service => service.Save(mockNewFoo.Object)); 为了做到这一点,我在这里发现我必须将Save()方法作为Virtual方法。 题: 为了使其可测试而对类中的所有方法使用Virtual关键字有什么后果? TL; DR 根据注
This question already has an answer here: Overriding an abstract method with a virtual one 2 answers 一个覆盖它的基本方法的方法是隐式虚拟的(除非用sealed关键字指定),你不需要指定它。 From MSDN An abstract method is implicitly a virtual method. Abstract method declarations are only permitted in abstract classes. The implementation is provided by an overriding method. Abstract method a
这个问题在这里已经有了答案: 用虚拟一个覆盖抽象方法2个答案 一个覆盖它的基本方法的方法是隐式虚拟的(除非用sealed关键字指定),你不需要指定它。 来自MSDN 抽象方法隐含地是一种虚拟方法。 抽象方法声明只能在抽象类中使用。 该实现是通过重写方法提供的。 抽象方法也可以在抽象类中被覆盖。
In the following example I am using C#. I have created an abstract class Base_Collection , and then I derive other classes from it ( Book , Movie , Game ). However, I don't know how to add new methods that can be used for those new children classes without getting a compiler error. namespace polymorphism_test_01 { abstract class Base_Collection { public int id = 0;
在以下示例中,我使用的是C#。 我创建了一个抽象类Base_Collection ,然后从中派生出其他类( Book , Movie , Game )。 但是,我不知道如何添加新的方法,可以用于这些新的子类而不会出现编译器错误。 namespace polymorphism_test_01 { abstract class Base_Collection { public int id = 0; public string title = ""; public Base_Collection() { } } } namespace polymorp
I'm trying to override an abstract method in an abstract class with a virtual method in a child class. I (assumed until now?) understand the difference between abstract and virtual methods. Obviously I'm not able to do it but my question is... why? Based on the accepted answer here and the following scenario, I just don't see the problem: public abstract class TopLevelParent
我试图用子类中的虚拟方法覆盖抽象类中的抽象方法。 我(假设直到现在?)理解抽象方法和虚拟方法之间的区别。 显然我无法做到,但我的问题是......为什么? 基于这里接受的答案和下面的场景,我只是没有看到问题: public abstract class TopLevelParent { protected abstract void TheAbstractMethod(); } public class FirstLevelChild1 : TopLevelParent { protected override voi
I was looking at Stack Overflow question What is the difference between abstract function and virtual function?, and I was wondering whether every abstract function should be considered to be a virtual function in C# or in general? I was a bit puzzled by the "you must override/you may override" responses to that question. Not being a C# programmer, I tend to think that abstract funct
我在看堆栈溢出问题抽象函数和虚函数有什么区别?我想知道每个抽象函数是否应该被认为是C#中的虚函数还是一般? 我对“你必须忽略/你可能会忽略”对这个问题的回应有些困惑。 不是C#程序员,我倾向于认为抽象函数只是一个编译时概念,而抽象函数是定义的虚函数,因为您必须至少提供一个,但可以在层次结构的更下方提供多个实现。 虚拟函数也有一个编译时间维度,因为你不能覆盖非虚函数,但它们大多是一个运行时概念,因为
c#中虚函数的实际用法是什么? So basically if in your ancestor class you want a certain behaviour for a method. If your descendent uses the same method but has a different implementation you can override it, If it has a virtual keyword. using System; class TestClass { public class Dimensions { public const double pi = Math.PI; protected double x, y; public Dimensions(
c#中虚函数的实际用法是什么? 所以基本上,如果在你的祖先类中你想要某种方法的某种行为。 如果你的后代使用相同的方法,但有不同的实现,你可以覆盖它,如果它有一个虚拟关键字。 using System; class TestClass { public class Dimensions { public const double pi = Math.PI; protected double x, y; public Dimensions() { } public Dimensions (double x, double y)
This question already has an answer here: What is the difference between an abstract function and a virtual function? 21 answers You need to declare the base virtual (which means it can be overridden if necessary) and override it where you need different behavior from the original. The non-overridden derived class will behave just like the base class. public class Parent() { public vir
这个问题在这里已经有了答案: 抽象函数和虚函数之间有什么区别? 21个答案 您需要声明基本virtual (这意味着可以在必要时override它),并在需要与原始行为不同的行为时override它。 未覆盖派生类的行为与基类相同。 public class Parent() { public virtual DataTable FetchScore() { // blahblah } } public class ChildNormal() : Parent { } public class ChildOdd() : Parent { public o
This question already has an answer here: What is the difference between an abstract function and a virtual function? 21 answers is it Virtual method have body and abstract is just a signature ???? Exactly. The point is that virtual methods can be overridden in derived classes, while abstract methods must be overridden. Likewise, a class that has at least one abstract method must itself
这个问题在这里已经有了答案: 抽象函数和虚函数之间有什么区别? 21个答案 是虚拟方法有身体和抽象只是一个签名? 究竟。 关键是虚拟方法可以在派生类中重写,而抽象方法必须重写。 同样,至少有一个抽象方法的类本身必须是抽象的,也就是说,由于其实现(部分)缺失,所以不能直接实例化。 最后,每个抽象方法也都是虚拟的。 virtual基本上只是意味着该方法在运行时被分派到正确的类,因此可以重写实现运行时多态