abstract-class

Can I force subclasses to override a method without making it abstract?

痞子三分冷 提交于 2019-12-18 15:52:17
问题 I have a class with some abstract methods, but I want to be able to edit a subclass of that class in the designer. However, the designer can't edit the subclass unless it can create an instance of the parent class. So my plan is to replace the abstract methods with stubs and mark them as virtual - but then if I make another subclass, I won't get a compile-time error if I forget to implement them. Is there a way to mark the methods so that they have to be implemented by subclasses, without

PHP: Fatal error: Cannot instantiate abstract class

大城市里の小女人 提交于 2019-12-18 15:51:46
问题 I have an abstract database class named as: abstract class database { protected $value; } I created another abstract class abstract class my_database extends database { public function set_value($value) { $this->value = $value; } } When I try to use it: $my_db = new my_database(); I get error: Fatal error: Cannot instantiate abstract class my_database in ... What I try to do is: The abstract class database has a protected $value and I would like to create a wrapper class, to be able to change

PHP: Fatal error: Cannot instantiate abstract class

无人久伴 提交于 2019-12-18 15:51:04
问题 I have an abstract database class named as: abstract class database { protected $value; } I created another abstract class abstract class my_database extends database { public function set_value($value) { $this->value = $value; } } When I try to use it: $my_db = new my_database(); I get error: Fatal error: Cannot instantiate abstract class my_database in ... What I try to do is: The abstract class database has a protected $value and I would like to create a wrapper class, to be able to change

java force an extending class

☆樱花仙子☆ 提交于 2019-12-18 14:04:11
问题 In Java, can I somehow force a class that extends an abstract class to implement its constructor with a Object as a parameter? Something like public abstract class Points { //add some abstract method to force constructor to have object. } public class ExtendPoints extends Points { /** * I want the abstract class to force this implementation to have * a constructor with an object in it? * @param o */ public ExtendPoints(Object o){ } } 回答1: You can use a constructor with a parameter in your

java force an extending class

烈酒焚心 提交于 2019-12-18 14:03:28
问题 In Java, can I somehow force a class that extends an abstract class to implement its constructor with a Object as a parameter? Something like public abstract class Points { //add some abstract method to force constructor to have object. } public class ExtendPoints extends Points { /** * I want the abstract class to force this implementation to have * a constructor with an object in it? * @param o */ public ExtendPoints(Object o){ } } 回答1: You can use a constructor with a parameter in your

Java 8: virtual extension methods vs abstract class

☆樱花仙子☆ 提交于 2019-12-18 13:55:12
问题 I'm looking at the new virtual extension methods in Java 8 interfaces: public interface MyInterface { default String myMethod() { return "myImplementation"; } } I get their purpose in allowing an interface to evolve over time, and the multiple inheritance bit, but they look awfully like an abstract class to me. If you're doing new work are abstract classes prefered over extension methods to provide implementation to an "interface" or are these two approaches conceptually equivalent? 回答1: One

Determine if a Python class is an Abstract Base Class or Concrete

北城以北 提交于 2019-12-18 13:54:22
问题 My Python application contains many abstract classes and implementations. For example: import abc import datetime class MessageDisplay(object): __metaclass__ = abc.ABCMeta @abc.abstractproperty def display(self, message): pass class FriendlyMessageDisplay(MessageDisplay): def greet(self): hour = datetime.datetime.now().timetuple().tm_hour if hour < 7: raise Exception("Cannot greet while asleep.") elif hour < 12: self.display("Good morning!") elif hour < 18: self.display("Good afternoon!")

Is there any way in C# to enforce operator overloading in derived classes?

你离开我真会死。 提交于 2019-12-18 12:50:10
问题 I need to define an Interface which has to enforce certain operator overloading to the types which implements it. There doesn't seem an obvious way to do it since operator overloading has to be done using static methods in class. Is there any way to achieve the same effect (using abstract classes or anything else)? 回答1: Bit of a hack, but... You could provide operator overloads in your base class that then call some published abstract methods in one of the classes to do the job there. public

When to use mixins and when to use interfaces in Dart?

橙三吉。 提交于 2019-12-18 10:39:28
问题 I'm very familiar with the concepts of interfaces and abstract classes, but not super familiar with the concepts of mixins . Right now, in Dart, every class A defines an implicit interface, which can be implemented by another class B by using the implements keyword. There's no explicit way of declaring interfaces as, for example, in Java, where an interface contains only unimplemented methods (and eventually static variables). In Dart, since interfaces are defined by classes, the methods of

How to create abstract properties in python abstract classes

不问归期 提交于 2019-12-18 10:30:53
问题 In the following code, I create a base abstract class Base . I want all the classes that inherit from Base to provide the name property, so I made this property an @abstractmethod . Then I created a subclass of Base , called Base_1 , which is meant to supply some functionality, but still remain abstract. There is no name property in Base_1 , but nevertheless python instatinates an object of that class without an error. How does one create abstract properties? from abc import ABCMeta,