abstract

How can abstract classes have references but not objects?

喜夏-厌秋 提交于 2019-12-03 12:02:47
Note that you cannot construct an object of an abstract class, but you can still have an object reference whose type is an abstract class. Of course, the actual object to which it refers must be an instance of a concrete subclass: Account anAccount; // OK anAccount = new Account(); // Error—Account is abstract anAccount = new SavingsAccount(); // OK anAccount = null; // OK Not understanding why you can have an object reference to an abstract class... When you have an object reference whose type is an abstract class, that reference doesn't mean "I'm referencing an abstract class." Instead, it

DataContract Serialize abstract class

南楼画角 提交于 2019-12-03 07:44:40
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes inherited from ServiceInfo, like CoreServiceInfo, ModuleServiceInfo etc. There is a service contract named RootService which returns IServiceInfo. public IServiceInfo GetServiceInfo() { return (IServiceInfo)new CoreServiceInfo(); } I am having problem serializing. I can use ServiceKnownType to identify base class, and KnownType to identify child class. Problem is I do not know all the ServiceInfo child, since application can have plugins with different child inherited from ServiceInfo, so I cannot

Should an abstract class have at least one abstract method?

二次信任 提交于 2019-12-03 06:59:33
问题 Is it necessary for an abstract class to have at least one abstract method? 回答1: The subject of this post and the body ask two different questions: Should it have at least one abstract member? Is it necessary to have at least one abstract member? The answer to #2 is definitively no. The answer to #1 is subjective and a matter of style. Personally I would say yes. If your intent is to prevent a class (with no abstract methods) from being instantiated, the best way to handle this is with a

Django: Best way to unit-test an abstract model

≡放荡痞女 提交于 2019-12-03 05:45:41
问题 I need to write some unit tests for an abstract base model, that provides some basic functionality that should be used by other apps. It it would be necessary to define a model that inherits from it just for testing purposes; are there any elegant/simple ways to define that model just for testing ? I have seen some "hacks" that make this possible, but never seen an "official" way in the django documentation or in other similar places. 回答1: Just stumbled across this feature myself: You can

How can I implement abstract static methods in Java?

我们两清 提交于 2019-12-03 05:44:19
There are numerous questions about the impossibility of including static abstract Java methods. There are also quite a lot about workarounds for this (design flaw/design strength). But I can't find any for the specific problem I'm going to state shortly. It seems to me that the people who made Java, and quite a lot of the people who use it, don't think of static methods the way I, and many others, do - as class functions, or methods that belong to the class and not to any object. So is there some other way of implementing a class function? Here is my example: in mathematics, a group is a set

No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation

混江龙づ霸主 提交于 2019-12-03 05:38:10
Consider the below code: class abstract Normal1 extends Something { } class Outer { class abstract Inner extends Normal1 { } } class General extends Outer.Inner // Problem occurs at this { } The error I am getting is "No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation" My question is can I extend the inner class like above ? Jordão Declare the inner class as static and you should be able to extend it: class outer { static abstract class inner extends normal1 { } } If inner is not abstract, it's tied to outer, and can only exist when an

Java final abstract class

只谈情不闲聊 提交于 2019-12-03 05:31:49
问题 I have a quite simple question: I want to have a Java Class, which provides one public static method, which does something. This is just for encapsulating purposes (to have everything important within one separate class)... This class should neither be instantiated, nor being extended. That made me write: final abstract class MyClass { static void myMethod() { ... } ... // More private methods and fields... } (though I knew, it is forbidden). I also know, that I can make this class solely

How to get the name of the calling class (in PHP)

拥有回忆 提交于 2019-12-03 04:49:18
define('anActionType', 1); $actionTypes = array(anActionType => 'anActionType'); class core { public $callbacks = array(); public $plugins = array(); public function __construct() { $this->plugins[] = new admin(); $this->plugins[] = new client(); } } abstract class plugin { public function registerCallback($callbackMethod, $onAction) { if (!isset($this->callbacks[$onAction])) $this->callbacks[$onAction] = array(); global $actionTypes; echo "Calling $callbackMethod in $callbacksClass because we got {$actionTypes[$onAction]}" . PHP_EOL; // How do I get $callbacksClass? $this->callbacks[$onAction

Static classes in PHP via abstract keyword?

若如初见. 提交于 2019-12-03 04:18:53
问题 According to the PHP manual, a class like this: abstract class Example {} cannot be instantiated. If I need a class without instance, e.g. for a registry pattern: class Registry {} // and later: echo Registry::$someValue; would it be considered good style to simply declare the class as abstract? If not, what are the advantages of hiding the constructor as protected method compared to an abstract class? Rationale for asking: As far as I see it, it could a bit of feature abuse, since the manual

Constructor injection into a base class using autofac

喜欢而已 提交于 2019-12-03 04:14:34
问题 I have an abstract base controller which has a constructor I hoped would be populated by autofac when the controllers were built. public abstract class BaseController : Controller { protected ILogger { get; private set; } protected BaseController() { } protected BaseController(ILogger logger) { Logger = logger; } } This doesnt seem to work when I derive a controller from it. I can only get this to work when I explicitly call the constructor explicitly from the controller. Is this the correct