polymorphism

Laravel - many-to-many where the many-to-many table is (part-) polymorph

两盒软妹~` 提交于 2019-12-25 05:45:47
问题 I have a table called bonus. A user can get a bonus (it's like an reward) for certain actions. Well, the bonus can be assigned to many users and many users can get the same bonus. So it's a many to many relation between user and bonus. This is no problem so far. But users can get the same bonus for different actions. So let's say there is a bonus for voting on a picture. Well, one user could vote on one picture and another one could vote on another picture which I'd like to save in the many

Dynamic instantiation advantages in JAVA

冷暖自知 提交于 2019-12-25 05:31:59
问题 I have 2 classes. Animal and Horse. Animal is the super class, horse extends animal. public class Animal { public void legs() { System.out.println(this + " Some animals can have 3 legs, some have 4"); } } public class Horse extends Animal { public void legs() { System.out.println(this + " Horses always have 4 legs"); } public void tail() { System.out.println("A horse definitely has a tail"); } } From another class, I am doing some functions on these classes. public class SCJP_Practice {

Using vectors to store different objects inherited from the same parent class c++

三世轮回 提交于 2019-12-25 05:30:51
问题 I have a class, let's call it Polynomials. The number of needed Polynomials are not known in advance and are determined at run-time. My current implementation uses a container class, let's call it Solutions, in which there is a vector, let's call it polynomialVector, it is defined this way: std::vector<Polynomial> polynomialVector; The vector is created and populated in this way (The constructor of Solutions do this job): polynomialVector.reserve(numberofneededpolynomials); for(int i = 0; i <

Preventing multiple objects from being printed

我只是一个虾纸丫 提交于 2019-12-25 05:06:54
问题 For my final CS180 project we have been given the task of creating a program that randomly generates some perishable and nonperishable items that would be found in a grocery cart and then printing a receipt for a customer. The problem I am running into is finding a way to check the frequency of an item being created and preventing the item from printing multiple times on a receipt. For example, if someone buys 5 peaches the I want the output to be Peach $.99 X 5, total $4.95, instead of

Is it possible to have polymorphic member overloading in C++?

别来无恙 提交于 2019-12-25 04:38:22
问题 I have been working on a Roguelike, and run into a problem with it. My problem is that I would like to use "polymorphic overloading" or sorts, but I'm guessing C++ doesn't support. My class diagram is like this: xMapObject <- xEntity <- xVehicle An the problem is, it is possible to have this: class xMapObject { public: virtual void Bump(xMapObject *MapObject); virtual void Bump(xEntity *Entity); virtual void Bump(xVehicle *Vehicle); virtual void BumpedBy(xMapObject *MapObject); virtual void

java polymorphism late binding rules

浪子不回头ぞ 提交于 2019-12-25 04:23:12
问题 I am trying to do some polymorphism exercises and I cannot figure out how this kind of polymorphism works. I did not find any deep information about this kind of exercised. I hope you guys could give me some explanation. exercise 1: class Top { public void m( Middle p ) System.out.print("A "); } class Middle extends Top { public void m( Object p ) System.out.print("M "); public void m( Middle p ) System.out.print("L "); } class Bottom extends Middle { public void m( Object p ) System.out

C# Get subclass attribute value

不问归期 提交于 2019-12-25 04:09:06
问题 I'm trying to make this: class A { //attibutes } class B : A { public int classBAttribute = 5; // other attributes and methods } My question is this if I have an instance of A class how can i get the instance of the B class or access his attributes? B b = new B(); Application.Add(b); //other form A a = Application.GetA(); B b = getBFromA(a);// ??? Note: B b = a as B; does't work i tried 回答1: You cannot do this -- there is no magical way to create derived objects from base objects in general.

Show function for polymorphic type

自闭症网瘾萝莉.ら 提交于 2019-12-25 03:58:12
问题 I'm trying to define the Show function for the polymorphic Tree type. Could anyone help me? import Char data Tree t = NilT | Node t (Tree t) (Tree t) class Mar t where maior :: t -> String instance Mar Tree where maior (NilT) = "a" maior (Node t a b) = "b" instance Show Tree where show = maior Thanks a Lot! Solution (given by ivanm): import Char data Tree t = NilT | Node t (Tree t) (Tree t) class Mar t where maior :: t -> String instance Mar (Tree t) where maior (NilT) = "a" maior (Node t a b

Dynamic cast in Java

若如初见. 提交于 2019-12-25 03:25:14
问题 I'm not sure if this is what it's called, but here is the problem: I have a superclass with three subclasses. Let's say Superclass, Subclass1, Subclass2,Subclass3 I have another class with the following overloaded method: public void exampleMethod (Subclass1 object1){ //Method to be called if the object is of subclass 1 } public void exampleMethod (Subclass2 object2){ //Method to be called if the object is of subclass 2 } public void exampleMethod (Subclass3 object3){ //Method to be called if

@Inject in abstract class based on subclass

不羁岁月 提交于 2019-12-25 02:34:32
问题 let imagine I have per entity a repository class (spring data jpa) for database access and a service class. The dependencies are managed by spring framework. Every service method does in most cases the same, so there is mainly code duplication: public class NewsService { @Inject private NewsRepository newsRepository; public void add(News news) { // do some validation newsRepository.save(news); } } public class UserService { @Inject private UserRepository userRepository; public void add(User