➊ 数组
/* 2023-11-03
* 5. 數組
* 6. 字符串
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
/*---5 數組---*/
//一維數組
/*int[] day = new int[] { 31, 28, 31, 31, 31, 31, 30, 30, 30, 30, 31, 31 };
for(int i = 0; i < 12; i++)
{
Console.WriteLine((i + 1) + "有" + day[i] + "天");
}
Console.ReadLine();*/
//二維數組
/*Console.Title = "車票系統";
string[,] array = new string[9,4];
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 4; j++)
{
array[i, j] = " 有票 ";
}
}
string s = string.Empty;
while (true)
{
Console.Clear();
Console.WriteLine("\n 車票系統"+"\n");
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 4; j++)
{
System.Console.Write(array[i, j]);
}
Console.WriteLine();
}
Console.Write("input:");
s = Console.ReadLine();
if (s == "q") break;//輸入q退出
string[] ss = s.Split(',');//複製 別輸入
int one = int.Parse(ss[0]);
int two = int.Parse(ss[1]);
array[one, two] = " 已售 ";
}*/
//楊輝三角形
/*int[][] Array_int = new int[10][];
for (int i = 0; i < Array_int.Length; i++)//i行j列
{
Array_int[i] = new int[i + 1];
for(int j = 0; j < Array_int[i].Length; j++)
{
if (i <= 1)
{
Array_int[i][j] = 1;
continue;
}
else
{
if (j == 0 || j == Array_int[i].Length - 1)
Array_int[i][j] = 1;//從0開始所以減一
else
Array_int[i][j] = Array_int[i - 1][j - 1] + Array_int[i-1][j];
}
}
}
for (int i = 0; i <= Array_int.Length-1; i++)
{
for(int k=0;k<=Array_int.Length- i; k++)
{
Console.Write(" ");
}
for(int j = 0; j < Array_int[i].Length; j++)
{
Console.Write("{0} ", Array_int[i][j]);
}
Console.WriteLine();
}
Console.ReadLine();*/
//foreach 遍歷數組
/*string[] roles = { "1", "w", "2", "r" };
foreach(string role in roles)
{
Console.Write(role + " ");
}
Console.ReadLine();*/
//sort對一維數組進行排序
/*int[] array = new int[] { 1, 4, 2, 7, 5, 9, 3 };
Array.Sort(array);
foreach(var item in array)
{
Console.Write(item+" ");
}
Console.ReadLine();*/
//reverse對一維數組進行反轉
/*int[] array = new int[] { 1, 4, 2, 7, 5, 9, 3 };
Array.Reverse(array);
foreach(var item in array)
{
Console.Write(item+" ");
}
Console.ReadLine();*/
}
}
}
➋ 字符串
/* 2023-11-03
* 5. 數組
* 6. 字符串
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
/*---6 字符串---*/
/*string str = "sjdcbshgad";//字符串,初始化
char[] array = { 't', 'i', 'm', 'e' };//字符數組
string str1 = new string(array);//數組初始化
string str2 = new string(array,1,2);//數組一部分初始化
Console.WriteLine(str.Length);//length獲取字符長度
Console.WriteLine(str[5]);//獲取字符串中的内容,索引5 位置6
Console.WriteLine(str.IndexOf('s'));//indexof獲取字符在字符串中首次出現的位置索引
Console.WriteLine(str.LastIndexOf('s')); //lastindexof獲取字符在字符串中末次出現的位置索引
//查找字符在字符串中出現的位置索引,下一次,把位置拉到上一次的後面
Console.WriteLine(str.IndexOf('s',0+1));//上一次是索引值是0
bool result = str.StartsWith("sjdc");//判斷是否是首内容
bool result1 = str.EndsWith("sjdc");//判斷是否是尾内容
Console.WriteLine(result);
Console.WriteLine(result1);
Console.WriteLine(str+str);//+ 字符串拼接
Console.ReadLine();*/
//字符串比較 例子-用戶密碼
/*Console.Write("input name:");
string name = Console.ReadLine();
Console.Write("input password:");
string password = Console.ReadLine();
Console.Write("input password1:");
string password1 = Console.ReadLine();
if (name == "mm" && password.Equals("kk") && Equals(password1, "yy"))
{
Console.WriteLine("welcome");
}
else
{
Console.WriteLine("---");
}
Console.ReadLine();*/
//數據格式化string.format 【c貨幣 d e指數型 f n p百分比 x十六進制】,大小寫都可,後跟數字是保留位數
/*Console.WriteLine(string.Format("{0:c}",12));
Console.WriteLine(string.Format("{0:E}", 12.1));
Console.WriteLine(string.Format("{0:N0}", 12));
Console.WriteLine(string.Format("{0:F3}", Math.PI));
Console.WriteLine(string.Format("{0:x}", 12));
Console.WriteLine(string.Format("{0:P1}", 0.0112));
Console.ReadLine();*/
//數據格式化 例子-輸出當前日期
/*DateTime strDate = DateTime.Now;
Console.WriteLine(string.Format("{0:d}", strDate));
Console.WriteLine(string.Format("{0:D}", strDate));
Console.WriteLine(string.Format("{0:f}", strDate));
Console.WriteLine(string.Format("{0:F}", strDate));
Console.WriteLine(string.Format("{0:g}", strDate));
Console.WriteLine(string.Format("{0:G}", strDate));
Console.WriteLine(string.Format("{0:t}", strDate));
Console.WriteLine(string.Format("{0:T}", strDate));
Console.WriteLine(string.Format("{0:M}", strDate));
Console.WriteLine(string.Format("{0:Y}", strDate));
Console.ReadLine();*/
/*結果:
2023/11/3
2023年11月3日
2023年11月3日 上午 10:55
2023年11月3日 上午 10:55:23
2023 / 11 / 3 上午 10:55
2023 / 11 / 3 上午 10:55:23
上午 10:55
上午 10:55:23
11月3日
2023年11月*/
//字符串截取substring
/*string str = "12.cs";
Console.WriteLine(str);
Console.WriteLine(str.Substring(0,str.LastIndexOf('.')));//如果是後綴的話用last比較好
Console.WriteLine(str.Substring(str.LastIndexOf('.')));
Console.ReadLine();*/
//字符串分割split(分割后作爲分隔符的那個字符會沒有)
/*string str = "12. .cs..1";
string[] splitstr = str.Split('.');
for (int i = 0; i < splitstr.Length; i++)
{
Console.WriteLine(splitstr[i]);
}
Console.ReadLine();*/
/*
string str = "12. .cs..1";
//string[] splitstr = str.Split('.');
//StringSplitOptions.RemoveEmptyEntries省略空數組
char[] seporator = { '.' };
string[] splitstr = str.Split(seporator, StringSplitOptions.RemoveEmptyEntries);//whitespace不算空
for (int i = 0; i < splitstr.Length; i++)
{
Console.WriteLine(splitstr[i]);
}
Console.ReadLine();*/
//trim 去除首尾内容
/*string str = " ***12shg ***";
Console.WriteLine(str.Trim('*'));//首部是whitespace,尾部是***,他去掉的是首位部的***
Console.WriteLine(str.Trim());//去掉首位部的whitespace
Console.ReadLine();*/
//替換字符replace
/*string str = " ***12shg ***";
Console.WriteLine(str.Replace('*','-'));
Console.WriteLine(str.Replace(" ***",""));
Console.ReadLine();*/
//stringbuilder類-追加/移除内容
}
}
}
➌ 对象
3.1 类封装数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
//类封装数据
static double r;//半径
const double PI = 3.14;
static void Main(string[] args)
{
Console.Write("input r = ");
Program.r = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("圆的面积 = " + PI*Math.Pow(r,2));
Console.ReadLine();
}
}
}
3.2 属性限制值的范围
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
//属性限制值的范围
private int age;
public int Age
{
get
{
return age;
}
set
{
if (value > 0 && value < 70)
{
age = value;
}
else
{
Console.WriteLine("不合理,请重设。");
}
}
}
static void Main(string[] args)
{
Program p = new Program();
while (true)
{
Console.Write("input your retired age:");
p.Age = Convert.ToInt16(Console.ReadLine());
}
}
}
}
3.3 构造函数和析构函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
//静态构造函数,只在引用类之前执行了一次
static Program()
{
Console.WriteLine("static");
}
//实例构造函数,每创建一个对象都会执行一次
private Program()
{
Console.WriteLine("实例构造函数");
}
static void Main(string[] args)
{
Program p1 = new Program();
Program p2 = new Program();
Program p3 = new Program();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
//静态构造函数,只在引用类之前执行了一次
static Program()
{
Console.WriteLine("static");
}
//实例构造函数,每创建一个对象都会执行一次
private Program()
{
Console.WriteLine("实例构造函数");
}
//析构函数,主要用来释放对象资源,不需要开发人员显式定义
//一般,对象的生命周期从构造函数开始,以析构函数结束
//我录了个屏 从中看到释放资源之前static确实是执行了的 实例构造函数好像就没有运行了
~Program()
{
Console.WriteLine("析构函数");
}
static void Main(string[] args)
{
Program p1 = new Program();
Program p2 = new Program();
Program p3 = new Program();
}
}
}
3.4 权限修饰符
private
public
范围不同:private本类,public任何代码都可以访问protected
internal
protected internal
3.5 不同类型参数方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
private int Add(int x,int y)//值参数,形参修改不影响实参
{
x = x + y;
return x;
}
private int Add(ref int x, int y)//ref参数,会影响实参
{
x = x + y;
return x;
}
private int Add(int x, int y,out int z)//out参数
{
z = x + y;
return z;
}
private int Add(params int[] x)//params参数
{
int result = 0;
for(int i = 0; i < x.Length; i++)
{
result += x[i];
}
return result;
}
static void Main(string[] args)
{
Program p = new Program();
int x = 3;
int y = 4;
int z;
Console.WriteLine(p.Add(x, y));//x=3
Console.WriteLine(p.Add(ref x, y));//input x=3,then output x=7
Console.WriteLine(p.Add(x, y,out z));//接上,这里的x已经是3+4了
Console.WriteLine(p.Add(2,3,4,5,6));
Console.ReadLine();
}
}
}
3.6 方法的重载
👉类中有2个及以上的同名方法(参数类型、顺序不同)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
public static int Add(int x,int y)
{
return x + y;
}
public double Add(int x, double y)
{
return x + y;
}
public int Add(int x, int y,int z)
{
return x + y+ z;
}
static void Main(string[] args)
{
Program p = new Program();
int x = 3;
int y = 4;
double y1 = 4.0;
int z = 7;
Console.WriteLine(Program.Add(x, y));
Console.WriteLine(p.Add(x, y1));
Console.WriteLine(p.Add(x, y,z));
Console.ReadLine();
}
}
}
3.7 静态方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
public static int Add(int x,int y)
{
return x + y;
}
static void Main(string[] args)
{
/*int x = 1;
int y = 2;
Console.WriteLine(Program.Add(x, y));*/
Console.WriteLine("{0}",Program.Add(1, 2));
Console.ReadLine();
}
}
}
3.8 例子-输出库存商品名
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
public class cStockInfo //创建cStockInfo类
{
public string FullName
{
get;
set;
}
public void ShowGoods()
{
Console.WriteLine("库存商品名:");
Console.WriteLine(FullName);
}
}
class Program
{
static void Main(string[] args)
{
cStockInfo stock = new cStockInfo();
stock.FullName = "脑脑";
stock.ShowGoods();
Console.ReadLine();
}
}
}
3.9 继承
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Goods //商品
{
public string TradeCode {get;set;} // 商品编号
public string FullName { get; set; } // 商品名称
}
class Buy:Goods //进货,继承Goods类, :标识继承关系
{
public string BuyTradeCode { get; set; } // 进货商品编号
public void ShowInfo()
{
Console.WriteLine("进货编号 {0}\n商品编号 {1}\n商品名称 {2}\n",BuyTradeCode,TradeCode,FullName);
}
}
class Program
{
static void Main(string[] args)
{
Buy stock = new Buy();
stock.TradeCode = "T001";
stock.FullName = "脑脑";
stock.BuyTradeCode = "i12";
stock.ShowInfo();
Console.ReadLine();
}
}
}
3.10 base调用父类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class X
{
public string sayHello() {return "hello";}
}
class Y:X
{
public new string sayHello() { return "hi "+base.sayHello(); } //用了new关键字来隐藏父类的方法,base调用父类方法
}
class Program
{
static void Main(string[] args)
{
X x = new X();
Y y = new Y();
Console.WriteLine("x:"+x.sayHello());
Console.WriteLine("y:"+y.sayHello());
Console.ReadLine();
}
}
}
3.11 虚方法的重写
virtual+override
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Vehicle
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
public virtual void Move()
{
Console.WriteLine("{0}都可以移动", Name);
}
}
class Train : Vehicle
{
public override void Move()
{
Console.WriteLine("{0}在铁轨上行驶", Name);
}
}
class Car : Vehicle
{
public override void Move()
{
Console.WriteLine("{0}在公路上行驶", Name);
}
}
class Program
{
static void Main(string[] args)
{
Vehicle vehicle = new Vehicle();
Train train = new Train();
Car car = new Car();
Vehicle[] vehicles = { vehicle, train, car };//使用基类和派生类对象创建数组
vehicle.Name = "交通工具";
train.Name = "火车";
car.Name = "汽车";
vehicles[0].Move();//交通工具都可以移动
vehicles[1].Move();//火车在铁轨上行驶
vehicles[2].Move();//汽车在公路上行驶
Console.ReadLine();
}
}
}
3.12 抽象类和抽象方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
public abstract class Market
{
public string Name { get; set; }
public string Goods { get; set; }
public abstract void Shop();
}
public class WallMarket : Market
{
public override void Shop()
{
Console.WriteLine(Name+"买"+Goods);
}
}
public class taobao : Market
{
public override void Shop()
{
Console.WriteLine(Name + "买" + Goods);
}
}
class Program
{
static void Main(string[] args)
{
Market market = new WallMarket();
market.Name = "沃尔玛";
market.Goods = "7";
market.Shop();//沃尔玛买7
market = new taobao();
market.Name = "淘宝";
market.Goods = "h";
market.Shop();//淘宝买h
Console.ReadLine();
}
}
}
3.13 接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
interface IPerson
{
string Name { get; set; }
int Age { get; set; }
void Speek();
void Work();
}
class Student : IPerson
{
public string Name { get; set; }
private int age;
public int Age
{
get { return age; }
set { if (age > 0 && age < 100) { age = value; } }
}
public void Speek()
{
Console.WriteLine(Name + ":老师好");
}
public void Work()
{
Console.WriteLine(Name + "开始记笔记");
}
}
class Teacher : IPerson
{
public string Name { get; set; }
private int age;
public int Age
{
get { return age; }
set { if (age > 0 && age < 100) { age = value; } }
}
public void Speek()
{
Console.WriteLine(Name + ":同学们好");
}
public void Work()
{
Console.WriteLine(Name + "开始讲课");
}
}
class Program
{
static void Main(string[] args)
{
IPerson[] person = new IPerson[] { new Student(), new Teacher() };
person[0].Name = "Stu.QE";
person[0].Age = 100;
person[1].Name = "Mr.PET";
person[0].Age = 10;
person[0].Speek();
person[1].Speek();
Console.WriteLine();
person[1].Work();
person[0].Work();
Console.ReadLine();
}
}
}