1. 程式人生 > >c#語言-------------型別轉換2(裡式轉換)

c#語言-------------型別轉換2(裡式轉換)

一、裡式轉換(在C#中一切類都直接或間接繼承與Object類)

1子類可以賦值給父類:實質如果一個地方需要一個父類做為引數,我們可以給一個子類代替(一個方法中需要傳進一個子類的物件,子類做實參,我們可以讓父類作為形參接收子類的物件)

2如果父類中裝的是子類的物件,那麼可以將這個父類轉換為子類物件

  **子類物件可以呼叫父類中的成員,但是父類物件只能呼叫自己的成員

3、

    is:表示型別轉換,如果轉換成功,則返回一個true,否則返回一個false

    as:表示型別轉換,如果能轉換則返回對應的物件,否則返回一個null

裡式轉換法(子類可以賦值給父類)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 里氏轉換法1
{
    class Program
    {
        static void Main(string[] args)
        {
            //子類可以賦值給父類:實質如果一個地方需要一個父類做為引數,我們可以給一個子類代替(一個方法中需要傳進一個子類的物件,子類做實參,我們可以讓父類作為形參接收子類的物件)
            Student stu1 = new Student();
            Person per1 = stu1;//

            Person per2 = new Student();
            Person per3 = new Teacher();

            string str = string.Join("|", new string[] { "1", "2", "3", "4" });//這裡也用到里氏轉換法
            Console.WriteLine(str);
          


            //如果父類中裝的是子類的物件,那麼可以將這個父類的物件強轉為子類物件
            Student st1 = (Student)per1;
            st1.StudentSayHello();
            Student st2 = (Student)per2;
            st2.StudentSayHello();
            Teacher tc3 = (Teacher)per3;
            tc3.TeacherSayHello();
            //Teacher tc4 = (Teacher)per1;//會丟擲異常因為per1中裝的是Student類的物件
            Console.ReadKey();
        }
    }
    public class Person
    {
        public void PersonSayHello()
        {
            Console.WriteLine("我是父類");
        }
    }
    public class Student:Person
    {
        public void StudentSayHello()
        {
            Console.WriteLine("我是學生類");
        }
    }
    public class Teacher : Person
    {
        public void TeacherSayHello()
        {
            Console.WriteLine("我是老師類");
        }
    }
}

執行結果:

裡式轉換法is的用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 里氏轉換法2之is
{
    class Program
    {
        static void Main(string[] args)
        {

            //子類可以賦值給父類:實質如果一個地方需要一個父類做為引數,我們可以給一個子類代替(一個方法中需要傳進一個子類的物件,子類做實參,我們可以讓父類作為形參接收子類的物件)
            Student stu1 = new Student();
            Person per1 = stu1;//

            Person per2 = new Student();
            Person per3 = new Teacher();


            //如果父類中裝的是子類的物件,那麼可以將這個父類的物件強轉為子類物件
            //is的用法
            //Student st1 = (Student)per1;
            //st1.StudentSayHello();
            if (per1 is Student)
            {
                Student st1 = (Student)per1;
                st1.StudentSayHello();
            }
            else 
            {
                Console.WriteLine("轉換失敗");
            }



            //Student st2 = (Student)per2;
            //st2.StudentSayHello();
            if (per2 is Student)
            {
                Student st2 = (Student)per2;
                st2.StudentSayHello();
            }
            else
            {
                Console.WriteLine("轉換失敗");
            }

            

            //Teacher tc3 = (Teacher)per3;
            //tc3.TeacherSayHello();
            if (per3 is Teacher)
            {
                Teacher tc3 = (Teacher)per3;
                tc3.TeacherSayHello();
            }
            else 
            {
                Console.WriteLine("轉換失敗");
            }


            //Teacher tc4 = (Teacher)per1;//會丟擲異常因為per1中裝的是Student類的物件
            if (per1 is Teacher)
            {
                Teacher tc4 = (Teacher)per1;
                tc4.TeacherSayHello();
            }
            else
            {
                Console.WriteLine("轉換失敗");
            }
            Console.ReadKey();
        }
    }
    public class Person
    {
        public void PersonSayHello()
        {
            Console.WriteLine("我是父類");
        }
    }
    public class Student : Person
    {
        public void StudentSayHello()
        {
            Console.WriteLine("我是學生類");
        }
    }
    public class Teacher : Person
    {
        public void TeacherSayHello()
        {
            Console.WriteLine("我是老師類");
        }
    }
}

執行結果:

裡式轉換法as的使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 里氏轉換法3之as
{
    class Program
    {
        static void Main(string[] args)
        {
            //子類可以賦值給父類:實質如果一個地方需要一個父類做為引數,我們可以給一個子類代替(一個方法中需要傳進一個子類的物件,子類做實參,我們可以讓父類作為形參接收子類的物件)
            Student stu1 = new Student();
            Person per1 = stu1;//

            Person per2 = new Student();
            Person per3 = new Teacher();
            //如果父類中裝的是子類的物件,那麼可以將這個父類的物件強轉為子類物件
            //as的用法
            Student st1 = per1 as Student;
            st1.StudentSayHello();
            Student st2 = per2 as Student;
            st2.StudentSayHello();
            Teacher tc1 = per3 as Teacher;
            tc1.TeacherSayHello();
            Teacher tc2 = per1 as Teacher;//tc2中是null


            Console.WriteLine("======分割線======");
            Console.WriteLine(st1);
            Console.WriteLine(st2);
            Console.WriteLine(tc1);
            Console.WriteLine(tc2);
            Console.WriteLine("上面的tc2裡面是空即什麼也打印不出來");
            Console.WriteLine("下面的t也為空即在下面的兩個分割線內什麼也打印不出來");
            Console.WriteLine("======分割線======");
            Teacher t = null;
            Console.WriteLine(t);
            Console.WriteLine("======分割線======");
            Console.WriteLine("結束");
            Console.ReadKey();
        }
    }
    public class Person
    {
        public void PersonSayHello()
        {
            Console.WriteLine("我是父類");
        }
    }
    public class Student : Person
    {
        public void StudentSayHello()
        {
            Console.WriteLine("我是學生類");
        }
    }
    public class Teacher : Person
    {
        public void TeacherSayHello()
        {
            Console.WriteLine("我是老師類");
        }
    }
}

執行結果:

里氏轉換法的練習:每次顯示的結果都是隨機的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 里氏轉換法4之練習
{
    class Program
    {
        static void Main(string[] args)
        {
            Person[] pers = new Person[10];//宣告一個Person型別的陣列來存Person類的物件(里氏轉換法可知也可存Person的子類)
            Random r = new Random();
            for (int i = 0; i < pers.Length; i++)//作用:通過隨機數給給我們的陣列賦值
            {
                int rNum = r.Next(1, 8);
                switch (rNum)
                {
                    case 1:pers[i] = new Chinese();
                        break;
                    case 2: pers[i] = new Person();
                        break;
                    case 3: pers[i] = new English();
                        break;
                    case 4: pers[i] = new American();
                        break;
                    case 5: pers[i] = new Russian();
                        break;
                    case 6: pers[i] = new French();
                        break;
                    case 7: pers[i] = new German();
                        break;

                }
                
            }
            for (int i = 0; i < pers.Length; i++)
            {
                if (pers[i] is Chinese)
                {
                    ((Chinese)pers[i]).ChineseSay();
                }
                else if (pers[i] is English)
                {
                    ((English)pers[i]).EnglishSay();
                }
                else if (pers[i] is American)
                {
                    ((American)pers[i]).AmericanSay();
                }
                else if (pers[i] is French)
                {
                    ((French)pers[i]).FrenchSay();
                }
                else if (pers[i] is Russian)
                {
                    ((Russian)pers[i]).RussianSay();
                }
                else if (pers[i] is German)
                {
                    ((German)pers[i]).GermanSay();
                }
                else
                {
                    pers[i].PersonSay();
                }
            }
            Console.ReadKey();

        }
    }
    public class Person
    {
        public void PersonSay()
        {
            Console.WriteLine("我是人類");
        }

    }
    public class Chinese : Person
    {
        public void ChineseSay()
        {
            Console.WriteLine("我是中國人");
        }
    }
    public class English : Person
    {
        public void EnglishSay()
        {
            Console.WriteLine("我是英國人");
        }
    }
    public class American : Person
    {
        public void AmericanSay()
        {
            Console.WriteLine("我是美國人");
        }
    }
    public class Russian : Person
    {
        public void RussianSay()
        {
            Console.WriteLine("我是俄羅斯人");
        }
    }
    public class German : Person
    {
        public void GermanSay()
        {
            Console.WriteLine("我是德國人");
        }
    }
    public class French : Person
    {
        public void FrenchSay()
        {
            Console.WriteLine("我是法國人");
        }
    }
}

執行結果: