abstract

Java- How to use Abstract class

允我心安 提交于 2019-12-11 02:06:02
问题 So I was studying Abstract class in java, and when I was reading other people's codes I saw the following: public abstract class Message implements Serializable, Comparable<Message> { //stuff in this class } In another class under the same project, the programmer declared a method as follows: public void notifyMessage(Message msg, HostType sourceType) { //some stuff in this method } Notice in the notifyMessage declaration, variable msg is of type "Message". I thought all abstract classes

java - Abstract base enum/class for singleton

谁说胖子不能爱 提交于 2019-12-11 01:27:32
问题 I've created two enum classes as singleton: public enum A { INSTANCE; public void init(param p1, param p2) { } public void connect() { } public void disconnect() { } public bool isConnected() { } } public enum B { INSTANCE; public void init(param p1) { } public void connect() { } public void disconnect() { } public bool isConnected() { } } As you can see both enum classes are very similar so I was wondering if I should create some kind of base abstract class/enum or interface and then have

Implementing an abstract function with access types in Ada

邮差的信 提交于 2019-12-10 23:59:05
问题 I have a package called Statements with an abstract type called Statement and an abstract function called execute(). In another package I have a type CompoundStatement which is a type Statement and it implements the execute() function. I have a function called createStatement(). It's purpose is to evaluate a token of type Unbounded_String and determine what keyword it contains. Then based on this keyword it will generate an access type based on this keyword. So far so good. But what I can't

Force implementation of a method in all inheriting classes

佐手、 提交于 2019-12-10 21:15:05
问题 I have a situation in which I want to enforce each and every class inheriting from a certain (abstract) class to implement a method. This is something I would normally achieve using @abstractmethod. However, considering this situation of multiple inheritance: from abc import ABCMeta, abstractmethod class A(object): __metaclass__ = ABCMeta @abstractmethod def very_specific_method(self): pass class B(A): def very_specific_method(self): print 'doing something in B' class C(B): pass I want to

Builder (Joshua Bloch-style) for concrete implementation of abstract class?

醉酒当歌 提交于 2019-12-10 19:07:36
问题 Let's say I have an abstract class (BaseThing). It has one required parameter ("base required") and one optional parameter ("base optional"). I have a concrete class that extends it (Thing). It also has one required parameter ("required") and one optional parameter ("optional"). So something like: public abstract class BaseThing { public static final String DEFAULT_BASE_OPTIONAL = "Default Base Optional"; private final String baseRequired; private String baseOptional = DEFAULT_BASE_OPTIONAL;

Abstract Methods in Number Class

為{幸葍}努か 提交于 2019-12-10 18:49:47
问题 Why is it that the Number Class provides abstract methods for conversion methods for Double, Int, Long, and Float but not abstract methods for byte and short? Overall I am slightly confused on when to use Abstract methods, as I just began learning Java. Thanks for any insight anyone can offer. 回答1: One look at the source for them says why: public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); } They both rely on the fact that intValue()

Liskov substitution principle, preconditions and abstract methods

眉间皱痕 提交于 2019-12-10 18:05:24
问题 Liskov substitution principle (LSP) says: Preconditions cannot be strengthened in a subtype. In C#, I could violate the whole principle as follows: public class A { public virtual void DoStuff(string text) { Contract.Requires(!string.IsNullOrEmpty(text)); } } public class B : A { public override void DoStuff(string text) { Contract.Requires(!string.IsNullOrEmpty(text) && text.Length > 10); } } But, what would happen if A.DoStuff would be an abstract method: public class A { public abstract

Visual Studio: Design a UserControl class that derives from an abstract base class

谁说我不能喝 提交于 2019-12-10 17:57:09
问题 I want to have an abstract base class for some of my custom UserControl 's. The reason is obvious: they share some common properties and methods (a basic implementation of some elements of an interface actually), and I want to implement them only once. I have done this by defining my abstract base class: public abstract class ViewBase : UserControl, ISomeInterface Then I went to implement one of my views, as usual, with the designer: public partial class SpecialView : UserControl //all OK Up

Delphi 6: Force compiler error on missing abstract class methods?

折月煮酒 提交于 2019-12-10 17:14:21
问题 I'm using Delphi Pro 6. Right now, the only way to know if a class is missing a base class abstract method is to wait for the IDE to emit a "constructing instance of {derived class} containing abstract method {base class.abstract method name}" warning or to wait for a runtime Abstract Error method when an attempt to call the missing method is made. The former isn't sufficient since it only finds warnings for those derived classes actually constructed in the current project. The latter is just

Alternative to overiding static in java

你。 提交于 2019-12-10 16:15:13
问题 I was wondering: what would be the best way to fit an attribute to a class which is part of a large inheritance structure. I wanted to make an abstract static method which each class would override but after a quick google that doesn't seem to work. Any suggestions? I could make it an instance method but it really is a class level specification. Thanks in advance. 回答1: I would suggest you create an abstract method, just as you have thought of, and let this method be implemented in terms of