abstract-class

Spring @ReponseBody @RequestBody with abstract class

穿精又带淫゛_ 提交于 2019-12-17 15:48:10
问题 Suppose I have three classes. public abstract class Animal {} public class Cat extends Animal {} public class Dog extends Animal {} Can i do something like this? Input: a json which it is Dog or Cat Output: a dog/cat depends on input object type I dont understand why the following code doesnt work. Or should I use two separate methods to handle new dog and cat? @RequestMapping(value = "/animal", method = RequestMethod.POST, produces = "application/json; charset=utf-8") private @ResponseBody

How and when to use an abstract class

一曲冷凌霜 提交于 2019-12-17 15:24:17
问题 This is my test program in Java. I want to know how much abstract class is more important here and why we use abstract class for this. Is it a mandatory or is it best method; if so how? class Shape1 { int i = 1; void draw() { System.out.println("this is shape:" + i); } } class Shape2 { int i = 4; void draw() { System.out.println("this is shape2:" + i); } } class Shape { public static void main(String args[]) { Shape1 s1 = new Shape1(); s1.draw(); Shape2 s2 = new Shape2(); s2.draw(); } } 回答1:

returning an abstract class from a function

纵然是瞬间 提交于 2019-12-17 10:58:21
问题 Is it possible to return an abstract class(class itself or a reference, doesn't matter) from a function? 回答1: You can return an abstract class pointer - assuming B is a concrete class derived from abstract class A : A * f() { return new B; } or a reference: A & f() { static B b; return b; } or a smart pointer: std::unique_ptr<A> f() { return std::make_unique<B>(...); } 回答2: You can declare the return type to be a reference or pointer to the abstract class, so that it can be assigned to

pure-specifier on function-definition

我的未来我决定 提交于 2019-12-17 10:53:10
问题 While compiling on GCC I get the error: pure-specifier on function-definition , but not when I compile the same code using VS2005. class Dummy { //error: pure-specifier on function-definition, VS2005 compiles virtual void Process() = 0 {}; }; But when the definition of this pure virtual function is not inline, it works: class Dummy { virtual void Process() = 0; }; void Dummy::Process() {} //compiles on both GCC and VS2005 What does the error means? Why cannot I do it inline? Is it legal to

Using shared_ptr in dll-interfaces

[亡魂溺海] 提交于 2019-12-17 10:37:36
问题 I have an abstract class in my dll. class IBase { protected: virtual ~IBase() = 0; public: virtual void f() = 0; }; I want to get IBase in my exe-file which loads dll. First way is to create following function IBase * CreateInterface(); and to add the virtual function Release() in IBase . Second way is to create another function boost::shared_ptr<IBase> CreateInterface(); and no Release() function is needed. Questions. 1) Is it true that the destructor and memory deallocation is called in the

C#: Abstract classes need to implement interfaces?

前提是你 提交于 2019-12-17 10:35:19
问题 My test code in C#: namespace DSnA { public abstract class Test : IComparable { } } Results in the following compiler error: error CS0535: 'DSnA.Test' does not implement interface member 'System.IComparable.CompareTo(object)' Since the class Test is an abstract class , why does the compiler require it to implement the interface? Shouldn't this requirement only be compulsory for concrete classes? 回答1: In C#, a class that implements an interface is required to define all members of that

Abstract UserControl inheritance in Visual Studio designer

别说谁变了你拦得住时间么 提交于 2019-12-17 09:37:32
问题 abstract class CustomControl : UserControl { protected abstract int DoStuff(); } class DetailControl : CustomControl { protected override int DoStuff() { // do stuff return result; } } I dropped a DetailControl in a form. It renders correctly at runtime, but the designer displays an error and won't open because the base user control is abstract. For the moment, I'm contemplating the following patch, which seems pretty wrong to me, as I want the child classes to be forced to implement the

Spring can you autowire inside an abstract class?

谁说胖子不能爱 提交于 2019-12-17 09:14:37
问题 Spring is failing to autowire my object? Is it possible to autowire an object within an abstract class. Assume all schemas are supplied in application-context.xml Question: What annotation should be on the base and extending classes (if any) @Service @Component? Example abstract class SuperMan { @Autowire private DatabaseService databaseService; abstract void Fly(); protected void doSuperPowerAction(Thing thing) { //busy code databaseService.save(thing); } } Extending class public class

How to create an instance of anonymous class of abstract class in Kotlin?

南楼画角 提交于 2019-12-17 08:55:09
问题 Assume that KeyAdapter is an abstract class with several methods that can be overridden. In java I can do: KeyListener keyListener = new KeyAdapter() { @Override public void keyPressed(KeyEvent keyEvent) { // ... } }; How to do the same in Kotlin? 回答1: From the official Kotlin language documentation and one of the first hits on Google: window.addMouseListener(object : MouseAdapter() { override fun mouseClicked(e : MouseEvent) { // ... } Applied to your problem at hand: val keyListener =

Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods?

无人久伴 提交于 2019-12-17 05:16:18
问题 A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain. For example, given the interface: public interface IAnything { void m1(); void m2(); void m3(); } the following abstract class gets merrily compiled without a warning or an error: public abstract class AbstractThing implements