inheritance

twig - pass function into template

霸气de小男生 提交于 2019-12-31 17:56:19
问题 Currently I place my function in a class and pass an instance of this class into template and call my required function as a class method. {{ unneededclass.blah() }} I need to do like below {{ blah() }} Is it possible? 回答1: Update 5/14/2015 Commenters point out that I'm mostly wrong. If you really need a function, and not a filter or macro, you can do it as suggested in the Twig docs: $twig = new Twig_Environment($loader); $function = new Twig_SimpleFunction('blah', function () { // ... });

twig - pass function into template

巧了我就是萌 提交于 2019-12-31 17:56:04
问题 Currently I place my function in a class and pass an instance of this class into template and call my required function as a class method. {{ unneededclass.blah() }} I need to do like below {{ blah() }} Is it possible? 回答1: Update 5/14/2015 Commenters point out that I'm mostly wrong. If you really need a function, and not a filter or macro, you can do it as suggested in the Twig docs: $twig = new Twig_Environment($loader); $function = new Twig_SimpleFunction('blah', function () { // ... });

.NET XML Serialization and inheritance

痞子三分冷 提交于 2019-12-31 17:14:34
问题 I have structure like this: public interface A { public void method(); } public class B : A { } public class C : A { } List<A> list; List contains objects of type B and C they also have some fields that I would like to keep, can I now serialize it, deserialize back and get the proper object instances? Preferably to XML EDIT: Is there any simple way to serialize this list that contains interfaces, and then deserialize it back to B and C instances? 回答1: You may try using DataContractSerializer:

Override a Property with a Derived Type and Same Name C#

╄→гoц情女王★ 提交于 2019-12-31 14:19:02
问题 I'm trying to override a property in a base class with a different, but derived type with the same name. I think its possible by covarience or generics but am not sure how to do it? The following code gets the error: Error 1 'Sun.Cache': type must be 'OuterSpace.Cache' to match overridden member 'OuterSpace.Cache' public class OuterSpace { public virtual OuterSpaceData Data {get; set;} public virtual OuterSpaceAnalysis Analysis {get; set;} public virtual OuterSpaceCache Cache {get; set;}

Override a Property with a Derived Type and Same Name C#

感情迁移 提交于 2019-12-31 14:16:06
问题 I'm trying to override a property in a base class with a different, but derived type with the same name. I think its possible by covarience or generics but am not sure how to do it? The following code gets the error: Error 1 'Sun.Cache': type must be 'OuterSpace.Cache' to match overridden member 'OuterSpace.Cache' public class OuterSpace { public virtual OuterSpaceData Data {get; set;} public virtual OuterSpaceAnalysis Analysis {get; set;} public virtual OuterSpaceCache Cache {get; set;}

Understanding Fortran extends types and override

不问归期 提交于 2019-12-31 12:59:20
问题 I am trying to understand the object-oriented concepts in Fortran 2003 standards (or later). I have some knowledge in C++ so I think there are some common ideas between these two languages which can help me understand them better. In C++, the polymorphism is done through class derivation and member function overriding. One defines a "abstract" base class where almost all the virtual functions are defined. Different derived classes contains the actual implementation of them. So other functions

Understanding Fortran extends types and override

邮差的信 提交于 2019-12-31 12:58:18
问题 I am trying to understand the object-oriented concepts in Fortran 2003 standards (or later). I have some knowledge in C++ so I think there are some common ideas between these two languages which can help me understand them better. In C++, the polymorphism is done through class derivation and member function overriding. One defines a "abstract" base class where almost all the virtual functions are defined. Different derived classes contains the actual implementation of them. So other functions

Should this be called some special case of object slicing?

折月煮酒 提交于 2019-12-31 11:40:58
问题 Let's say I have a class Derived which derives from class Base whereas sizeof(Derived) > sizeof(Base) . Now, if one allocates an array of Derived like this: Base * myArray = new Derived[42]; and then attempts to access the n -th object using doSomethingWithBase(myArray[n]); Then this is might likely (but not always) cause undefined behaviour due to accessing Base from an invalid location. What is the correct term for such an programming error? Should it be considered a case of object slicing?

Cannot convert from List<Bar> to List<Foo>

非 Y 不嫁゛ 提交于 2019-12-31 09:57:24
问题 I have a set up like this: abstract class Foo {} class Bar : Foo {} and a method elsewhere of this form: void AddEntries(List<Foo>) {} I am trying to call this method using a list of objects of type Bar List<Bar> barList = new List<Bar>() AddEntries(barList); but this gives me the error: cannot convert from List<Bar> to List<Foo> Is there anyway around this problem? I Need to keep the method definition using the abstract class. 回答1: You could make your AddEntries generic and change it to this

Cannot convert from List<Bar> to List<Foo>

无人久伴 提交于 2019-12-31 09:57:05
问题 I have a set up like this: abstract class Foo {} class Bar : Foo {} and a method elsewhere of this form: void AddEntries(List<Foo>) {} I am trying to call this method using a list of objects of type Bar List<Bar> barList = new List<Bar>() AddEntries(barList); but this gives me the error: cannot convert from List<Bar> to List<Foo> Is there anyway around this problem? I Need to keep the method definition using the abstract class. 回答1: You could make your AddEntries generic and change it to this