如何使用此C#代码创建默认构造函数?

时间:2015-08-31 16:39:11

标签: c#

我正在尝试使用此代码创建默认构造函数。这是没有构造函数的原始版本,有人可以帮忙吗?:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Chapter_2_Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }



    private void Order_Click(object sender, EventArgs e)
    {

    string cakeFlavor, frostingFlavor;
    int size; //This declasres an integer that represents size
    cakeFlavor = Cake.Text;            frostingFlavor = Frosting.Text;
    size = Convert.ToInt32(Size.Text);

    Display.Text = "Cake Flavor: " + cakeFlavor + Environment.NewLine + "Frosting Flavor: " + frostingFlavor + Environment.NewLine + "Cake Size: " + size + " Inches" + Environment.NewLine + "Thank you for shopping" + enter code hereEnvironment.NewLine + "at The Token Bakery!";
        //This Displays all the info that the user input

    }

}

}

非常感谢。谢谢!

1 个答案:

答案 0 :(得分:1)

我可以做一些功课吗?

class Cake
{
#region Fields
private string _cakeFlavor;
private string _frostingFlavor;
#endregion

#region Properties
public string CakeFlavor
{
    get { return _cakeFlavor; }
    set { _cakeFlavor = value; }
}

public string FrostingFlavor
{
    get { return _frostingFlavor; }
    set { _frostingFlavor = value; }
}
#endregion

#region Constructors
public Cake() : this("Cake flavor not provided.", "Frosting flavor not provided.")
{
}
public Cake(string cakeFlavor, string frostingFlavor)
{
    this._cakeFlavor = cakeFlavour;
    this._frostingFlavor = frostingFlavor;
}
#endregion

#region Methods
public void PrintCakeFlavors()
{
    Console.WriteLine("Cake Flavor: {0}\nFrosting Flavor: {1}", this._cakeFlavor, this._frostingFlavor);
}
#endregion
}