黑马程序员_每天必敲的几段代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 继承 7 { 8 #region 人类,父类。 9 class Person 10 { 11 private string name; 12 13 protected string Name 14 { 15 get { return name; } 16 set { name = value; } 17 } 18 19 private char sex; 20 21 protected char Sex 22 { 23 get { return sex; } 24 set { sex = value; } 25 } 26 27 private int age; 28 29 protected int Age 30 { 31 get { return age; } 32 set { age = value; } 33 } 34 35 public Person(string name, char sex, int age) 36 { 37 this.Name = name; 38 this.Sex = sex; 39 this.Age = age; 40 } 41 } 42 #endregion 43 44 #region 老师类,继承(派生)于人类,子类。 45 class Teacher : Person 46 { 47 private int year; 48 49 public int Year 50 { 51 get { return year; } 52 set { year = value; } 53 } 54 55 private int salary; 56 57 public int Salary 58 { 59 get { return salary; } 60 set { salary = value; } 61 } 62 63 public Teacher(string name, char sex, int age, int year, int salary) 64 : base(name, sex, age) 65 { 66 this.Year = year; 67 this.Salary = salary; 68 } 69 70 public void SayHello() 71 { 72 Console.WriteLine("我是{0},当了{1}年老师,我的月薪是{2}。", Name, Year, Salary); 73 } 74 } 75 #endregion 76 77 #region 学生类,继承(派生)于人类,子类。 78 class Student : Person 79 { 80 private string hobby; 81 82 protected string Hobby 83 { 84 get { return hobby; } 85 set { hobby = value; } 86 } 87 88 private int classID; 89 90 protected int ClassID 91 { 92 get { return classID; } 93 set { classID = value; } 94 } 95 96 public Student(string name, char sex, int age, string hobby, int classID) 97 : base(name, sex, age) 98 { 99 this.Hobby = hobby; 100 this.ClassID = classID; 101 } 102 103 public void SayHello() 104 { 105 Console.WriteLine("我是{0},我是{1}孩,今年{2}岁,爱好是{3},在{4}班。", Name, Sex, Age, Hobby, ClassID); 106 } 107 } 108 #endregion 109 110 class Program 111 { 112 static void Main(string[] args) 113 { 114 Student s = new Student("张三", '男', 18, "踢足球", 3); 115 s.SayHello(); 116 Teacher t = new Teacher("王老师", '女', 30, 5, 8000); 117 t.SayHello(); 118 Console.ReadKey(true); 119 } 120 } 121 //输出结果: 122 //我是张三,我是男孩,今年18岁,爱好是踢足球,在3班。 123 //我是王老师,当了5年老师,我的月薪是8000。 124 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 多态USB的例子 7 { 8 class Standard 9 { 10 public virtual void USB() 11 { 12 Console.WriteLine("USB标准。"); 13 } 14 } 15 class Phone : Standard 16 { 17 public override void USB() 18 { 19 Console.WriteLine("充电中"); 20 } 21 } 22 class Light : Standard 23 { 24 public override void USB() 25 { 26 Console.WriteLine("灯亮了"); 27 } 28 } 29 class Program 30 { 31 static void Main(string[] args) 32 { 33 while (true) 34 { 35 Console.WriteLine(@"请选择插入的USB设备: 36 1.手机 37 2.灯"); 38 string select = Console.ReadLine(); 39 Standard s; 40 switch (select) 41 { 42 case "1": s = new Phone(); break; 43 case "2": s = new Light(); break; 44 default: s = null; break; 45 } 46 if (s != null) 47 { 48 s.USB(); 49 } 50 else 51 { 52 Console.WriteLine("输入有误,请重新输入:"); 53 } 54 Console.ReadKey(true); 55 Console.Clear(); 56 } 57 } 58 } 59 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 委托比较算法 7 { 8 class Program 9 { 10 #region 准备委托类型 11 public delegate bool DeleCompare(string str1, string str2); 12 #endregion 13 14 static void Main(string[] args) 15 { 16 while (true) 17 { 18 string str1 = "20"; 19 string str2 = "100"; 20 Console.WriteLine("请问你要用什么方式进行比较:(0、数字;1、字符串)"); 21 string res = Console.ReadLine(); 22 #region 定义委托变量 23 DeleCompare dc; 24 #endregion 25 #region 定义委托变量 26 if (res == "0") 27 { 28 //装配方法 29 dc = Compare1; 30 } 31 else if (res == "1") 32 { 33 //装配方法 34 dc = Compare2; 35 } 36 else 37 { 38 dc = null; 39 Console.WriteLine("输入有误,请重新输入。"); 40 Console.ReadKey(true); 41 Console.Clear(); 42 continue; 43 } 44 #endregion 45 string str = ""; 46 47 //开始比较 48 #region 使用 49 str = dc(str1, str2) ? ">" : "<"; 50 #endregion 51 Console.WriteLine("{0}{1}{2}", str1, str, str2); 52 Console.ReadKey(true); 53 Console.Clear(); 54 } 55 } 56 #region 准备方法 57 //比较数字的大小 58 static bool Compare1(string str1, string str2) 59 { 60 int num1 = int.Parse(str1); 61 int num2 = int.Parse(str2); 62 return num1 > num2; 63 } 64 //比较字符串的大小 65 static bool Compare2(string str1, string str2) 66 { 67 if (string.Compare(str1, str2) > 0) 68 { 69 return true; 70 } 71 return false; 72 } 73 #endregion 74 } 75 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Text.RegularExpressions; 6 using System.IO; 7 8 namespace 生成十万数据库 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 List<string> strFirstName = new List<string>(); 15 List<string> boys = new List<string>(); 16 List<string> girls = new List<string>(); 17 MatchCollection mc = Regex.Matches(File.ReadAllText(@"E:\同步盘\Tb_Soft\C#\程序素材\百家姓.txt", Encoding.UTF8), @"〔([\w]{1,2})〕"); 18 foreach (Match temp in mc) 19 { 20 strFirstName.Add(temp.Groups[1].Value); 21 } 22 boys.AddRange(File.ReadAllText(@"E:\同步盘\Tb_Soft\C#\程序素材\好听的男孩名字.txt", Encoding.UTF8).Split(new char[] { '、' })); 23 girls.AddRange(File.ReadAllText(@"E:\同步盘\Tb_Soft\C#\程序素材\好听的女孩名字.txt", Encoding.Default).Split(new char[] { '、' })); 24 StringBuilder sb = new StringBuilder(); 25 sb.AppendLine(File.ReadAllText(@"E:\同步盘\Tb_Soft\C#\程序素材\十万数据库建表.txt", Encoding.Default)); 26 Random r = new Random(); 27 string strLastName = null; 28 string strGender = null; 29 for (int i = 0; i < 100000; i++) 30 { 31 if (r.Next(2) == 1) 32 { 33 strLastName = boys[r.Next(boys.Count)]; 34 strGender = "男"; 35 } 36 else 37 { 38 strLastName = girls[r.Next(girls.Count)]; 39 strGender = "女"; 40 } 41 sb.AppendLine(string.Format("insert into Score values({0},{1},{2},{3})\r\ngo", r.Next(151), r.Next(151), r.Next(151), r.Next(301))); 42 sb.AppendLine(string.Format("insert into Student values('{0}',{1},'{2}',{3})\r\ngo", strFirstName[r.Next(strFirstName.Count)] + strLastName, r.Next(10, 51), strGender, i + 1)); 43 } 44 File.WriteAllText(@"E:\生成十万数据库.sql", sb.ToString()); 45 } 46 } 47 }
1 <html> 2 <head> 3 <title>练习01</title> 4 </head> 5 <body> 6 <h1>促销信息</h1> 7 <img src=".\notebook.jpg" border="1"alt="ACER上网本,特惠拍卖!"/><br/> 8 拍卖ACER上网本<br/> 9 奔腾双核,1G内存,200G硬盘<br/> 10 跳楼疯抢价<img src=".\1.png" width="20"height="30"/>元起<br/> 11 </body> 12 </html>
1 <html> 2 <head> 3 <title>练习05</title> 4 </head> 5 <body> 6 <h3>贵美网站会员登录</h3> 7 <form> 8 <table border="0"> 9 <tr> 10 <td>贵美网站邮箱:</td><td><input type="text" name="mail" value=""/></td> 11 </tr> 12 <tr> 13 <td>输 入 密 码:</td><td><input type="password" name="pwdfirst" value=""/></td> 14 </tr> 15 <tr> 16 <td>再次输入密码:</td><td><input type="password" name="pwdsecond" value=""/></td> 17 </tr> 18 <tr> 19 <td><input type="radio" name="verson"/>豪华版</td><td><input type="radio" name="verson"/>简洁版</td> 20 </tr> 21 <tr> 22 <td><input type="checkbox" name="autologin"/>自动登录</td><td><input type="checkbox" name="safelogin"/>安全登录</td> 23 </tr> 24 <tr> 25 <td><input type="reset" value="重置"/></td><td><input type="button" value="登录"/></td> 26 </tr> 27 <tr> 28 <td colspan="2"><input type="image" src="start.png"/></td> 29 </tr> 30 </table> 31 </form> 32 </body> 33 </html>
陆续添加中......