abstract

Swift - class method which must be overridden by subclass

我只是一个虾纸丫 提交于 2020-01-19 04:38:16
问题 Is there a standard way to make a "pure virtual function" in Swift, ie. one that must be overridden by every subclass, and which, if it is not, causes a compile time error? 回答1: You have two options: 1. Use a Protocol Define the superclass as a Protocol instead of a Class Pro : Compile time check for if each "subclass" (not an actual subclass) implements the required method(s) Con : The "superclass" (protocol) cannot implement methods or properties 2. Assert in the super version of the method

Why can't I have an private abstract method?

倖福魔咒の 提交于 2020-01-16 04:49:20
问题 Let's say I have my classic: public abstract class Mammal { private int numLegs; private String voice; private Coat coat; public abstract void eat(); public abstract void speak(); public abstract void sleep(); private abstract void ingestFood(Food f); //The abstract method ingestFood in type Mammal can only set a visibility modifier, one of public or protected } With these concrete implementations: public class Dog extends Mammal { private int numLegs = 4; private String voice = "Woof!";

Why does C# allow for an abstract class with no abstract members?

守給你的承諾、 提交于 2020-01-12 04:19:25
问题 The C# spec, section 10.1.1.1, states: An abstract class is permitted (but not required) to contain abstract members. This allows me to create classes like this: public abstract class A { public void Main() { // it's full of logic! } } Or even better: public abstract class A { public virtual void Main() { } } public abstract class B : A { public override sealed void Main() { // it's full of logic! } } This is really a concrete class; it's only abstract in so far as one can't instantiate it.

protected virtual methods in f#

余生颓废 提交于 2020-01-05 01:11:08
问题 F# does not support the definition of protected methods. Here it is explained why F# replaces virtual methods with abstract methods defined in abstract classes (see here). I was wondering if there is a way to prevent access to abstract methods from outside the derived classes at all. 回答1: Like Patryk Ćwiek, I also don't think it's possible, but here's one alternative: From Design Patterns we know that we should favour Composition over Inheritance . In my experience, everything you can do with

Force SubClasses to @Override method from SuperClass. Method in SuperClass must have body

核能气质少年 提交于 2020-01-03 20:35:39
问题 I would like to force SubClasses to @Override method from SuperClass . Method in SuperClass can't be abstract , cause I wanna provide some basic implementation. Here is example of my code: public abstract class GenericModel<T extends GenericModel> { long id, counter; public String methodToBeOverridenInSubClass(String name) { // some basic implementation // rest details will be provided by subclass return name; } } public class SubClass extends GenericModel<SubClass> { @Override public String

How to use generics and inherit from parent class without causing name clash?

╄→гoц情女王★ 提交于 2020-01-02 23:13:42
问题 I have a parent class in Java called Flight . I have children classes: JetFlight , NormalFlight , etc. which inherit from Flight . I want all the children classes to implement compareTo from the Comparable interface. I want them to inherit from Flight because I want to use polymorphism (for example, initiate a Flight array and fill it with objects of JetFlight , NormalFlight , etc.). This is my code for the parent class: public abstract class Flight { public abstract int compareTo(Object o);

How to use generics and inherit from parent class without causing name clash?

别说谁变了你拦得住时间么 提交于 2020-01-02 23:13:33
问题 I have a parent class in Java called Flight . I have children classes: JetFlight , NormalFlight , etc. which inherit from Flight . I want all the children classes to implement compareTo from the Comparable interface. I want them to inherit from Flight because I want to use polymorphism (for example, initiate a Flight array and fill it with objects of JetFlight , NormalFlight , etc.). This is my code for the parent class: public abstract class Flight { public abstract int compareTo(Object o);

What is the purpose of an abstract method?

一曲冷凌霜 提交于 2020-01-02 09:29:56
问题 abstract public class car { abstract void drive(); } As in the code snippet above, what exactly is the purpose of an abstract method in Java? From what I can gather, by definition, they aren't allowed to have bodies. 回答1: When you make things implement the same abstract class, you are saying, these things are alike at a high level, but they vary in the particulars of how they do things. An abstract method is how you say, "Here's something that all the things that extend this class have to do,

What is the purpose of an abstract method?

时间秒杀一切 提交于 2020-01-02 09:29:11
问题 abstract public class car { abstract void drive(); } As in the code snippet above, what exactly is the purpose of an abstract method in Java? From what I can gather, by definition, they aren't allowed to have bodies. 回答1: When you make things implement the same abstract class, you are saying, these things are alike at a high level, but they vary in the particulars of how they do things. An abstract method is how you say, "Here's something that all the things that extend this class have to do,

boost serialize polymorphic class

£可爱£侵袭症+ 提交于 2020-01-02 09:13:17
问题 With the following example I attempting to learn a few new to me concepts. abstraction polymorphic classes factory programming. boost serialization The nuances of how pointers behave are still something I am working to figure out. Here is a small program that I have written to show you the issue I am struggling to understand. When I unserialize the polymorphic object below I only get an object created from the default constructor. TodoFactory::retrieveATodo is not recreating the object from