.Net基礎(chǔ)11

主要內(nèi)容
File類

上一篇文章漏了幾個(gè)方法,這里補(bǔ)上。

static void Main(string[] args)
{
    //讀取
    byte[] buffer = File.ReadAllBytes(@"C:\Users\Administrator\Desktop\text.txt");
    //將字節(jié)數(shù)組中的每個(gè)元素都按照我們指定的編碼格式解碼成字符串
    string s = Encoding.GetEncoding(@"utf-8").GetString(buffer);
    Console.WriteLine(s);


    //寫入
    //沒有這個(gè)文件的話匯創(chuàng)建一個(gè)
    string str = "敲鍵盤";
    //需要字符串轉(zhuǎn)換成字節(jié)數(shù)組
    byte[] buffer2 = Encoding.Default.GetBytes(str);
    File.WriteAllBytes(@"C:\Users\Administrator\Desktop\tt.txt", buffer2);

    Console.WriteLine("------------------------------------");

    //以行的形式讀取
    string[] contents = File.ReadAllLines(@"C:\Users\Administrator\Desktop\tt.txt", Encoding.Default);
    foreach (var item in contents)
    {
        Console.WriteLine(item);
    }

    //讀取所有文本
    string strr = File.ReadAllText(@"C:\Users\Administrator\Desktop\tt.txt", Encoding.Default);
    Console.WriteLine(strr);

    //以行的形式寫入
    File.WriteAllLines(@"C:\Users\Administrator\Desktop\tt.txt", new string[] {"111", "222"});

    //寫入字符串
    File.WriteAllText(@"C:\Users\Administrator\Desktop\tt.txt", "ewtqeqweqwewq");


    //追加
    File.AppendAllText(@"C:\Users\Administrator\Desktop\tt.txt", "追加的文字");


    Console.ReadKey();
}

絕對(duì)路徑和相對(duì)路徑

絕對(duì)路徑:通過(guò)給定的這個(gè)路徑直接能在我的電腦中找到這個(gè)文件。
相對(duì)路徑:文件相對(duì)于應(yīng)用程序的路徑。
我們?cè)陂_發(fā)中應(yīng)該去盡量的使用相對(duì)路徑。

List泛型集合
static void Main(string[] args)
{
    //創(chuàng)建泛型集合對(duì)象
    List<int> list = new List<int>();
    list.Add(20);
    list.Add(30);
    list.Add(40);

    list.AddRange(new int[] { 1, 2, 3});
    list.AddRange(list);

    //list泛型集合可以轉(zhuǎn)換為數(shù)組
    int[] nums = list.ToArray();
    //將數(shù)組轉(zhuǎn)集合
    List<int> listTwo = nums.ToList();

    for (int i = 0; i < list.Count; i++)
    {
        Console.WriteLine(list[i]);
    }

    Console.ReadKey();
}

裝箱拆箱

裝箱:就是將值類型轉(zhuǎn)換為引用類型。
拆箱:將引用類型轉(zhuǎn)換為值類型。
看兩種類型是否發(fā)生了裝箱或者拆箱,要看,這兩種類型是否存在繼承關(guān)系。只有存在繼承才有可能發(fā)生。

static void Main(string[] args)
{
    int n = 10;
    object o = n;//裝箱
    int m = (int)o;//拆箱

    Console.ReadKey();
}
字典集合
static void Main(string[] args)
{
    Dictionary<int, string> dict = new Dictionary<int, string>();
    dict.Add(1, "張三");
    dict.Add(2, "李四");

    dict[1] = "新來(lái)的";

    foreach (var item in dict.Keys)
    {
        Console.WriteLine("key: {0}------value: {1}", item, dict[item]);
    }

    //一對(duì)一對(duì)遍歷
    foreach (KeyValuePair<int, string> kv in dict)
    {
        Console.WriteLine("key {0} ----- value {1}", kv.Key, kv.Value);
    }

    Console.ReadKey();
}
輸出結(jié)果
FileStream 文件流 (操作字節(jié)的)
  • FileStream的讀寫
static void Main(string[] args)
{
    //File 一次性讀取 適合操作小文件
    //FileStream 操作字節(jié)的
    //StreamReader 和 StreamWriter 操作字符的


    //使用FileStream讀取文件
    ////創(chuàng)建FileStream對(duì)象
    //FileStream fsRead = new FileStream(@"C:\Users\Administrator\Desktop\text.txt", FileMode.OpenOrCreate, FileAccess.Read);
    ////每次讀取大小
    //byte[] buffer = new byte[1024 * 1024 * 5];
    ////返回本次實(shí)際讀取到的有效字節(jié)數(shù)
    //int r = fsRead.Read(buffer, 0, buffer.Length);
    ////將字節(jié)數(shù)組中的每一個(gè)元素按照指定的編碼格式解碼成字符串
    //string s = Encoding.Default.GetString(buffer, 0, r);
    ////關(guān)閉流 
    //fsRead.Close();
    ////釋放流所占用的資源
    //fsRead.Dispose();
    //Console.WriteLine(s);
    //Console.ReadKey();


    //使用FileStream寫入文件
    //將創(chuàng)建文件流對(duì)象的過(guò)程寫在using當(dāng)中,會(huì)自動(dòng)的幫助我們釋放流所占用的資源。

    using (FileStream fsWrite = new FileStream(@"C:\Users\Administrator\Desktop\text.txt", FileMode.OpenOrCreate, FileAccess.Write))
    {
        string s = "看我有沒有把你覆蓋";
        byte[] buffer = Encoding.Default.GetBytes(s);
        fsWrite.Write(buffer, 0, buffer.Length); 
    }

    Console.ReadKey();
}

  • 使用文件流實(shí)現(xiàn)多媒體復(fù)制
