1、new关键字 1)、创建对象 2)、隐藏从父类那里继承过来的成员
2、访问修饰符 public:公开的,公共的 private:私有的,只能在当前类的内部访问,类中成员们,如果不加访问修饰符,默认就是private procteced:受保护的,可以在当前类的内部访问,也可以在该类的子类中访问 internal:在当前项目中都可以访问。 protected internal: 能够修饰类的访问修饰符只有两个,internal和public
3、常用的关键字 this 1、当前类的对象 2、调用自己的构造函数 base 调用父类的成员 new 1、创建对象 2、隐藏父类的成员 virtual 标记一个方法是虚方法 abstract 抽象的 override 重写 interface 接口 partial 部分类 sealed 密封类:不允许被继承 return 1、在方法中返回要返回的值 2、立即结束本次方法 break 跳出当前循环 continue 结束本次循环,回到循环条件进行判断 static 静态的 struct 结构 enum 枚举 const 常量
4、字符串 1、字符串的不可变性 2、字符串可以看做是char类型的只读数组
5、集合 ArrayList Hashtable List<T> Dictionary<TKey,TValue>
6装箱或者拆箱 装箱:就是将值类型转换为引用类型 拆箱:就是将引用类型转换为值类型。 值类型:bool int double char struct enum decimal 引用类型:string 数组 集合 interface object 自定义类 拆箱或者装箱的两种类型必须具有继承关系
7、结构和类的区别 1、类当中写了一个新的构造函数之后,默认的无参数的就被干掉了。 但是在结构中,写了一个新的构造函数之后,默认的那个无参数的还在。 在结构的构造函数当中,必须给所有的字段赋值,这样也就限制了结构中最多 只能有两个构造函数,一个是默认的无参数的构造函数,一个是全参数的构造函数。
2、结构的new关键字只干了一件事儿,就是调用结构的构造函数。
3、结构不能继承于类,而类也不能继承于结构。 结构不具备面向对象的特征。 而类才是面向对象的产物。
4、如果不给结构创建对象的话,不允许调用结构中的非静态方法。
01面向对象复习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _01面向对象复习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //this 14 //new 15 Person p = new Person(); 16 17 } 18 } 19 20 public class Person 21 { 22 23 protected string _name; 24 internal int _age; 25 } 26 27 internal class Teacher : Person 28 { 29 public void T() 30 { 31 32 } 33 } 34 }
02关键字的用法
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _02关键字的用法 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //partial 14 } 15 } 16 17 /// <summary> 18 /// 密封类 19 /// </summary> 20 public sealed class Teacher:Person 21 { 22 23 } 24 25 26 public partial class Person 27 { 28 private string _name; 29 } 30 31 public partial class Person 32 { 33 public void T() 34 { 35 // _name; 36 } 37 } 38 }
03抽象类练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _03抽象类练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 MobileDisk md = new MobileDisk(); 14 Mp3 mp3 = new Mp3(); 15 UDisk u = new UDisk(); 16 17 Computer cpu = new Computer(); 18 cpu.MS = mp3; 19 cpu.CPURead(); 20 cpu.CPUWrite(); 21 Console.ReadKey(); 22 } 23 } 24 25 /// <summary> 26 /// 抽象的移动存储设备父类 27 /// </summary> 28 public abstract class MobileStorage 29 { 30 public abstract void Read(); 31 public abstract void Write(); 32 } 33 public class MobileDisk : MobileStorage 34 { 35 public override void Read() 36 { 37 Console.WriteLine("移动硬盘在读取数据"); 38 } 39 40 public override void Write() 41 { 42 Console.WriteLine("移动硬盘在写入数据"); 43 } 44 } 45 public class UDisk : MobileStorage 46 { 47 public override void Read() 48 { 49 Console.WriteLine("U盘在读取数据"); 50 } 51 public override void Write() 52 { 53 Console.WriteLine("U盘在写入数据"); 54 } 55 } 56 public class Mp3 : MobileStorage 57 { 58 public void PlayMusic() 59 { 60 Console.WriteLine("Mp3自己可以播放音乐"); 61 } 62 63 public override void Read() 64 { 65 Console.WriteLine("Mp3读取数据"); 66 } 67 68 public override void Write() 69 { 70 Console.WriteLine("Mp3写入数据"); 71 } 72 } 73 74 75 public class Computer 76 { 77 78 public MobileStorage MS 79 { 80 get; 81 set; 82 } 83 public void CPURead() 84 { 85 this.MS.Read(); 86 } 87 88 public void CPUWrite() 89 { 90 this.MS.Write(); 91 } 92 } 93 }
04虚方法和抽象类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _04虚方法和抽象类 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 } 14 } 15 public abstract class Person 16 { 17 private string _name; 18 19 public string Name 20 { 21 get { return _name; } 22 set { _name = value; } 23 } 24 public Person(string name) 25 { 26 this.Name = name; 27 } 28 public Person() 29 { 30 31 } 32 public virtual void SayHello() 33 { 34 35 } 36 public abstract double Test(string name); 37 38 // public abstract void Test(); 39 } 40 41 public class Student : Person 42 { 43 44 public override double Test(string name) 45 { 46 return 123; 47 } 48 // public abstract void M(); 49 //public void SayHello() 50 //{ 51 52 //} 53 } 54 }
05接口复习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.IO; 7 namespace _05接口复习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //alt+shift+F10 14 // File 15 //接口是什么?什么时候使用接口?使用接口的目的是什么?多态 16 //鸟会飞 飞机也会飞 17 18 IFlyable fly = new Plane();//new Bird(); 19 fly.Fly(); 20 Console.ReadKey(); 21 } 22 } 23 public interface IFlyable 24 { 25 void Fly(); 26 } 27 28 public class Bird 29 { 30 //public void Fly() 31 //{ 32 // Console.WriteLine("大多数鸟都会飞"); 33 //} 34 } 35 //public class QQ : Bird 36 //{ 37 38 //} 39 40 public class Maque : Bird, IFlyable 41 { 42 /// <summary> 43 /// 这个函数是父类的?还是子类自己的?还实现接口的? 44 /// </summary> 45 public void Fly() 46 { 47 Console.WriteLine("鸟会飞"); 48 } 49 void IFlyable.Fly() 50 { 51 Console.WriteLine("实现的接口的飞方法"); 52 } 53 } 54 55 public class Plane : IFlyable 56 { 57 public void Fly() 58 { 59 Console.WriteLine("飞机会飞"); 60 } 61 } 62 }
06面向对象练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _06面向对象练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //作业:定义父亲类Father(姓lastName,财产property,血型bloodType), 14 //儿子Son类(玩游戏PlayGame方法),女儿Daughter类(跳舞Dance方法), 15 //调用父类构造函数(:base())给子类字段赋值 16 17 //Son son = new Son("张三",10m,"AB"); 18 //son.PlayGame(); 19 //son.SayHello(); 20 //Daughter d = new Daughter("张梅梅", 100m, "O"); 21 //d.SayHello(); 22 //d.Dance(); 23 //Console.ReadKey(); 24 25 //作业:定义汽车类Vehicle属性(brand(品牌),color(颜色))方法run, 26 //子类卡车(Truck) 属性weight载重 方法拉货,轿车 (Car) 属性passenger载客数量 方法载客 27 28 //Truck truck = new Truck("解放牌卡车", "绿色", 30000); 29 //Car car = new Car("奔驰", "黑色", 5); 30 //truck.LaHuo(); 31 //car.LaKe(); 32 //Console.ReadKey(); 33 34 } 35 } 36 37 /// <summary> 38 /// 汽车的父类 39 /// </summary> 40 public class Vehicle 41 { 42 public string Brand 43 { 44 get; 45 set; 46 } 47 48 public string Color 49 { 50 get; 51 set; 52 } 53 54 public void Run() 55 { 56 Console.WriteLine("我是汽车我会跑"); 57 } 58 59 public Vehicle(string brand, string color) 60 { 61 this.Brand = brand; 62 this.Color = color; 63 } 64 } 65 66 67 public class Truck : Vehicle 68 { 69 public double Weight 70 { 71 get; 72 set; 73 } 74 public Truck(string brand, string color, double weight) 75 : base(brand, color) 76 { 77 this.Weight = weight; 78 } 79 80 81 public void LaHuo() 82 { 83 Console.WriteLine("我最多可以拉{0}KG货物",this.Weight); 84 } 85 } 86 87 88 public class Car : Vehicle 89 { 90 public int Passenger 91 { 92 get; 93 set; 94 } 95 public Car(string brand, string color, int passenger) 96 : base(brand, color) 97 { 98 this.Passenger = passenger; 99 } 100 101 102 public void LaKe() 103 { 104 Console.WriteLine("我最多可以拉{0}个人",this.Passenger); 105 } 106 } 107 108 109 public class Father 110 { 111 private string _lastName; 112 public string LastName 113 { 114 get { return _lastName; } 115 set { _lastName = value; } 116 } 117 118 private decimal _property; 119 public decimal Property 120 { 121 get { return _property; } 122 set { _property = value; } 123 } 124 125 private string _bloodType; 126 public string BloodType 127 { 128 get { return _bloodType; } 129 set { _bloodType = value; } 130 } 131 132 public Father(string lastName, decimal property, string bloodType) 133 { 134 this.LastName = lastName; 135 this.Property = property; 136 this.BloodType = bloodType; 137 } 138 139 public void SayHello() 140 { 141 Console.WriteLine("我叫{0},我有{1}元,我是{2}型血",this.LastName,this.Property,this.BloodType); 142 } 143 144 } 145 146 public class Son : Father 147 { 148 public Son(string lastName, decimal property, string bloodType) 149 : base(lastName, property, bloodType) 150 { 151 152 } 153 154 public void PlayGame() 155 { 156 Console.WriteLine("儿子会玩游戏"); 157 } 158 } 159 160 public class Daughter : Father 161 { 162 public Daughter(string lastName, decimal property, string bloodType) 163 : base(lastName, property, bloodType) 164 { 165 166 } 167 168 public void Dance() 169 { 170 Console.WriteLine("女儿会跳舞"); 171 } 172 } 173 }
07面向对象多态练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _07面向对象多态练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //作业:员工类、部门经理类 程序猿类 14 //(部门经理也是员工,所以要继承自员工类。员工有上班打卡的方法。用类来模拟。 15 16 Employee emp = new Programmer();//new Manager();//new Employee(); 17 emp.DaKa(); 18 Console.ReadKey(); 19 } 20 } 21 public class Employee 22 { 23 public virtual void DaKa() 24 { 25 Console.WriteLine("员工九点打卡"); 26 } 27 } 28 29 public class Manager : Employee 30 { 31 public override void DaKa() 32 { 33 Console.WriteLine("经理十一点打卡"); 34 } 35 } 36 37 public class Programmer : Employee 38 { 39 public override void DaKa() 40 { 41 Console.WriteLine("程序猿不打卡"); 42 } 43 } 44 45 }
08面向对象多态练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _08面向对象多态练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //作业:动物Animal 都有吃Eat和叫Bark的方法,狗Dog和猫Cat叫的方法不一样. 14 //父类中没有默认的实现所哟考虑用抽象方法。 15 16 Animal a = new Dog();//new Cat(); 17 a.Bark(); 18 a.Eat(); 19 Console.ReadKey(); 20 } 21 } 22 23 public abstract class Animal 24 { 25 public abstract void Eat(); 26 public abstract void Bark(); 27 } 28 29 public class Cat : Animal 30 { 31 public override void Eat() 32 { 33 Console.WriteLine("猫咪舔着吃"); 34 } 35 36 public override void Bark() 37 { 38 Console.WriteLine("猫咪喵喵的叫"); 39 } 40 } 41 42 public class Dog : Animal 43 { 44 public override void Bark() 45 { 46 Console.WriteLine("狗狗旺旺的叫"); 47 } 48 49 public override void Eat() 50 { 51 Console.WriteLine("狗狗咬着吃"); 52 } 53 } 54 55 }
09面向对象接口练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _09面向对象接口练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //作业:鸟-麻雀sparrow[‘spær?u] ,鸵鸟ostrich[‘?strit?] , 14 //企鹅penguin[‘pengwin] ,鹦鹉parrot[‘pær?t] 15 //鸟能飞,鸵鸟,企鹅不能。。。你怎么办 16 17 IFlyable fly = new YingWu();// new QQ();//new YingWu();//new MaQue(); 18 fly.Fly(); 19 Console.ReadKey(); 20 } 21 } 22 23 public interface IFlyable 24 { 25 void Fly(); 26 } 27 28 public class Bird 29 { 30 public double Wings 31 { 32 get; 33 set; 34 } 35 36 public void SayHello() 37 { 38 Console.WriteLine("我是小鸟"); 39 } 40 } 41 42 public class MaQue : Bird, IFlyable 43 { 44 public void Fly() 45 { 46 Console.WriteLine("麻雀会飞"); 47 } 48 } 49 50 public class TuoNiao : Bird 51 { 52 53 } 54 55 public class QQ : Bird 56 { 57 58 } 59 60 public class YingWu : Bird, IFlyable 61 { 62 public void Fly() 63 { 64 Console.WriteLine("鹦鹉会飞"); 65 } 66 } 67 }
10面向对象多态练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _10面向对象多态练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //作业:橡皮rubber鸭子、木wood鸭子、真实的鸭子realduck。 14 //三个鸭子都会游泳,而橡皮鸭子和真实的鸭子都会叫, 15 //只是叫声不一样,橡皮鸭子“唧唧”叫,真实地鸭子“嘎嘎”叫,木鸭子不会叫. 16 //IBark bark = new RealDuck();//new XPDuck(); 17 //bark.Bark(); 18 XPDuck xp = new XPDuck(); 19 MuDuck md = new MuDuck(); 20 RealDuck rd = new RealDuck(); 21 IBark bark = rd; 22 Duck duck = rd; 23 duck.Swim(); 24 bark.Bark(); 25 26 27 Console.ReadKey(); 28 29 } 30 } 31 32 public class Duck 33 { 34 public virtual void Swim() 35 { 36 Console.WriteLine("是鸭子就会游泳"); 37 } 38 } 39 40 public interface IBark 41 { 42 void Bark(); 43 } 44 45 public class RealDuck : Duck, IBark 46 { 47 public void Bark() 48 { 49 Console.WriteLine("这的鸭子嘎嘎叫"); 50 } 51 52 public override void Swim() 53 { 54 Console.WriteLine("真的鸭子会游泳"); 55 } 56 } 57 58 public class MuDuck:Duck 59 { 60 public override void Swim() 61 { 62 Console.WriteLine("木头鸭子也会游泳"); 63 } 64 } 65 66 public class XPDuck : Duck, IBark 67 { 68 public void Bark() 69 { 70 Console.WriteLine("橡皮鸭子唧唧叫"); 71 } 72 73 public override void Swim() 74 { 75 Console.WriteLine("橡皮鸭子也会游泳"); 76 } 77 } 78 }
11、抽象类练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _11_抽象类练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //作业:计算形状Shape(圆Circle,矩形Square ,正方形Rectangle)的面积、周长 14 Shape shape = new Square(5,7);//new Circle(5); 15 double area = shape.GetArea(); 16 double perimeter = shape.GetPerimeter(); 17 Console.WriteLine(area); 18 Console.WriteLine(perimeter); 19 Console.ReadKey(); 20 } 21 } 22 23 public abstract class Shape 24 { 25 public abstract double GetArea(); 26 public abstract double GetPerimeter(); 27 } 28 29 public class Square : Shape 30 { 31 public double Height 32 { 33 get; 34 set; 35 } 36 37 public double Width 38 { 39 get; 40 set; 41 } 42 43 public Square(double height, double width) 44 { 45 this.Height = height; 46 this.Width = width; 47 } 48 49 public override double GetArea() 50 { 51 return this.Height * this.Width; 52 } 53 54 public override double GetPerimeter() 55 { 56 return (this.Height + this.Width) * 2; 57 } 58 } 59 60 61 public class Circle : Shape 62 { 63 public double R 64 { 65 get; 66 set; 67 } 68 public Circle(double r) 69 { 70 this.R = r; 71 } 72 73 public override double GetArea() 74 { 75 return Math.PI * this.R * this.R; 76 } 77 78 public override double GetPerimeter() 79 { 80 return 2 * Math.PI * this.R; 81 } 82 } 83 }
12字符串
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _12字符串 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //string s1 = "张三";//GC 14 //s1 = null; 15 //if (string.IsNullOrEmpty(s1)) 16 //{ 17 // Console.WriteLine("是的"); 18 //} 19 //else 20 //{ 21 // Console.WriteLine("不是"); 22 //} 23 24 //string s = "abcdefg"; 25 //char[] chs = s.ToCharArray(); 26 //chs[0] = ‘b‘; 27 //s = new string(chs); 28 //Console.WriteLine(s); 29 ////s[0]=‘b‘; 30 //Console.WriteLine(s[0]); 31 //Console.ReadKey(); 32 33 //string s1 = "c#"; 34 //string s2="C#"; 35 //if (s1.Equals(s2,StringComparison.OrdinalIgnoreCase)) 36 //{ 37 // Console.WriteLine("相同"); 38 //} 39 //else 40 //{ 41 // Console.WriteLine("不相同"); 42 //} 43 //Console.ReadKey(); 44 45 //string s = "abcdefg"; 46 //string sNew = s.Substring(3,1); 47 //Console.WriteLine(sNew); 48 //Console.ReadKey(); 49 50 //string str = "abcd , , fd, fdafd [[ ---"; 51 //string[] strNew = str.Split(new char[] { ‘,‘, ‘ ‘, ‘[‘, ‘-‘ }, StringSplitOptions.RemoveEmptyEntries); 52 53 54 //string[] names = { "张三", "李四", "王五", "赵六" }; 55 //string s = string.Join("|",‘c‘,true,3.13,100,1000m,"张三"); 56 //Console.WriteLine(s); 57 //Console.ReadKey(); 58 59 string str = "老赵伟大"; 60 str = str.Replace("老赵", "**"); 61 Console.WriteLine(str); 62 Console.ReadKey(); 63 } 64 } 65 }
13、字符串练习
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace _13_字符串练习 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //课上练习1:接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba" 15 16 //string str = "abcdefg"; 17 //char[] chs = new char[str.Length]; 18 19 //foreach (var item in chs) 20 //{ 21 // Console.WriteLine(item); 22 //} 23 //Console.ReadKey(); 24 25 //Console.WriteLine("请输入一个字符串"); 26 //string str = Console.ReadLine(); 27 //char[] chs = new char[str.Length]; 28 //int i = 0; 29 //foreach (var item in str) 30 //{ 31 // chs[i] = item; 32 // i++; 33 //} 34 //foreach (var item in chs) 35 //{ 36 // Console.WriteLine(item); 37 //} 38 //Console.ReadKey(); 39 //char[] chs = str.ToCharArray(); 40 //for (int i = 0; i < chs.Length/2; i++) 41 //{ 42 // char temp = chs[i]; 43 // chs[i] = chs[chs.Length - 1 - i]; 44 // chs[chs.Length - 1 - i] = temp; 45 //} 46 47 //str = new string(chs); 48 //Console.WriteLine(str); 49 50 //Array.Reverse(chs); 51 //str = new string(chs); 52 //Console.WriteLine(str); 53 54 ////课上练习2:接收用户输入的一句英文,将其中的单词以反序输出。 “I love you"→“i evol uoy" 55 //string str = "I Love You";// I evoL uoY 56 //string s2 = null; 57 //string[] strNew = str.Split(new char[] { ‘ ‘ }, StringSplitOptions.RemoveEmptyEntries); 58 //for (int i = 0; i < strNew.Length; i++) 59 //{ 60 // string s = ProcessStr(strNew[i]); 61 // s2 += s+" "; 62 63 //} 64 //Console.WriteLine(s2); 65 66 //Console.ReadKey(); 67 68 //课上练习3:”2012年12月21日”从日期字符串中把年月日分别取出来,打印到控制台 69 //string date = "2012年12月21日"; 70 //string[] dateNew = date.Split(new char[] { ‘年‘, ‘月‘, ‘日‘ }, StringSplitOptions.RemoveEmptyEntries); 71 //Console.WriteLine("{0}--{1}--{2}",dateNew[0],dateNew[1],dateNew[2]); 72 //Console.ReadKey(); 73 74 75 76 77 // 练习5:123-456---789-----123-2把类似的字符串中重复符号去掉, 78 //既得到123-456-789-123-2. split()、 79 //string str = "123-456---789-----123-2"; 80 //string[] strNew = str.Split(new char[] { ‘-‘ }, StringSplitOptions.RemoveEmptyEntries); 81 //str = string.Join("-", strNew); 82 //Console.WriteLine(str); 83 //Console.ReadKey(); 84 85 string[] str = File.ReadAllLines("员工工资.txt", Encoding.Default); 86 int max = int.MinValue; 87 int min = int.MaxValue; 88 int sum = 0; 89 for (int i = 0; i < str.Length; i++) 90 { 91 //张三,100 92 string[] strNew = str[i].Split(new char[] { ‘,‘ }, StringSplitOptions.RemoveEmptyEntries); 93 if (int.Parse(strNew[1]) > max) 94 { 95 max = int.Parse(strNew[1]); 96 } 97 if (int.Parse(strNew[1]) < min) 98 { 99 min = int.Parse(strNew[1]); 100 } 101 sum += int.Parse(strNew[1]); 102 } 103 Console.WriteLine("工资最高是{0},最低是{1},平均薪资是{2}",max,min,sum/str.Length); 104 105 106 Console.ReadKey(); 107 108 109 } 110 111 112 public static string ProcessStr(string str) 113 { 114 char[] chs = str.ToCharArray(); 115 for (int i = 0; i < chs.Length / 2; i++) 116 { 117 char temp = chs[i]; 118 chs[i] = chs[chs.Length - 1 - i]; 119 chs[chs.Length - 1 - i] = temp; 120 } 121 return new string(chs); 122 } 123 } 124 }
14、高效的StringBuilder
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _14_高效的StringBuilder 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 StringBuilder sb = new StringBuilder(); 14 sb.Append("张三"); 15 sb.Append("李四"); 16 sb.Append("王五"); 17 sb.Insert(1, 123); 18 sb.Replace("李四", "颜世伟"); 19 sb.Replace("颜世伟", "杀马特"); 20 Console.WriteLine(sb.ToString()); 21 Console.ReadKey(); 22 } 23 } 24 }
15、使用StringBuilder来拼接网页
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace _15_使用StringBuilder来拼接网页 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void button1_Click(object sender, EventArgs e) 21 { 22 23 StringBuilder sb = new StringBuilder(); 24 sb.Append("<html>"); 25 sb.Append("<head>"); 26 sb.Append("</head>"); 27 sb.Append("<body>"); 28 sb.Append("<table border=‘1px‘ cellpadding=‘0px‘ cellspacing=‘0px‘>"); 29 sb.Append("<tr>"); 30 sb.Append("<td>星期一</td>"); 31 sb.Append("<td>星期一</td>"); 32 sb.Append("<td>星期一</td>"); 33 sb.Append("<td>星期一</td>"); 34 sb.Append("</tr>"); 35 sb.Append("<tr>"); 36 sb.Append("<td>星期一</td>"); 37 sb.Append("<td>星期一</td>"); 38 sb.Append("<td>星期一</td>"); 39 sb.Append("<td>星期一</td>"); 40 sb.Append("</tr>"); 41 sb.Append("<tr>"); 42 sb.Append("<td>星期一</td>"); 43 sb.Append("<td>星期一</td>"); 44 sb.Append("<td>星期一</td>"); 45 sb.Append("<td>星期一</td>"); 46 sb.Append("</tr>"); 47 sb.Append("<tr>"); 48 sb.Append("<td>星期一</td>"); 49 sb.Append("<td>星期一</td>"); 50 sb.Append("<td>星期一</td>"); 51 sb.Append("<td>星期一</td>"); 52 sb.Append("</tr>"); 53 sb.Append("</table>"); 54 sb.Append("</body>"); 55 sb.Append("</html>"); 56 webBrowser1.DocumentText = sb.ToString(); 57 // webBrowser1.DocumentText 58 } 59 } 60 }
1 namespace _15_使用StringBuilder来拼接网页 2 { 3 partial class Form1 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.webBrowser1 = new System.Windows.Forms.WebBrowser(); 32 this.button1 = new System.Windows.Forms.Button(); 33 this.SuspendLayout(); 34 // 35 // webBrowser1 36 // 37 this.webBrowser1.Location = new System.Drawing.Point(68, 73); 38 this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); 39 this.webBrowser1.Name = "webBrowser1"; 40 this.webBrowser1.Size = new System.Drawing.Size(439, 250); 41 this.webBrowser1.TabIndex = 0; 42 // 43 // button1 44 // 45 this.button1.Location = new System.Drawing.Point(68, 23); 46 this.button1.Name = "button1"; 47 this.button1.Size = new System.Drawing.Size(75, 23); 48 this.button1.TabIndex = 1; 49 this.button1.Text = "button1"; 50 this.button1.UseVisualStyleBackColor = true; 51 this.button1.Click += new System.EventHandler(this.button1_Click); 52 // 53 // Form1 54 // 55 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 56 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 57 this.ClientSize = new System.Drawing.Size(676, 416); 58 this.Controls.Add(this.button1); 59 this.Controls.Add(this.webBrowser1); 60 this.Name = "Form1"; 61 this.Text = "Form1"; 62 this.ResumeLayout(false); 63 64 } 65 66 #endregion 67 68 private System.Windows.Forms.WebBrowser webBrowser1; 69 private System.Windows.Forms.Button button1; 70 } 71 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace _15_使用StringBuilder来拼接网页 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 应用程序的主入口点。 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 Application.EnableVisualStyles(); 18 Application.SetCompatibleTextRenderingDefault(false); 19 Application.Run(new Form1()); 20 } 21 } 22 }
16、集合复习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Collections; 7 8 namespace _16_集合复习 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 // ArrayList list = new ArrayList(); 15 //// list.Add() 16 // Hashtable ht = new Hashtable(); 17 // //ht.Add() 18 19 // List<int> list = new List<int>(); 20 //list.Add(); 添加单个元素 21 //list.AddRange();添加集合 22 //list.Insert();插入 23 //list.InsertRange();插入集合 24 //list.Remove();移除 25 //list.RemoveAt();根据下标移除 26 //list.RemoveRange();移除一定范围的元素 27 //list.Contains();//判断是否包含 28 29 // list.RemoveAll() 30 31 //Dictionary<int, string> dic = new Dictionary<int, string>(); 32 //dic.Add(1,"张三"); 33 //dic.Add(2,"李四"); 34 //dic.Add(3, "颜世伟"); 35 //dic.Add(4, "杀马特"); 36 //dic[4] = "还是杀马特"; 37 38 //foreach (KeyValuePair<int,string> kv in dic) 39 //{ 40 // Console.WriteLine("{0}---{1}",kv.Key,kv.Value); 41 //} 42 //Console.ReadKey(); 43 44 //dic.ContainsKey(); 45 46 47 } 48 } 49 }
17集合练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _17集合练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 #region 集合练习1 14 //案例:把分拣奇偶数的程序用泛型实现。int[] nums={1,2,3,4,5,6,7,8,9};奇数在左边 偶数在右边 15 //int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 16 //List<int> listJi = new List<int>(); 17 //List<int> listOu = new List<int>(); 18 //for (int i = 0; i < nums.Length; i++) 19 //{ 20 // if (nums[i] % 2 == 0) 21 // { 22 // listOu.Add(nums[i]); 23 // } 24 // else 25 // { 26 // listJi.Add(nums[i]); 27 // } 28 //} 29 30 //listJi.AddRange(listOu); 31 //foreach (var item in listJi) 32 //{ 33 // Console.WriteLine(item); 34 //} 35 //Console.ReadKey(); 36 #endregion 37 38 39 40 41 #region 集合练习2 42 //练习1:将int数组中的奇数放到一个新的int数组中返回。 43 //将数组中的奇数取出来放到一个集合中,最终将集合转换成数组 。 44 //int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 45 46 //List<int> listJi = new List<int>(); 47 48 //for (int i = 0; i < nums.Length; i++) 49 //{ 50 // if (nums[i] % 2 != 0) 51 // { 52 // listJi.Add(nums[i]); 53 // } 54 //} 55 ////集合转换成数组 56 //int[] numsNew = listJi.ToArray(); 57 //foreach (var item in numsNew) 58 //{ 59 // Console.WriteLine(item); 60 //} 61 //Console.ReadKey(); 62 #endregion 63 64 65 66 67 #region 集合练习3 68 //练习2:从一个整数的List<int>中取出最大数(找最大值)。 69 //集合初始化器 70 //List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 71 //int max = list[0]; 72 //for (int i = 0; i < list.Count; i++) 73 //{ 74 // if (list[i] > max) 75 // { 76 // max = list[i]; 77 // } 78 //} 79 //Console.WriteLine(max); 80 //Console.ReadKey(); 81 ////foreach (var item in list) 82 ////{ 83 //// Console.WriteLine(item); 84 ////} 85 //Console.ReadKey(); 86 // list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); 87 //Person p = new Person("李四", 16, ‘女‘) { Name = "张三", Age = 19, Gender = ‘男‘ }; 88 //p.SayHello(); 89 //Console.ReadKey(); 90 #endregion 91 92 93 94 95 96 #region 集合练习4 97 //练习:把123转换为:壹贰叁。Dictionary<char,char> 98 //"1一 2二 3三 4四 5五 6六 7七 8八 9九" 99 //string str = "1一 2二 3三 4四 5五 6六 7七 8八 9九"; 100 ////123 一二三 101 //Dictionary<char, char> dic = new Dictionary<char, char>(); 102 //string[] strNew = str.Split(new char[] { ‘ ‘ }, StringSplitOptions.RemoveEmptyEntries); 103 104 //for (int i = 0; i < strNew.Length; i++) 105 //{ 106 // //1一 strNew[i][0] strNew[i][1] 107 // dic.Add(strNew[i][0], strNew[i][1]); 108 //} 109 110 //Console.WriteLine("请输入阿拉伯数字"); 111 //string input = Console.ReadLine(); 112 113 //for (int i = 0; i < input.Length; i++) 114 //{ 115 // if (dic.ContainsKey(input[i])) 116 // { 117 // Console.Write(dic[input[i]]); 118 // } 119 // else 120 // { 121 // Console.Write(input[i]); 122 // } 123 //} 124 //Console.ReadKey(); 125 #endregion 126 127 128 129 #region 集合练习5 130 ////练习:计算字符串中每种字符出现的次数(面试题)。 “Welcome to Chinaworld”,不区分大小写,打印“W2”“e 2”“o 3”…… 131 132 //string s = "Welcome to Chinaworld"; 133 134 //Dictionary<char, int> dic = new Dictionary<char, int>(); 135 ////遍历 s 136 //for (int i = 0; i < s.Length; i++) 137 //{ 138 // if (s[i] == ‘ ‘) 139 // { 140 // continue; 141 // } 142 // if (dic.ContainsKey(s[i])) 143 // { 144 // dic[s[i]]++; 145 // } 146 // else 147 // { 148 // dic[s[i]] = 1; 149 // } 150 //} 151 152 153 //foreach (KeyValuePair<char,int> kv in dic) 154 //{ 155 // Console.WriteLine("字母{0}出现了{1}次",kv.Key,kv.Value); 156 //} 157 //Console.ReadKey(); 158 #endregion 159 160 161 162 #region 集合练习5 163 //案例:两个(List)集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。 164 //List<string> listOne = new List<string>() { "a", "b", "c", "d", "e" }; 165 //List<string> listTwo = new List<string>() { "d", "e", "f", "g", "h" }; 166 //for (int i = 0; i < listTwo.Count; i++) 167 //{ 168 // if (!listOne.Contains(listTwo[i])) 169 // { 170 // listOne.Add(listTwo[i]); 171 // } 172 //} 173 174 175 //foreach (var item in listOne) 176 //{ 177 // Console.WriteLine(item); 178 //} 179 //Console.ReadKey(); 180 #endregion 181 182 } 183 } 184 185 public class Person 186 { 187 public Person(string name, int age, char gender) 188 { 189 this.Name = name; 190 this.Age = age; 191 this.Gender = gender; 192 } 193 194 public string Name 195 { 196 get; 197 set; 198 } 199 public char Gender 200 { 201 get; 202 set; 203 } 204 public int Age 205 { 206 get; 207 set; 208 } 209 210 public void SayHello() 211 { 212 Console.WriteLine("{0}---{1}---{2}", this.Name, this.Age, this.Gender); 213 } 214 } 215 }
18、静态和非静态的区别
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _18_静态和非静态的区别 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Student.Test(); 14 Student.Test(); 15 Student.Test(); 16 Console.ReadKey(); 17 } 18 } 19 20 public class Person 21 { 22 private static string _name; 23 private int _age; 24 public void M1() 25 { 26 27 } 28 29 public static void M2() 30 { 31 32 } 33 34 35 public Person() 36 { 37 Console.WriteLine("非静态类构造函数"); 38 } 39 40 } 41 42 public static class Student 43 { 44 static Student() 45 { 46 Console.WriteLine("静态类构造函数"); 47 } 48 49 public static void Test() 50 { 51 Console.WriteLine("我是静态类中的静态方法"); 52 } 53 // private string _name; 54 } 55 }
19、结构和类的区别
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _19_结构和类的区别 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //类型 14 //结构:值类型 15 //类:引用类型 16 17 //声明的语法:class struct 18 19 //在类中,构造函数里,既可以给字段赋值,也可以给属性赋值。构造函数是可以重载的 20 //但是,在结构的构造函数当中,必须只能给字段赋值。 21 //在结构的构造函数当中,我们需要给全部的字段赋值,而不能去选择的给字段赋值 22 23 //调用: 24 25 PersonClass pc = new PersonClass(); 26 27 28 //结构是否可以New? 29 //在栈开辟空间 结构new 调用结构的构造函数 30 PersonStruct ps = new PersonStruct(); 31 ps.M2(); 32 PersonStruct.M1(); 33 Console.ReadKey(); 34 //结构和类的构造函数: 35 //相同点:不管是结构还是类,本身都会有一个默认的无参数的构造函数 36 //不同点:当你在类中写了一个新的构造函数之后,那个默认的无参数的构造函数都被干掉了 37 //但是,在结构当中,如果你写了一个新的构造函数,那么个默认的无参数的构造函数依然在。 38 // 39 //如果我们只是单纯的存储数据的话,我们推荐使用结构。 40 41 //如果我们想要使用面向对象的思想来开发程序,我们推荐使用我们的Class 42 43 //结构并不具备面向对象的特征 44 45 46 // int 47 } 48 } 49 50 public class PersonClass 51 { 52 //字段、属性、方法、构造函数 53 } 54 55 public struct PersonStruct 56 { 57 //字段、属性 58 private string _name; 59 public string Name 60 { 61 get { return _name; } 62 set { _name = value; } 63 } 64 65 private int _age; 66 public int Age 67 { 68 get { return _age; } 69 set { _age = value; } 70 } 71 72 private char _gender; 73 public char Gender 74 { 75 get { return _gender; } 76 set { _gender = value; } 77 } 78 79 public static void M1() 80 { 81 Console.WriteLine("我是结构中的静态方法"); 82 } 83 public void M2() 84 { 85 Console.WriteLine("我是结构的非静态方法"); 86 } 87 88 public PersonStruct(string name, int age, char gender) 89 { 90 //this.Name = name; 91 //this.Age = age; 92 //this.Gender = gender; 93 94 this._name = name; 95 this._age = age; 96 this._gender = gender; 97 } 98 99 100 //public PersonStruct(string name) 101 //{ 102 // this._name = name; 103 //} 104 105 } 106 }