开发者

Calling overridden parent method from parent method

开发者 https://www.devze.com 2022-12-28 01:11 出处:网络
Heres the situation. Component B extends component A and overrides the init method to accept a different parameter. A also has a create method that calls init.

Heres the situation. Component B extends component A and overrides the init method to accept a different parameter. A also has a create method that calls init.

If i have an instance of B and i call create, its calling the wrong init - it calls init in B, where i need it to call init in A.

I dont want to call super.init() 开发者_开发知识库as there may not always be a super. Is there any way to specify to call the init in the parent component?


Refactor the actual code you want to execute into their own methods, and have the Init methods call that code, and have the create method call that code as well.

It sounds like your objects are not well designed at the moment. Try to break your methods down to smaller, more constrained units.

Edit:

I think my answer still stands.

If the Init method in the parent component does some stuff, move that stuff to a new method, say, "initDoStuff(), and have the init method call that method.

Then, have your create method call the initDoStuff() method instead of init.

ColdFusion is a dynamically typed language, and you don't need to override methods just to accept different parameters. You can do that in other ways.

CF isn't able to pick methods based on argument signatures. So, if you have a situation like this, you need to handle it in a different way.

Basically, the idea of overriding a method to change its argument types is not really valid in ColdFuion.

Component A:

<cffunction name="init" access="public" output="false">
  <cfargument name=... ...>
  <cfreturn initDoSomething(argumentCollection=arguments)>
</cffunction>

<cffunction name="initDoSomething" access="package" output="false">
  {do stuff}
  <cfreturn {whatever}>
</cffunction>

Component B:

<cffunction name="create" access="package" output="false">
  <cfset {something} = initDoSomething({whatever arguments})>
  <cfreturn {whatever}>
</cffunction>


call the base initializer I don't know what language you're running but in C# it's for example B b = new B() : base(parameters...) of course you'd still have to check wether the component is in fact of type A or B but you can do that in most languages.

This will also run the extened initializer, but thats the way it is. If you want to get rid of the extended objects initializer you'll just have to create a parent type object I think. It's the only thing I can think of.

But of course it would help if you specified what language you are programming in, in order to know the options and syntax.


I solved this by extending the create method to call super.create and super.init. theres still a call to the child init that fails, but overall it works.

I would still prefer a solution that works on the parent init without having to override the create.

0

精彩评论

暂无评论...
验证码 换一张
取 消