static void Main(string[] args)
{
    //思路:先將要復(fù)制的多媒體文件讀取出來(lái),然后寫入到指定的位置
    string source = @"C:\Users\Administrator\Desktop\4、變量的使用規(guī)則.avi";
    string taeget = @"C:\Users\Administrator\Desktop\1111.avi";

    CopyFile(source, taeget);
    Console.ReadKey();
}

public static void CopyFile(string source, string target)
{
    //創(chuàng)建一個(gè)讀取的流
    using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
    {

        //創(chuàng)建一個(gè)寫入的流
        using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
        {
            byte[] buffer = new byte[1024 * 1024 * 5];
            //因?yàn)槲募赡軙?huì)比較大 ,所以我們?cè)谧x取的時(shí)候應(yīng)該通過(guò)一個(gè)循環(huán)去讀取
            while (true)
            {
                //返回本次實(shí)際讀取的字節(jié)數(shù)
                int r = fsRead.Read(buffer, 0, buffer.Length);
                //如果返回一個(gè)0 也就一位置什么都沒有讀取到,讀取完了
                if (r == 0)
                {
                    break;
                }
                fsWrite.Write(buffer, 0, r);
            }  
        }
    }
}
StreamReader和StreamWriter (操作字符的)
static void Main(string[] args)
{
    //使用StreamReader 來(lái)讀取一個(gè)文本文件
    //using (StreamReader sr = new StreamReader(@"C:\Users\Administrator\Desktop\text.txt", Encoding.Default))
    //{
    //    while (!sr.EndOfStream)
    //    {
    //        Console.Write(sr.ReadLine());
    //    }
    //}
    //Console.ReadKey();

    //使用StreamReader 來(lái)寫入一個(gè)文本文件
    using (StreamWriter sw = new StreamWriter(@"C:\Users\Administrator\Desktop\text.txt", true))
    {
        sw.Write("今天天氣好晴朗");
    }
    Console.ReadKey();
}

多態(tài)

概念:讓一個(gè)對(duì)象能夠表現(xiàn)出多種狀態(tài)(類型)
實(shí)現(xiàn)多態(tài)的3種手段 1、虛方法 2、抽象類 3、接口

  • 虛方法
    步驟
    1、將父類的方法標(biāo)記為虛方法 ,使用關(guān)鍵字 virtual,這個(gè)函數(shù)可以被子類重新寫一遍。在子類方法中使用關(guān)鍵字 virtual
static void Main(string[] args)
{
    //概念:讓一個(gè)對(duì)象能夠表現(xiàn)出多種狀態(tài)(類型)
    //實(shí)現(xiàn)多態(tài)的3中手段 1、虛方法 2、抽象類 3、接口
    
    Chinese cn1 = new Chinese("韓梅梅");
    Chinese cn2 = new Chinese("李雷");

    Japanese j1 = new Japanese("竹下");
    Japanese j2 = new Japanese("田中");

    Korean k1 = new Korean("xxx俊熙");
    Korean k2 = new Korean("dede美熙");

    Person[] pers = { cn1, cn2, j1, j2, k1, k2};

    for (int i = 0; i < pers.Length; i++)
    {
        //if (pers[i] is Chinese)
        //{
        //    ((Chinese)pers[i]).SayHello();
        //}
        //else if (pers[i] is Japanese)
        //{
        //    ((Japanese)pers[i]).SayHello();
        //}
        //else
        //{
        //    ((Korean)pers[i]).SayHello();
        //}

        pers[i].SayHello();
    }

    Console.ReadKey();
}

public class Person
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public Person(string name)
    {
        this.Name = name;
    }

    public virtual void SayHello()
    {
        Console.WriteLine("我是人類");
    }
}

public class Chinese : Person
{
    public Chinese(string name) : base(name)
    {
    }

    public override void SayHello()
    {
        Console.WriteLine("我是中國(guó)人,我叫{0}", this.Name);
    }

}

public class Japanese : Person
{
    public Japanese(string name) : base(name)
    {
    }

    public override void SayHello()
    {
        Console.WriteLine("日本人 {0}", this.Name);
    }
}

