polymorphism

How to ignore/bypass an overridden method?

眉间皱痕 提交于 2020-08-25 07:31:49
问题 I'm fairly new to Java and would like to know how to accomplish the following task, and also, whether it is considered bad style, even if it is possible. Thank you. Fish f; // Fish is a superclass, Tuna t = new Tuna(); // to which Tuna is a subclass. f=t; // the Fish object "f" now refers to the Tuna object "t". // Both Fish and Tuna have an identical method (signature wise) called swim() , f.swim(); // and so Tuna's overridden swim() method is invoked here. But can I now get Fish's swim()

Serialize/Deserialize a class hierarchy with .NET Core System.Text.Json

大兔子大兔子 提交于 2020-07-30 02:56:31
问题 I have a simple class hierarchy that I want to serialize using System.Text.Json. There are 3 classes. The base is Shape . Inherited ones are Box and Circle . I have a plan to use these classes as a tagged union on my frontend app so I just introduced a discriminator property Tag . I wrote a type convertor that supports serialization/deserialization of this hierarchy. What I'm trying to understand - is this a best approach to implement such functionality or not. Indeed the output result of

Why doesn't Scala's implicit class work when one of the type parameters should be Nothing?

蹲街弑〆低调 提交于 2020-07-28 13:54:48
问题 Update: I modified the example so that can be compiled and tested. I have an implicit class that defines an enrichment method: case class Pipe[-I,+O,+R](f: I => (O, R)); object Pipe { // The problematic implicit class: implicit class PipeEnrich[I,O,R](val pipe: Pipe[I,O,R]) extends AnyVal { def >->[X](that: Pipe[O,X,R]): Pipe[I,X,R] = Pipe.fuse(pipe, that); def <-<[X](that: Pipe[X,I,R]): Pipe[X,O,R] = Pipe.fuse(that, pipe); } def fuse[I,O,X,R](i: Pipe[I,O,R], o: Pipe[O,X,R]): Pipe[I,X,R] =

Why doesn't Scala's implicit class work when one of the type parameters should be Nothing?

∥☆過路亽.° 提交于 2020-07-28 13:53:19
问题 Update: I modified the example so that can be compiled and tested. I have an implicit class that defines an enrichment method: case class Pipe[-I,+O,+R](f: I => (O, R)); object Pipe { // The problematic implicit class: implicit class PipeEnrich[I,O,R](val pipe: Pipe[I,O,R]) extends AnyVal { def >->[X](that: Pipe[O,X,R]): Pipe[I,X,R] = Pipe.fuse(pipe, that); def <-<[X](that: Pipe[X,I,R]): Pipe[X,O,R] = Pipe.fuse(that, pipe); } def fuse[I,O,X,R](i: Pipe[I,O,R], o: Pipe[O,X,R]): Pipe[I,X,R] =

How to do if-else for some specific generic type?

我只是一个虾纸丫 提交于 2020-07-23 06:18:09
问题 I have MyClass and I want to call both mySubMethod(key) and mySubMethod2(key) for integer, but only call mySubMethod2(key) for non-integer. I tried below codes but doesn't work. I also tried to change K == int to something else, e.g. K == Integer , K.equals(int) and K.equals(Integer) etc., but all of them don't work. How Can I fix it? Thanks. public class MyClass<K,V> { public boolean myMethod(K key) { if (K == int) { mySubMethod(key); }else { // do nothing } mySubMethod2(key); return false;