C# 如何使用此C代码创建默认构造函数?
我正在尝试使用此代码创建默认构造函数。这是没有构造器的原件,有人能帮忙吗C# 如何使用此C代码创建默认构造函数?,c#,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 { p
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
}
}
}
非常感谢。谢谢大家! 给我做你的家庭作业是小菜一碟吗
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
}
默认构造函数只是指没有参数/参数的构造函数。那么,public Form1()是一个默认构造函数(没有参数),你能澄清你的问题吗?是的-如果你的意思是你想要一个额外的构造函数,只需添加一个新的public Form1(字符串myVal),将逻辑放在中间,并在代码后添加InitializeComponet()。或者更好的是,从重载函数调用默认值,如下所示:public Form1(string myArgument):this()
这是没有构造函数的原始函数
,但是代码中有一个默认构造函数。你确定你知道什么是默认构造函数吗?研究更多关于构造函数的知识。