public class Korean : Person
{
    public Korean(string name) : base(name)
    {

    }

    public override void SayHello()
    {
        Console.WriteLine("韓國(guó)棒子 {0}", this.Name);
    }
}

  • 抽象類

當(dāng)父類中的方法不知道如何去實(shí)現(xiàn)的時(shí)候,可以考慮將父類寫成抽象類,將方法寫成抽象方法。
注意:使用關(guān)鍵字 abstract, 抽象方法不能有方法體,子類方法使用override 從寫。

static void Main(string[] args)
{
    //狗狗會(huì)叫 貓咪會(huì)叫

    Animal a = new Dog();
    a.Bark();

    Animal b = new Cat();
    b.Bark();

    Console.ReadKey();
}

//使用關(guān)鍵字 abstract
public abstract class Animal
{
    //抽象方法不能有方法體
    public abstract void Bark();
}

public class Dog : Animal
{
    public override void Bark()
    {
        Console.WriteLine("狗狗旺旺叫");
    }

}

public class Cat : Animal
{
    public override void Bark()
    {
        Console.WriteLine("貓咪喵喵叫");
    }
}

1.抽象成員必須標(biāo)記為abstract,并且不能有任何實(shí)現(xiàn)。
2.抽象成員必須在抽象類中。
3.抽象類不能被實(shí)例化

4.子類繼承抽象類后,必須把父類中的所有抽象成員都重寫。

(除非子類也是一個(gè)抽象類,則可以不重寫)
5.抽象成員的訪問(wèn)修飾符不能是private
6.在抽象類中可以包含實(shí)例成員。
并且抽象類的實(shí)例成員可以不被子類實(shí)現(xiàn)

7.抽象類是有構(gòu)造函數(shù)的。雖然不能被實(shí)例化。

8、如果父類的抽象方法中有參數(shù),那么。繼承這個(gè)抽象父類的子類在重寫父類的方法的時(shí)候必須傳入對(duì)應(yīng)的參數(shù)。

如果抽象父類的抽象方法中有返回值,那么子類在重寫這個(gè)抽象方法的時(shí)候 也必須要傳入返回值。

======
如果父類中的方法有默認(rèn)的實(shí)現(xiàn),并且父類需要被實(shí)例化,這時(shí)可以考慮將父類定義成一個(gè)普通類,用虛方法來(lái)實(shí)現(xiàn)多態(tài)。

如果父類中的方法沒有默認(rèn)實(shí)現(xiàn),父類也不需要被實(shí)例化,則可以將該類定義為抽象類。

抽象類練習(xí):使用多態(tài)求矩形的面積和周長(zhǎng)以及圓形的面積和周長(zhǎng)。

static void Main(string[] args)
{
    //使用多態(tài)求矩形的面積和周長(zhǎng)以及圓形的面積和周長(zhǎng)

    Shape shape = new Square(2, 3);//new Circle(3.0);
    Console.WriteLine(shape.GetArea());
    Console.WriteLine(shape.GetPerimeter());

    Console.ReadKey();
}

public abstract class Shape
{
    public abstract double GetArea();
    public abstract double GetPerimeter();
}

public class Circle : Shape
{
    private double _r;

    public double R
    {
        get { return _r; }
        set { _r = value; }
    }

    public Circle(double r)
    {
        this.R = r;
    }

    public override double GetArea()
    {
        return Math.PI * this.R * this.R;
    }

    public override double GetPerimeter()
    {
        return 2 * Math.PI * this.R;
    }
}

public class Square : Shape
{
    private double _length;
    private double _width;

    public double Length
    {
        get { return _length; }
        set { _length = value; }
    }

    public double Width
    {
        get { return _width; }
        set { _width = value; }
    }

    public Square(double length, double width)
    {
        this.Length = length;
        this.Width = width;
    }

    public override double GetArea()
    {
        return this.Length * this.Width;
    }

    public override double GetPerimeter()
    {
        return 2 * (Length + Width);
    }
}

往期回顧

圖文無(wú)關(guān).png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,728評(píng)論 18 399
  • 一:java概述:1,JDK:Java Development Kit,java的開發(fā)和運(yùn)行環(huán)境,java的開發(fā)工...
    ZaneInTheSun閱讀 2,813評(píng)論 0 11
  • 1.import static是Java 5增加的功能,就是將Import類中的靜態(tài)方法,可以作為本類的靜態(tài)方法來(lái)...
    XLsn0w閱讀 1,428評(píng)論 0 2
  • 面向?qū)ο笾饕槍?duì)面向過(guò)程。 面向過(guò)程的基本單元是函數(shù)。 什么是對(duì)象:EVERYTHING IS OBJECT(萬(wàn)物...
    sinpi閱讀 1,220評(píng)論 0 4
  • 璞玉57閱讀 389評(píng)論 2 2

友情鏈接更多精彩內(nèi)容