C#Lesson08 字符串、方法的重载和递归

string字符串


---------------------------------------------------------------------------------------------------------------------

应用

using System;
using System.Text;


namespace Lesson08_1
{
class MainClass
{
public static void Main (string[] args)
{
// string name = "前任";
// //在字符串后面拼接其他字符串
//     name = name + "2";
// Console.WriteLine (name);
//
// string name3 = string.Concat ("我叫", "王大锤");//拼接字符串
// Console.WriteLine (name3);
// char lastChar = name3 [4];
// Console.WriteLine (lastChar);


// string s1 = "1234568";
// string s2 = "12345689";
// //s1 = s2 : 0
// //s1 > s2 : 1
// //s1 < s2 : -1
// int result = string.Compare (s1, s2);
// Console.WriteLine (result);


// string s1 = "我叫,王大锤";
// Console.WriteLine (s1.Contains (","));//判断s1中是否包含某个字符串


// MyString ms = new MyString ();
// ms.TString = "我叫,王大锤";
// Console.WriteLine (ms.Contain (','));


// string s1 = "我叫,王大锤";
// Console.WriteLine (s1.Remove (2));
// Console.WriteLine (s1.Remove (2,3));//指定下标删除指定个数


// string s1 = "Hello China";
// char[] target = new char[20];
// s1.CopyTo (4,target,3,7);
// for (int i = 0; i < target.Length; i++) {
// Console.WriteLine (target[i]);
// }


// string res = string.Format ("{0:P1}--{1:C}", 123,123,554,75);
// Console.WriteLine (res);


// string s1 = "Hello China,AADFQ";
// int index = s1.IndexOf ("a");//返回A在s1中索引
// Console.WriteLine (index);


// string s1 = "hwaHaHAwhAand";
// char[] cc = { 'a', 'h' };
// //遍历s1,如果s1中的字符和cc中的某个字符相等,就直接返回这个字符在s1中的索引
// int index = s1.IndexOfAny (cc);
// Console.WriteLine (index);


// string s1 = "China";
// string s2 = s1.Insert (5, "Hello");
// Console.WriteLine (s2);




// string[] ss = {"Hello","hi","china","asd","fh"};
// string res = string.Join("??",ss);
// Console.WriteLine (res);


// string s1 = "Hello lanou,china";
// int index = s1.LastIndexOf ("a");
// Console.WriteLine (index);


// string s1 = "Hello lanou,China";
// string res = s1.PadLeft (50, '*');
// string res1 = s1.PadRight (50, '!');
// Console.WriteLine (res);
// Console.WriteLine (res1);


// string s1 = "Hello C#";
// string res = s1.Replace ("C#", "China");
// string res1 = s1.Replace ('o', 'c');
// Console.WriteLine (res1);


// string s1 = "id,name,price \n 0,titi,32,\n 1,sds,12";
// string[] ss = s1.Split ('\n');
// for (int i = 0; i < ss.Length; i++) {
// string[] sss = ss [i].Split (',');
// for (int j = 0; j < sss.Length; j++) {
// Console.WriteLine (sss[j]);
// }
// }


// string s1 = "Hello world";
// string res = s1.Substring (2, 6);
// Console.WriteLine (res);


// string so = "Hello world";
// string s1 = so.ToUpper ();//变大写
// string s2 = so.ToLower ();//变小写
// Console.WriteLine (s1);
// Console.WriteLine (s2);


// string s1 = "Hello world ";
// Console.WriteLine (s1.Length);
// //trim,将字符串后面的空格去掉
// string s2 = s1.Trim ();
// Console.WriteLine (s2.Length);


// string s = "Because of you I never stray to far from the sidewalk Because of you I learned to play on the safe side ";
// string s1 = s.Replace ("you", "lanou");
// string s2 = s.Replace ("to", "too");
// Console.WriteLine (s1);
// Console.WriteLine (s2);
// s = s.Trim ();
// string[] ss = s.Split (' ');
// Console.WriteLine (ss.Length);
//
//
// for (int i = 'z'; i >='a' ; i--) {
// char old = (char)i;
// char n = (char)(i + 1);
// s = s.Replace (old,n);
// }
//
// for (int i = 'Z'; i >='A' ; i--) {
// char old = (char)i;
// char n = (char)(i + 1);
// s = s.Replace (old,n);
// }
//
// Console.WriteLine (s);


// StringBuilder sb = new StringBuilder ("hello C#");
sb.Capacity = 5;//手动去指定当前sb存放字符的空间大小
sb.Append("ASDDF",2,3);
sb.AppendFormat("{0:C}",100);
// Console.WriteLine (sb.MaxCapacity);
// sb.Insert(sb.Length ,"CC");
// Console.WriteLine (sb);


// StringBuilder sb = new StringBuilder ("asdasgrvcxqwdxccv");
// sb.Remove (0, 5);//从0开始标记 4删除长度
// Console.WriteLine (sb);


// StringBuilder sb = new StringBuilder("你好,lanou");
// sb.Replace("lanou","China");
// Console.WriteLine (sb);
// string a = "nis";
// string b = "nis";
// bool res = a.Equals (b);
// Console.WriteLine (res);


// MyMath math = new MyMath ();
// //math.Add (1, 2, 3, 4, 5, 6, 7, 87);
// Console.WriteLine (math.Peach (1));
// Console.WriteLine (math.Sheep(1));




}
}
}

-------------------------------------------------------------------------------------------------------------------

Mymath类

using System;


namespace Lesson08_1
{
public class MyMath
{
//计算两个数的和
public float Add(float a,float b){
return a + b;
}
public int Add(int a,int b ){
return a + b;
}
public int Add(params int[] arr){
int sum = 0;
for (int i = 0; i < arr.Length; i++) {
sum += arr[i];
}
return sum;
}
//具有相同参数列表,但是返回值类型不一样的方法,不能重载
public int XXX(int a,int b){
return a * b;
}
public float XXX(float a,float b){
return a * b;
}
public double XXX(double a,double b){
return a * b;
}


public int GetN(int n ){
if (n <= 2) {
return 1;
}
int value = GetN (n - 1) + GetN (n - 2);
return value;
}


public int Peach(int n){
if (n == 7) {
return 1;
}
int count = (Peach(n + 1) + 1) * 2;
return  count;
}


public int Sheep(int n){
if (n == 7) {
return 2;
}
int nn = (Sheep (n + 1) + 1) * 3;
return nn;
}
}
}

---------------------------------------------------------------------------------------------------------------

重载

什么是方法重载?
方法重载是指在同一个类中方法同名,参数不同,调用时根据实
参的形式,选择与他匹配的方法执行操作的一种技术。
这里所说的参数不同是指以下几种情况:
1、参数的类型不同
2、参数的个数不同
3、参数的个数相同时他们的先后顺序不同


----------------------------------------------------------------------------------------------------------------------

递归

递归的概览:
函数体内调用本函数自身,知道符合某一条件不再继续调用
注:简单说就是让函数先执行到满足条件的那一步,然后带着数据开始调用函数
本身
满足的条件:
1、有反复执行的过程(调用自身)
2、有跳出反复执行过程的条件(函数出口)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值