标签:winform style blog http io color os ar 使用
将普通日期格式:“2014年7月8日”转换成汉字日期格式:“二零一四年七月八日”。(暂时不考虑10日,13日,23日)
class Program { static void Main(string[] args) { string date = "2014年7月8日"; date = ConvertDate(date); Console.WriteLine(date); Console.ReadKey(); } private static string ConvertDate(string date) //字符串具有不可变性,不能直接修改字符串 { //将字符串转换成一个真正的char数组 char[] chs = date.ToCharArray(); for (int i = 0; i < date.Length; i++) { switch (chs[i]) { case ‘0‘: chs[i] = ‘零‘; break; case ‘1‘: chs[i] = ‘一‘; break; case ‘2‘: chs[i] = ‘二‘; break; case ‘3‘: chs[i] = ‘三‘; break; case ‘4‘: chs[i] = ‘四‘; break; case ‘5‘: chs[i] = ‘五‘; break; case ‘6‘: chs[i] = ‘六‘; break; case ‘7‘: chs[i] = ‘七‘; break; case ‘8‘: chs[i] = ‘八‘; break; case ‘9‘: chs[i] = ‘九‘; break; } } //把char数组转换成字符串 return new string(chs); } }
创建一个Person类,属性:姓名、性别、年龄;方法:SayHi() 。
class Person { private string _name; public string Name { get { return _name; } set { _name = value; } } public bool Gender { get; set; } public int Age { get; set; } public virtual void SayHi() //加上virtual,子类才能重写它 { Console.WriteLine("Hi!!!!!!"); } }
再创建一个Employee类继承Person类,扩展属性Salary,重写SayHi方法。
class Employee:Person { public decimal salary{get;set;} public override void SayHi() { Console.WriteLine("重写了子类中的方法"); ; } }
请编写一个类:ItcastClass,该类中有一个私有字段_names.数据类型为字符串数组,数组长度为5,并有5个默认的姓名。要求:为ItcastClass编写一个索引器,要求该索引器能够通过下标访问_names中的内容。
class ItcastClass { private string[] _names = { "叶长种", "王少伟", "杨中科", "苏坤", "科比" }; public string this[int index] //索引器 { get { return _names[index]; } set { _names[index] = value; } } }
class Program { static void Main(string[] args) { ItcastClass itcast = new ItcastClass(); //Console.WriteLine(itcast._names[0]);//没有索引器的情况下得这样写! Console.WriteLine(itcast[0]); Console.WriteLine(itcast[3]); Console.ReadKey(); } }
属性和索引
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _02属性和索引 { class Person { private string _name; public string Name { get {return _name ;} set { _name = value; } } //属性就是一个字段和两个方法 public int Age { get; set; } } class MyClass { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } //public string Item//在增加的时候不能用Item这个词 因为在索引器中它自动生成了Item属性 //{ // get; // set; //} public string this[int index] { get { string result = ""; switch (index) { case 0: result=this.Name; break; case 1: result = this.Age.ToString(); break; case 2: result = this.Email; break; } return result; } } public string this[string key] //重载 { get { string result = ""; switch (key) { case "name": result = this.Name; break; case "age": result = this.Age.ToString(); break; case "email": result = this.Email; break; } return result; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _02属性和索引 { class Program { static void Main(string[] args) { //MyClass mc = new MyClass(); //mc.Name = "叶长重"; //mc.Age = 18; //mc.Email = "826217795@qq.com"; //Console.WriteLine(mc.Name); //Console.WriteLine(mc.Age); //Console.WriteLine(mc.Email); //Console.ReadKey(); MyClass mc = new MyClass(); mc.Name = "叶长重"; mc.Age = 18; mc.Email = "826217795@qq.com"; Console.WriteLine(mc[0]); Console.WriteLine(mc[1]); Console.WriteLine(mc[2]); Console.WriteLine(mc["name"]); Console.WriteLine(mc["age"]); Console.WriteLine(mc["email"]); Console.ReadKey(); } } }
编程遍历WinForm窗体上所有TextBox控件并给它赋值为“叶长种”。
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //控件继承Control类 foreach (Control item in this.Controls) { //判断当前控件是否为文本框 if (item is TextBox) { ((TextBox)item).Text = "叶长种"; } } } }
使用WinForm窗体,制作一个简单的计算器,默认值为“请选择”。要求具有+、-、*、/功能!
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.cboCaoZuoFu.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { //校验用户选择的“操作符” if (cboCaoZuoFu.SelectedIndex > 0) { int n1 = Convert.ToInt32(txtNumber1.Text.Trim()); int n2 = Convert.ToInt32(txtNumber2.Text.Trim()); //int n2 = int.Parse(textBox2.Text.Trim());//这个也可以 switch (cboCaoZuoFu.Text) { case "+": lblResult.Text = (n1 + n2).ToString(); break; case "-": lblResult.Text = (n1 - n2).ToString(); break; case "*": lblResult.Text = (n1 * n2).ToString(); break; case "/": lblResult.Text = (n1 / n2).ToString(); break; default: break; } } else { MessageBox.Show("请选择操作符"); } } }
随机点名器:
private void button2_Click(object sender, EventArgs e) { Random r = new Random(); MessageBox.Show(r.Next(1,51).ToString()); }
什么是方法重载。(方法重载1.方法名必须一致。2,方法参数列表不同)
请将字符串数组(“中国”,“美国”,“巴西”,“澳大利亚”,“加拿大”)中的内容反转,然后输出反转后的数组,不能用数组的Reverse()方法。
class Program { static void Main(string[] args) { string[] names = { "中国", "美国", "巴西", "澳大利亚", "加拿大" }; //Array.Reverse(names); ReverseArray(names); for (int i = 0; i < names.Length; i++) //快捷键 打上for再按Tab键 { Console.WriteLine(names[i]); } Console.ReadKey(); } private static void ReverseArray(string[] names) //数组本身就是引用类型,不需要返回值 { for (int i = 0; i < names.Length / 2; i++) { string temp = names[i]; names[i] = names[names.Length - 1 - i]; names[names.Length - 1 - i] = temp; } } }
.net程序基本编写、执行流程(c#)
添加一个教师类和一个学生类:
public class Teacher { //构造函数特点:1、函数名和类名完全一样 2、不能有返回值,哪怕是void 3、一般访问修饰符为public public Teacher() { } //构造函数重载 public Teacher(string name) { this.Name = name; } public Teacher(string name, int Age) { this.Name = name; this.Age = Age; } public string Name { get; set; } public int Age { get; set; } public void Teach() { Console.WriteLine("上课。。。"); } }
class Student { public string Name { get; set; } public string SId { get; set; } public void shangke() { Console.WriteLine("上课。。。"); } }
class Program { static void Main(string[] args) { //当写好一个类以后就会有一个默认的无参的构造函数。 Teacher t = new Teacher(); } }
标签:winform style blog http io color os ar 使用
原文地址:http://www.cnblogs.com/yechangzhong-826217795/p/4055019.html