polymorphism

What is the “a” in “List a” in the length example?

佐手、 提交于 2019-12-12 13:12:20
问题 I'm wondering where I can find information about the " a " used in the Length example. It seems to be a type of some kind? 回答1: [1,2,3] is a List Int , functions that could only work with a Lists of Ints would have to have List Int in their type signature. ["a", "b"] is a List String , functions that could only work with a Lists of Strings would have to have List String in their type signature. A function that works with a list of any type (for example List.length ) can have a generic type

How to pass a complex view model into a controller action via ajax call with JSON in .Net MVC4?

[亡魂溺海] 提交于 2019-12-12 13:05:44
问题 So I scoured Stack Overflow as much as I possibly could and couldn't find an answer to this specific issue. Apologies if this has already been asked. I've found answers to: how to pass an object/class to an action how to pass an object via the query string to an action how to pass an object via json to an action how to pass a polymorphic object to an action and have a custom model binder process it Suppose you have the following code, how can you combine the above techniques into one solution

Calling child methods while using polymorphism in an arraylist

白昼怎懂夜的黑 提交于 2019-12-12 12:42:10
问题 I have a small project that I'm working with inheritance and polymorphism. I have an ArrayList of type Employee that contains both Hourly and Salary employee objects. I would like to be able to use a for loop to call a calcPay function in the Hourly class provided the object in the ArrayList of type Employee is an Hourly employee. The line System.out.println("Wage: " e.calcPay()); Gives the error The method calcPay() is undefined for type employee . How do you downcast the object? I've looked

Call base method instead of override

感情迁移 提交于 2019-12-12 11:11:27
问题 In C#, class A contains a public method Foo() which does some processing and returns a value. protected method Bar() , also in class A performs the same logic as Foo() , followed by some additional processing, and then returns a value. In order to avoid duplicating code, Bar() calls Foo() and uses the return as an intermediate value. class A { public virtual String Foo() { String computedValue; // Compute the value. return computedValue; } protected String Bar() { String computedValue; String

Overriding a templated function with a polymorphic one

假如想象 提交于 2019-12-12 10:57:39
问题 If I have template<class T> TalkyBuffer& operator<<(T const &object) { // Template ... } TalkyBuffer& operator<<(TalkySerialisable const &object); // Override and a class class A : public TalkySerialisable { ...} Then if I perform TalkyBuffer b; A test; b << test; Then gcc is calling the Template function rather than the Override function However if I specifically define an override TalkyBuffer& operator<<(A const &object); // Override without polymorphism Then gcc picks that one. Is there a

In Java, can I consolidate two similar functions where uses JspWriter and the other PrintWriter?

走远了吗. 提交于 2019-12-12 10:47:48
问题 I have the following class, which as you will see has rather a rather redundant formatNameAndAddress method: package hu.flux.helper; import java.io.PrintWriter; import javax.servlet.jsp.JspWriter; // A holder for formatting data public class NameAndAddress { public String firstName; public String middleName; public String lastName; public String address1; public String address2; public String city; public String state; public String zip; // Print out the name and address. public void

Heterogeneous map

你。 提交于 2019-12-12 10:38:43
问题 I need a map which can contain arbitrary values as long as their types are of the same typeclass. My first naive approach was something like this: type HMap = forall a . MyClass a => M.Map Int a but it does not seem to work: the following code gives a compile error: testFunction :: (forall a . MyClass a => M.Map Int a) -> Int -> IO () testFunction m i = do case M.lookup i m of Nothing -> return () Just v -> someActionFromMyClass v >> putStrLn "OK" Ambiguous type variable `a0' in the

Polymorphism with Scala type classes

淺唱寂寞╮ 提交于 2019-12-12 10:37:10
问题 We are refactoring an inherited method to use a type class instead - we would like to concentrate all of the method implementations in one place, because having them scattered among the implementing classes is making maintenance difficult. However, we're running into some trouble as we're fairly new to type classes. At present method is defined as trait MethodTrait { def method: Map[String, Any] = // default implementation } abstract class SuperClass extends MethodTrait { override def method

deep copy and dynamic cast unique_ptr

主宰稳场 提交于 2019-12-12 10:25:00
问题 Suppose I have a class like the following: class A { virtual ~A(); ... } class B : public A { ... } class C : public A { ... } I also have a vector of unique_ptr which is declared this way: std::vector<std::unique_ptr<A>> vec; Assume vec is populated with unique_ptr to objects of derived class. What should I do if I want a deep copy of any of the vector elements, either b or c, and let a base class unique_ptr pointing to it? Originally I was doing things like std::unique_ptr<A> tmp = std:

why the polymorphic types error and cleanup question?

喜欢而已 提交于 2019-12-12 10:21:17
问题 #include <iostream> #include <string> #include <map> #include <vector> class base {}; class derived1 : public base { public: unsigned short n; derived1() { n = 2; } }; class derived2 : public base {}; void main() { // way 1 { std::vector<derived1> a1; std::vector<derived2> a2; std::map<std::string, base*> b; a1.push_back(derived1()); b["abc"] = &a1.at(0); std::cout<<(dynamic_cast<derived1*>(b.find("abc")->second))->n<<std::endl; } // way 2 { std::map<std::string, base*> b; b["abc"] = new