polymorphism

Polymorphic MVC Views

情到浓时终转凉″ 提交于 2019-12-22 06:43:05
问题 So I am making a registration page in ASP.NET MVC and theres a list of fields which changes depending on a number of conditions (taken from a database). What would be considered the best approach for displaying these fields on a view without code changes each time the DB is updated. The current system I am thinking about is making a Dictionary with "Name, DataType" then just iterating through the list and putting a new input type of each name (using a switch for data type to get the correct

Casting in Java(interface and class)

浪子不回头ぞ 提交于 2019-12-22 06:35:21
问题 if: interface I{} class A implements I{} class B extends A{} class C extends B{} A a = new A(); B b = new B(); Why a = (B)(I)b; is correct but b = (B)(I)a; is false? I find casting to be very confusing, what is the best way to understand if I can down cast or up cast an object? 回答1: Your class hierarchy looks like this: Object x can be casted to class Y , if runtime type of x is subclass of Y . Or, in other words, if there is a path from runtime type of x to Y . By "runtime type" i mean the

Basic polymorphic pointers to base classes

孤街醉人 提交于 2019-12-22 06:34:07
问题 While I've been working in c++ for a while, I haven't had need to use polymorphic features until now, and I'm very intrigued by them. If I have a base class ClassA and another ClassB derives from it, I understand that I can have virtual member function in ClassA that, when implemented in ClassB , will be called in a ClassB instance even if that instance is pointed at using a ClassA pointer. Without this virtual keyword, I presume the base class implementation would prevail when using a base

alternative to virtual typedef

你说的曾经没有我的故事 提交于 2019-12-22 05:54:30
问题 I have an interface IEnumerable for template classes of List<>, Array<> and Dictionary<>. I was hoping to use typedef to get their templated types T. I was hoping to do the following. class IEnumerable { public: virtual typedef int TemplateType; } And then override in inherited member, but you cant make a virtual typedef. So is there any other way that i could get the type of an unknown template class (IEnumerable is not template)? 回答1: Well, here is what is discussed in the comments in case

What is the criteria for throwing exceptions in subclass

无人久伴 提交于 2019-12-22 05:30:19
问题 What I have known till now is that a subclass if overriding a superclass method should throw the same exception or a subclass of the exception. For example: This is correct class SuperClass { public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{ String signature = "(String, Integer[])"; System.out.println(str + " " + signature); return 1; } } public final class SubClass extends SuperClass { public int doIt(String str, Integer... data) throws

Low level details of inheritance and polymorphism

*爱你&永不变心* 提交于 2019-12-22 05:15:13
问题 This question is one of the big doubts that looms around my head and is also hard to describe it in terms of words . Some times it seems obvious and sometimes a tough one to crack.So the question goes like this:: class Base{ public: int a_number; Base(){} virtual void function1() {} virtual void function2() {} void function3() {} }; class Derived:public Base{ public: Derived():Base() {} void function1() {cout &lt&lt "Derived from Base" &lt&lt endl; virtual void function4() {cout &lt&lt "Only

Should I use enum to emulate the polymorphism or use trait with Box<trait> instead?

六月ゝ 毕业季﹏ 提交于 2019-12-22 04:47:12
问题 Using enum Axes to confine Coordinate and Quaternion : #[derive(Clone)] pub enum Axes { Coordinate {x: f64, y: f64, z: f64, reserve: Vec<f64>,}, Quaternion {x: f64, y: f64, z: f64}, } impl Axes { pub fn shift(&mut self, Sample: &Axes) -> () { let Dup: Axes = self.clone(); match Dup { Axes::Coordinate {x, y, z, reserve} => { match &Sample { Axes::Coordinate {x, y, z, reserve} => { *self = Axes::Coordinate {x: *x, y: *y, z: *z, reserve: reserve.to_vec()}; } _ => panic!(), } } Axes::Quaternion

Is an object allowed to legally change its type during its lifetime in C++?

Deadly 提交于 2019-12-22 04:10:39
问题 I have this code: class Class { public: virtual void first() {}; virtual void second() {}; }; Class* object = new Class(); object->first(); object->second(); delete object; that I compile with Visual C++ 10 with /O2 and have this disassembly: 282: Class* object = new Class(); 00403953 push 4 00403955 call dword ptr [__imp_operator new (4050BCh)] 0040395B add esp,4 0040395E test eax,eax 00403960 je wmain+1Ch (40396Ch) 00403962 mov dword ptr [eax],offset Class::`vftable' (4056A4h) 00403968 mov

Preserving Polymorphic Types in a WCF Service using JSON

二次信任 提交于 2019-12-22 03:51:13
问题 I have a C# WCF service using a webHttpBinding endpoint that will receive and return data in JSON format. The data to send/receive needs to use a polymorphic type so that data of different types can be exchanged in the same "data packet". I have the following data model: [DataContract] public class DataPacket { [DataMember] public List<DataEvent> DataEvents { get; set; } } [DataContract] [KnownType(typeof(IntEvent))] [KnownType(typeof(BoolEvent))] public class DataEvent { [DataMember] public

“polymorphism” for FOREIGN KEY constraints

妖精的绣舞 提交于 2019-12-22 03:48:09
问题 There is this field in a table: room_id INT NOT NULL CONSTRAINT room_id_ref_room REFERENCES room I have three 2 tables for two kinds of rooms: standard_room and family_room How to do something like this: room_id INT NOT NULL CONSTRAINT room_id_ref_room REFERENCES standard_room or family_room I mean, room_id should reference either standard_room or family_room . Is it possible to do so? 回答1: Here is the pattern I've been using. CREATE TABLE room ( room_id serial primary key, room_type VARCHAR