1.重载,递归,字符串的优化
using System;
using System.Text;namespace Day11_1
{class MathTool{public int PlusNumbers(int a, int b){return a + b;}public int PlusNumbers(int a, int b,int c = 0){return a + b + c;}public float PlusNumbers(float a, float b){return a + b;}/// <summary>/// 斐波那契数列/// </summary>/// <param name="a"></param>/// <param name="b"></param>/// <returns></returns>public long Fib(long index){if (index == 1)return 0;else if (index ==2)return 1;return Fib(index - 1) + Fib(index - 2);}}class Circle{private string myName;public string yourName;public string MyName{get => myName;set => myName = value;}public string YourName{get => yourName;set => yourName = value;}public void ShowMe(){Console.WriteLine($"{myName}");}public void ShowYou(){Console.WriteLine($"{yourName}");}}class Program{static void Main(string[] args){#region 字符串的优化/*Weapon wjzr = new Weapon();Console.WriteLine(wjzr);string stri = new string("sdf");string str = "";StringBuilder stringBuilder = new StringBuilder();stringBuilder.Append("abc");Console.WriteLine(stringBuilder);stringBuilder.Append("def");stringBuilder.Clear();//清空字符串stringBuilder.Append("你好吗?");Console.WriteLine(stringBuilder);*/#endregion#region 重载/*MathTool mathTool = new MathTool();//1.参数的类型不同//2.参数的个数不同//3.参数的类型和个数不同//返回值类型不同无法使用重载mathTool.PlusNumbers(1, 2);mathTool.PlusNumbers(1, 2, 4);//有默认值,输入两个实参,就会调用两个参数的方法,系统会警告mathTool.PlusNumbers(1.5f, 1.5f);Console.WriteLine(1);Console.WriteLine(1.5f);Console.WriteLine("abc");Console.WriteLine();*/#endregion#region 递归//1.自己调用自己,一定要有出口,否则死循环//2.互相调用Circle circle = new Circle();circle.MyName = "小圆圈";circle.YourName = "大圆圈";circle.ShowMe();circle.ShowYou();//【练习】//斐波那契数列//0 1 1 2 3 5 8 ......MathTool newMath = new MathTool();Console.WriteLine($"{newMath.Fib(43)}");#endregion}}
}
2.构造函数、析构函数
using System;namespace Day11_2
{/// <summary>/// 结构体/// </summary>struct Student{private String name;private byte age;public string address;//结构体的有参构造,必须给当前结构体所有字段赋初值public Student(string name, byte age,string address){this.name = name;this.age = age;this.address = address;}}class Person{//字段private string school = "清华大学" ;private char sex;private string name = "一朵花";public float[] scores; //数组,不new得话,会报空引用异常public Person Friend;//类的构造函数//构造函数1public Person(){scores = new float[5];Console.WriteLine("一个人出生了...");}//构造函数2public Person(string name):this(){this.name = name;}//构造函数3public Person(string name, char sex) : this(name){this.sex = sex;}//构造函数4//调用其他的构造函数public Person(string name, char sex, string school):this(name,sex){this.school = school;}public Person(Person friend) : this(){this.Friend = friend;}}class Friend{public string hobby;//如果一个类中一个构造函数都没有,系统会自动创造一个无参无方法体的构造函数//如果你创造了构造函数,系统则不会给你创建构造函数public Friend(){}public Friend(string hobby){this.hobby = hobby;}~Friend(){Console.WriteLine("我自爆了...");}}class Program{static void Main(string[] args){#region 构造函数 ------对象创建的时候/*//没有返回值一说//默认构造函数//构造函数的重载//结构体对象//结构体默认有一个无参无方法体(或者说方法体为空)的构造函数//程序员无法在创建一个无参的构造函数//无论怎样的构造函数都要给结构体的所有字段赋值*********************************结构体的构造函数特点//结构体不允许设置默认值Student xiaoming = new Student();xiaoming.address = "沙河镇";//类的对象//类中也默认创建了一个无参无方法体(或者说方法体为空)的构造函数//类不一定要给全部字段赋值***********************************************与结构体构造函数的区别//类允许设置默认值,即使不设置也是根据数据类型设置默认值的Person xiaohong = new Person("小红",'M',"千峰"); //类的实例化Console.WriteLine(xiaohong.scores.Length);Console.WriteLine(xiaohong.Friend);*/#endregion#region 析构函数 -------对象销毁的时候//如何让一个对象销毁呢?Friend gf = new Friend();//手动回收垃圾GC.Collect();#endregion}}
}