1留凭、里氏轉(zhuǎn)換
1)然爆、子類可以賦值給父類
2)、如果父類中裝的是子類對(duì)象拖云,那么可以講這個(gè)父類強(qiáng)轉(zhuǎn)為子類對(duì)象
2那伐、類型轉(zhuǎn)換
is:表示類型轉(zhuǎn)換踏施,如果能夠轉(zhuǎn)換成功,則返回一個(gè)true罕邀,否則返回一個(gè)false
as:表示類型轉(zhuǎn)換畅形,如果能夠轉(zhuǎn)換則返回對(duì)應(yīng)的對(duì)象,否則返回一個(gè)null
ArrayList集合
集合:很多數(shù)據(jù)的一個(gè)組合
數(shù)組:長度不可變诉探、類型單一
集合的好處:長度可以任意改變 類型隨便
- 基本使用
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01ArrayList集合
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建集合對(duì)象
ArrayList list = new ArrayList();
list.Add(1);
list.Add(3.24);
list.Add(true);
list.Add("張三");
list.Add('女');
list.Add(5000m);
list.Add(new int[] { 1, 2, 4 });
Person p = new Person();
list.Add(p);
list.Add(list);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
}
//我們將一個(gè)對(duì)象輸出到控制臺(tái) 默認(rèn)情況下 打印的就是這個(gè)對(duì)象所在的類的命名空間
public class Person
{
public void sayHello()
{
Console.WriteLine("hello");
}
}
}
}
這個(gè)結(jié)果好像并不是我們想達(dá)到的日熬。這是因?yàn)?strong>我們將一個(gè)對(duì)象輸出到控制臺(tái) 默認(rèn)情況下 打印的就是這個(gè)對(duì)象所在的類的命名空間
- 我們使用里氏變化來轉(zhuǎn)換下 修改for循環(huán)中的代碼如下。
for (int i = 0; i < list.Count; i++)
{
if (list[i] is Person)
{
((Person)list[i]).sayHello();
}
else if (list[i] is int[])
{
for (int j = 0; j < ((int[])list[i]).Length; j++)
{
Console.WriteLine(((int[])list[i])[j]);
}
}
else
{
//未判斷
Console.WriteLine(list[i]);
}
}
從結(jié)果可以看出問題得到了解決阵具,當(dāng)然最后一個(gè)沒有進(jìn)行轉(zhuǎn)換碍遍。因?yàn)閷懫饋頍┒ㄍ!怕敬!?/p>
- 當(dāng)我們需要添加單個(gè)元素的時(shí)候可以用Add()方法揣炕,但當(dāng)需要添加集合的時(shí)候選用AddRange()更方便,看例子
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(3.24);
list.Add(true);
list.Add("張三");
list.Add('女');
list.Add(5000m);
//添加集合
list.AddRange(new int[] { 1, 2, 4 });
list.AddRange(list);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
}
使用AddRange()可以省略上面for循環(huán)中的一坨代碼东跪。
常用的一些方法畸陡。
//清空所有元素
list.Clear();
//刪除單個(gè)元素
list.Remove("張三");
//根據(jù)索引刪除元素
list.RemoveAt(0);
//根據(jù)下表刪除一定范圍的元素
list.RemoveRange(0, 3);
//升序排列
list.Sort();
//反轉(zhuǎn)元素
list.Reverse();
//在指定位置插入一個(gè)元素
list.Insert(1, "插入的");
//在指定位置插入集合
list.InsertRange(2, new char[
//判斷集合是否包含某個(gè)元素
bool b = list.Contains('A');
- ArrayList集合的長度問題
每次集合中實(shí)際包含的元素個(gè)數(shù)(count)超過了可以包含的元素的個(gè)數(shù)(capcity)的時(shí)候,集合就會(huì)向內(nèi)存中申請(qǐng)多開辟一倍的空間虽填,來保證集合的長度一直夠用丁恭。
HashTable (鍵值對(duì)集合)
在鍵值對(duì)集合當(dāng)中,我們是根據(jù)鍵去找值的斋日。
語法:鍵值對(duì)對(duì)象[鍵]=值;(鍵不可以重復(fù)牲览,值可以重復(fù))
鍵值對(duì)對(duì)象[鍵] = 值(鍵不存在,添加該數(shù)據(jù)恶守;鍵存在第献,覆蓋該鍵已經(jīng)存在的對(duì)應(yīng)數(shù)據(jù))
static void Main(string[] args)
{
//創(chuàng)建一個(gè)鍵值對(duì)集合對(duì)象
Hashtable ht = new Hashtable();
ht.Add(1, "張三");
ht.Add(2, true);
ht.Add(false, "錯(cuò)誤");
ht[9] = "新來的";//這也是一種添加數(shù)據(jù)的方式
ht[1] = "把張三干掉";//如果有鍵1 就添加,有就覆蓋
//在鍵值對(duì)集合中兔港,是根據(jù)鍵去找值的
Console.WriteLine(ht[1]);
Console.WriteLine(ht[2]);
Console.WriteLine(ht[false]);
foreach (var item in ht.Keys)
{
Console.WriteLine("鍵{0} ------ 值{1}", item, ht[item]);
}
Console.ReadKey();
}
對(duì)于鍵值對(duì)集合取值庸毫,可以采用foreach循環(huán)
小練習(xí):簡繁轉(zhuǎn)換
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04簡繁轉(zhuǎn)換
{
class Program
{
private const String jian = "張長場";
private const String fan = "張長場";
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
for (int i = 0; i < jian.Length; i++)
{
ht.Add(jian[i], fan[i]);
}
Console.WriteLine("請(qǐng)隨便輸入");
string input = Console.ReadLine();
//將用戶輸入的簡體字轉(zhuǎn)換成繁體字
for (int i = 0; i < input.Length; i++)
{
if (ht.ContainsKey(input[i]))
{
Console.Write(ht[input[i]]);
}
else
{
Console.Write(input[i]);
}
}
Console.ReadKey();
}
}
}
Path類
專門用來操作路徑的一個(gè)類。直接上代碼衫樊。
static void Main(string[] args)
{
string str = @"D:\CodeRush\Support\abc.mp4";
//int index = str.LastIndexOf("\\");
//str = str.Substring(index +1);
//Console.WriteLine(str);
//獲取文件名字
Console.WriteLine(Path.GetFileName(str));
//獲取文件名不包括擴(kuò)展名
Console.WriteLine(Path.GetFileNameWithoutExtension(str));
//獲取文件的擴(kuò)展名
Console.WriteLine(Path.GetExtension(str));
//獲取文件所在文件夾的名稱
Console.WriteLine(Path.GetDirectoryName(str));
//獲取文件所在的全路徑
Console.WriteLine(Path.GetFullPath(str));
//連接兩個(gè)字符串作為路徑
Console.WriteLine(Path.Combine(@"c:\a\",@"b.text"));
Console.ReadKey();
}
File類
直接丟代碼
static void Main(string[] args)
{
//創(chuàng)建文件
//File.Create(@"C:\Users\Administrator\Desktop\new.txt");
//刪除文件
//File.Delete(@"C:\Users\Administrator\Desktop\new.txt");
//復(fù)制一個(gè)文件
File.Copy(@"C:\Users\Administrator\Desktop\new.txt", @"C:\Users\Administrator\Desktop\new2.txt");
Console.ReadKey();
}
編碼
文本文件編碼飒赃,文本文件有不同的存儲(chǔ)方式,將字符串一什么樣的形式保存為二進(jìn)制科侈,這就是編碼载佳。
產(chǎn)生亂碼的原因:就是因?yàn)槟惚4孢@個(gè)文件的編碼格式跟你打開這個(gè)文件的編碼格式不一樣。
往期回顧
- .Net基礎(chǔ)01
- .Net基礎(chǔ)02
- .Net基礎(chǔ)03
- .Net基礎(chǔ)04
- .Net基礎(chǔ)05
- .Net基礎(chǔ)06
- .Net基礎(chǔ)07
- .Net基礎(chǔ)08
- .Net基礎(chǔ)09