Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:56:27+00:00 2026-05-29T08:56:27+00:00

I am making a simulation of an ecosystem, it’s very simple so don’t worry

  • 0

I am making a simulation of an ecosystem, it’s very simple so don’t worry about realism.
The reproduction method is located in the Organism class, it could be used exactly by the class Plant, except for the return type which is Organism:

    public Organism Reproduce()
    {
        double[] copy = new double[genes.Count];
        for(int i = 0; i < copy.Length; i++)
            // 10% chance to mutate, change up to 10%
            copy[i] = genes[i] + (Program.rand.Next(10) < 1 ? 
                genes[i] * 0.2 * (Program.rand.NextDouble() - 0.5) : 0.0);
        return new Organism(genes);
    }

I know in Ruby it’s possible to return ‘self’ so if the method is used by a class that extends this one, the method will return an object of the inheriting class.

So the question is: How can I modify this method so that when it’s called from a Plant, it will make a Plant and return one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:56:28+00:00Added an answer on May 29, 2026 at 8:56 am

    You will need to use generics. The simplest way would be to make just this method generic. The one issue you will come across is that you must have a public parameter-less constructor.

    public TOrganism Reproduce<TOrganism>()
        where TOrganism : Organism, new()
    {
        double[] copy = new double[genes.Count];
        for(int i = 0; i < copy.Length; i++)
            // 10% chance to mutate, change up to 10%
            copy[i] = genes[i] + (Program.rand.Next(10) < 1 ? 
                genes[i] * 0.2 * (Program.rand.NextDouble() - 0.5) : 0.0);
    
        TOrganism child = new TOrganism();
        child.Genes = genes;
        return child;
    }
    

    Then you will call this method like so.

    Plant myPlant = new Plant();
    Plant newPlant = myPlant.Reproduce<Plant>();
    

    I would go one step further and make this a separate class to make testing easier. Maybe even an extension method.

    public static class OrganismReproducer : IOrganismReproducer
    {
        public static TOrganism Reproduce<TOrganism>(this TOrganism organism)
            where TOrganism : Organism, new()
        {
            double[] copy = new double[organism.Genes.Count];
    
            for(int i = 0; i < copy.Length; i++)
                // 10% chance to mutate, change up to 10%
                copy[i] = genes[i] + (Program.rand.Next(10) < 1 ? 
                    genes[i] * 0.2 * (Program.rand.NextDouble() - 0.5) : 0.0);
    
            TOrganism child = new TOrganism();
            child.Genes = genes;
            return child;
        }
    }
    

    You can then call this like so.

    Plant myPlant = new Plant();
    Plant newPlant = myPlant.Reproduce(); 
        // no need to specify generic type parameter
        // it is inferred based on the type of myPlant
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context: I'm making a simple solar system simulation in c++/OpenGL. Research: I've tried searching
I'm making a simple learning simulation, where there are multiple organisms on screen. They're
I am making a simulation using python 2.6 + Simpy, about the subway system.
I am making a simple flok- or crowd-simulation for a small game. The behaviour
I'm currently making a simple manager game/simulation to learn how to handle bigger projects.
I ran into the situation where my class is making one asynchronous web call
I am making a simulation that involves particles dynamically added to the stage (lots
I'm making a basic simulation of moving planets and gravitational pull between them, and
I'm making a Monte-Carlo simulation. And as a part of this task I generate
i am making 3D Simulation of Solar System for every planet i have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.