overriding

How to enforce a method call (in the base class) when overriding method is invoked?

末鹿安然 提交于 2019-12-01 17:42:12
I have this situation that when AbstractMethod method is invoked from ImplementClass I want to enforce that MustBeCalled method in the AbstractClass is invoked. I’ve never come across this situation before. Thank you! public abstract class AbstractClass { public abstract void AbstractMethod(); public void MustBeCalled() { //this must be called when AbstractMethod is invoked } } public class ImplementClass : AbstractClass { public override void AbstractMethod() { //when called, base.MustBeCalled() must be called. //how can i enforce this? } } An option would be to have the Abstract class do the

Overriding a synchronized method

て烟熏妆下的殇ゞ 提交于 2019-12-01 17:21:25
问题 What happens when a method in super class is synchronized, but you override the method in a subclass and don't synchronize it ? 回答1: If a method in super class is synchronized, but you override the method in a subclass and don't synchronize it, then the method is no longer synchronized if called on the subclass . 来源: https://stackoverflow.com/questions/10173345/overriding-a-synchronized-method

Invoking a method via reflection with generics and overrides

风流意气都作罢 提交于 2019-12-01 17:12:49
I'm trying to invoke the RegisterType method in the Unity container. RegisterType has a total of 16 overrides (some of those are parameters some are types). I'm trying to perform the equivalent of: Container.RegisterType<IMyDataProvider, MockData.MockProvider>("MockData", new ContainerControlledLifetimeManager()) Using GetMethod() was a total failure, so I ended up doing this ugly thing: MethodInfo registerTypeGeneric = Container.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance). Where(p => p.ToString() == "Microsoft.Practices.Unity.IUnityContainer RegisterType[TFrom,TTo]

Override a virtual method in a partial class

孤人 提交于 2019-12-01 16:57:51
问题 I am currently working with the nopCommerce source code and trying my best to avoid editing the source at all, but instead using partial classes and plugins that are separate from the source code, should we ever need to upgrade versions. I want to make some changes to the code that places an order, by using a partial class in the same assembly: Orignal Source Code: namespace Nop.Services.Orders { public partial class OrderProcessingService : IOrderProcessingService { public virtual

Overriding ApplicationRecord initialize, bad idea?

末鹿安然 提交于 2019-12-01 16:47:57
问题 I am creating a foo obeject like this: @foo = Foo.new(foo_params) @foo.bar = Bar.where(name: "baz").first_or_create But there are other objects that I will need to do this as well. So, I thought of overriding the Foo initialize method to do something like this: class Foo < ApplicationRecord def initialize(*args, BarName) @foo = super @foo.bar = Bar.where(name: BarName).first_or_create end end and call it like this: @foo = Foo.new(foo_params, "baz") But Foo is an ApplicationRecord and it seems

Is it recommended to explicitly make overriding functions virtual?

谁说胖子不能爱 提交于 2019-12-01 16:46:00
In times before C++11 when a virtual function was overriden in a derived class, it was recommended to add the virtual keyword also to the derived class function to make the intention clear. Nowadays such a function is marked "override" which kind of includes the notion that there must be a virtual base function. Therefore I am now preferring to omit the virtual: class Derived: public Base { public: void Overriden() override; // Instead of: virtual void Overriden() override; }; However this leads to an IntelliSense error in MSVC 2012: the 'override' modifier requires a virtual function

When we override the toString() method we should always return a string representation of the object?

帅比萌擦擦* 提交于 2019-12-01 16:26:54
In practice, the toString method should return a String representation of the object. In one project, I found some classes that override the toString method allowing return null. Like: @Override public String toString() { ....... return null; } For me, this practice is a violation to the principal propose of toString method that should return a String representation of the object. The API documentation for Object.toString() says: public String toString() Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The

In java, Can we override a method by passing subclass of the parameter used in super class method?

99封情书 提交于 2019-12-01 16:17:12
As per the rule, while overriding a method in subclass, parameters cannot be changed and have to be the same as in the super class. What if we pass subclass of parameter while overriding method ? Will it be called as overloading or overriding? Based on my query I have written some code below. I was expecting the output as "Dog eats Flesh Food" but to my surprise the output is "Animal eats Flesh Food" Will appreciate if someone can explain how does Animal method gets called when the object assigned is of type Dog ? class Food { public String toString(){ return "Normal Food"; } } class Flesh

C#: Any way to skip over one of the base calls in polymorphism?

我的未来我决定 提交于 2019-12-01 16:00:14
class GrandParent { public virtual void Foo() { ... } } class Parent : GrandParent { public override void Foo() { base.Foo(); //Do additional work } } class Child : Parent { public override void Foo() { //How to skip Parent.Foo and just get to the GrandParent.Foo base? //Do additional work } } As the code above shows, how can I have the Child.Foo() make a call into GrandParent.Foo() instead of going into Parent.Foo()? base.Foo() takes me to the Parent class first. Your design is wrong if you need this. Instead, put the per-class logic in DoFoo and don't call base.DoFoo when you don't need to.

Is it possible to override a method at runtime?

南楼画角 提交于 2019-12-01 15:55:31
Is there anyway to override a method at run time? Even if it requires dynamically creating a subclass from that instance? With plain Java, no. With ByteBuddy (preferred), asm , cglib or aspectj , yes. In plain Java, the thing to do in a situation like that is to create an interface-based proxy that handles the method invocation and delegates to the original object (or not). You could create an anonymous class that overrides the method and uses the strategy pattern to decide what to do. If you are looking for dynamic compilation from code, you can follow these instructions I think it not