abstract

C#: Creating an instance of an abstract class without defining new class

左心房为你撑大大i 提交于 2019-11-29 01:55:26
问题 I know it can be done in Java, as I have used this technique quite extensively in the past. An example in Java would be shown below. (Additional question. What is this technique called? It's hard to find an example of this without a name.) public abstract class Example { public abstract void doStuff(); } public class StartHere{ public static void main(string[] args){ Example x = new Example(){ public void doStuff(){ System.out.println("Did stuff"); } }; x.doStuff(); } } Now, my main question

Abstract Method in Ruby

浪子不回头ぞ 提交于 2019-11-29 00:54:18
How can I force a subclass to implement a method in Ruby. There doesn't seem to be an abstract keyword in Ruby, which is the approach I would take in Java. Is there another more Ruby-like way to enforce abstract? Andy Abstract methods are supposed to be less useful in Ruby because it's not strongly staticly typed. However, this is what I do: class AbstractThing MESS = "SYSTEM ERROR: method missing" def method_one; raise MESS; end def method_two; raise MESS; end end class ConcreteThing < AbstractThing def method_one puts "hi" end end a = ConcreteThing.new a.method_two # -> raises error. It

Benefits of using an abstract classes vs. regular class

六月ゝ 毕业季﹏ 提交于 2019-11-29 00:47:25
问题 I have decided to start doing small coding projects on my own that focus on code quality instead of code quantity and have a question about the use of abstract classes. Now I know the differences between abstract classes and interfaces with the biggest one (I think) being that interface allow you to only define methods that need to be implemented by classes using the interface and abstract classes allowing you to define both method and members along with default method implementation if you

Why can't you call abstract functions from abstract classes in PHP?

血红的双手。 提交于 2019-11-28 21:04:45
I've set up an abstract parent class, and a concrete class which extends it. Why can the parent class not call the abstract function? //foo.php <?php abstract class AbstractFoo{ abstract public static function foo(); public static function getFoo(){ return self::foo();//line 5 } } class ConcreteFoo extends AbstractFoo{ public static function foo(){ return "bar"; } } echo ConcreteFoo::getFoo(); ?> Error: Fatal error: Cannot call abstract method AbstractFoo::foo() in foo.php on line 5 This is a correct implementation; you should use static, not self, in order to use late static bindings :

Should I add an @Override annotation when implementing abstract methods in Java?

女生的网名这么多〃 提交于 2019-11-28 20:46:51
问题 When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well? 回答1: I tend to prefer the use of @Override in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.). The only real difference is that without the annotation, if the method in the superclass/interface is changed or

Abstract constants in PHP - Force a child class to define a constant

本小妞迷上赌 提交于 2019-11-28 19:34:57
问题 I noticed that you can't have abstract constants in PHP. Is there a way I can force a child class to define a constant (which I need to use in one of the abstract class internal methods) ? 回答1: A constant is a constant ; there is no abstract or private constants in PHP as far as I know, but you can have a work around: Sample Abstract Class abstract class Hello { const CONSTANT_1 = 'abstract'; // Make Abstract const CONSTANT_2 = 'abstract'; // Make Abstract const CONSTANT_3 = 'Hello World'; //

Abstract variables in Java?

孤街浪徒 提交于 2019-11-28 19:06:37
I am coming from c# where this was easy, and possible. I have this code: public abstract class clsAbstractTable { public abstract String TAG; public abstract void init(); } but Eclipse tells me I use illegal modifier. I have this class: public class clsContactGroups extends clsAbstractTable { } I want the variable and method defined in such way, that Eclipse to prompt me , I have unimplemented abstract variables and methods. How do I need to define my abstract class so I should be prompted to implement the abstracts? EDIT 1 I will create different classes for different db tables. Each class

Java: static field in abstract class

时光毁灭记忆、已成空白 提交于 2019-11-28 18:24:16
问题 I just start out with an example, that explains it best: public abstract class A{ static String str; } public class B extends A{ public B(){ str = "123"; } } public class C extends A{ public C(){ str = "abc"; } } public class Main{ public static void main(String[] args){ A b = new B(); A c = new C(); System.out.println("b.str = " + b.str); System.out.println("c.str = " + c.str); } } This will print out: b.str = abc c.str = abc But I would like a solution where each subclass that instantiate

Best practices to test protected methods with PHPUnit (on abstract classes)

痞子三分冷 提交于 2019-11-28 18:01:34
With PHPUnit and PHP >= 5.3 it is possible to test protected methods. The following page at stackoverflow outlined the best practice on it: "Best practices to test protected methods with PHPUnit" protected static function callProtectedMethod($name, $classname, $params) { $class = new ReflectionClass($classname); $method = $class->getMethod($name); $method->setAccessible(true); $obj = new $classname($params); return $method->invokeArgs($obj, $params); } To test public methods on abstract classes is easy with PHPUnit. To test protected methods on normal classes is easy with approach above. To

Defining an abstract class without any abstract methods

狂风中的少年 提交于 2019-11-28 16:08:41
Can I define an abstract class without adding an abstract method? Of course. Declaring a class abstract only means that you don't allow it to be instantiated on its own. Declaring a method abstract means that subclasses have to provide an implementation for that method. The two are separate concepts, though obviously you can't have an abstract method in a non-abstract class. You can even have abstract classes with final methods but never the other way around. Yes you can do it. Why don't you just try doing that? Yes you can. The abstract class used in java signifies that you can't create an