简体   繁体   中英

C# Parameter + Overloading

I'm trying to create a GUI application that makes use of a class called Chameleon. Within the class, there are three instance variables, Length, Name and Color. Length is a double, Name and Color are strings.

I need to overload the constructor of Chameleon. A new Chameleon object can be created: 1. with the length and name, 2. with the name and color, or 3. with the length, name and color specified. The constructor should place a message with information about the Chameleon that was just created into a public instance variable.

The GUI should allow the user to enter the length (if it is specified the valid length is from 0.1 to 10.2 inches), the name (must not be empty) and the color (if it is specified it must be one of: 'green', 'orange', 'brown', 'black', 'yellow', or 'red').

When the user presses an 'OK' button a Chameleon object is created using only the arguments for which the user has specified a value. After that the public instance variable containing the message must be displayed in a label from the 'OK' button's click procedure.

I have very little experience with GUI applications and I am completely lost here. Any help at all to at least get me started would be much appreciated. Like for starters, how would you overload the constructor and allow it so that three different object can be created: 1. with the length and name, 2. with the name and color, or 3. with the length, name and color specified?

Add your other constructors with the parameters you need, like so:

class Chameleon
{

    private double length;
    private string name;
    private string color;

    public Chameleon(string nameValue, double lengthValue, string colorValue)
    {
        name = nameValue;
        length = lengthValue;
        color = colorValue;
    }

    public Chameleon(string nameValue, double lengthValue)
    {
        name = nameValue;
        length = lengthValue;
    }

    public Chameleon(string nameValue, string colorValue)
    {
        name = nameValue;
        color = colorValue;
    }
}

Then from the procedure that creates this instance, write it like so:

Chameleon chameleon = new Chameleon("SomeName", 1.0);
Chameleon anotherChameleon = new Chameleon("SomeOtherName", "blue");
Chameleon aThirdChameleon = new Chameleon("ChameleonName", 1.0, "blue");

You first should create a default constructor. A default constructor would be used when you do not pass any parameters to instantiate your object. The default constructor will use your default values. Then you can create your overloaded constructors. An overloaded constructor will take any number of different parameters that will override the default values.

class Chameleon 
{ 
    private double length; 
    private string name; 
    private string color; 

    //default constructor
    public Chameleon()
    {
      length = 2;
      name = "widget";
      color = "blue";
    }

    //Overloaded Constructor
    public Chameleon( double lengthValue, string nameValue, string colorValue)
    { 
        length = lengthValue; 
        name = nameValue; 
        color = colorValue; 
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM