c#學習心得


1.關於重載運算符

2.包含與委托(不太明白

class Program
    {
       
            class Radio 
            {
            protected bool on_off;

            public void On()
                {
                if (!on_off )Console .WriteLine ("Radio is now on!");
                    on_off  = true;
                }
            
            public void Off()
            {
                if (!on_off) Console.WriteLine("Radio is now off!");
                on_off = false;
            }
            }
      
    }
    class AlarmClock
    {
        private int currentTime;
        private int alarmTime;

        protected virtual  void SoundAlarm()
        {
            Console.WriteLine("Buzz!");

        }
        public void Run()
        {
            for (int currentTime = 0; currentTime < 43200; currentTime++)
            {
                SetCurrentTime(currentTime);
                if (GetCurrentTime() == GetAlarmTime())
                {
                    Console.WriteLine("Current Time = {0}!", currentTime);
                    SoundAlarm();
                    break;
                }
            }

        }
        public int GetCurrentTime()
        {
            return currentTime;
        }
        public void SetCurrentTime(int aTime)
        {
            currentTime = aTime;
        }
        public int GetAlarmTime()
        {

            return alarmTime;

        }
        public void SetAlarmTime(int aTime)
        {
            alarmTime = aTime;
        }
    }
    class ClockRadio : AlarmClock
    {
        private Radio radio;//聲明其他成員的變量
        public bool radioAlarm;

        public ClockRadio()
        {
            radio = new Radio();
            radioAlarm = false;
        }
        //-------------Delegate to Radio---------------
        public void RadioOn()
        {
            radio.On();
        }
        public void RadioOff()
        {
            radio.Off();
        }
        //--------------Overridde AlarmClock-----------------
        protected override void SoundAlarm()
        {
            if (radioAlarm)
            {
                RadioOn();

            }
            else
            {
                base.SoundAlarm();

            }
        }
        //-------New ClockRadio functionalitiy------------
        public void SetRadioAlarm(bool useRadio)
        {
            radioAlarm = useRadio;
        }

    }
    class ContInh
    {
        static int Main(string[] args)
        {
            ClockRadio clockRadio;

            clockRadio = new ClockRadio();

            clockRadio.SetRadioAlarm(true);
            clockRadio.SetAlarmTime(100);
            clockRadio.Run();

            //wait for user to acknowledge the results
            Console.WriteLine("Hit Enter to terminate....");
            Console.Read();
            return 0;
        }
    }

  錯誤提示:

錯誤 1 未能找到類型或命名空間名稱“Radio”(是否缺少 using 指令或程序集引用?) c:\users\administrator\documents\visual studio 2012\Projects\ConsoleApplication36\ConsoleApplication36\Program.cs 75 17 ConsoleApplication36

is 關鍵字與as關鍵字

//類 類對象 方法 接口
    public interface IPrintMessage
    {
        void Print();
    }
    class Class1
    {
        public void Print()
        {
            Console.WriteLine("from class1 ");
        }
    }
    class Class2 : IPrintMessage
    {
        public void Print()
        {
            Console.WriteLine("from class2");
        }
    }
    class Mainclass
    {
        public static void Main()
        { 
        PrintClass PrintObject = new PrintClass ();
        PrintObject.PrintMessage();
        }
    }
    class PrintClass
    {
        public void PrintMessage()
        {

            Class1 Object1 = new Class1();
            Class2 Object2 = new Class2();


            PrintMessageFromObject(Object1);
            PrintMessageFromObject(Object2);

        }
        private void PrintMessageFromObject(object obj)
        {
            if (obj is IPrintMessage)//對象標識符 關鍵字is 接口標識符
            {
                IPrintMessage PrintMessage;

                PrintMessage = (IPrintMessage)obj;//訪問對象接口實現時 先聲明一個接口類型的變量 然后將對象類型轉換成接口類型
                PrintMessage.Print();
            }
        }
    }
 private void PrintMessageFromObject(object obj)
        {


            IPrintMessage PrintMessage;

            PrintMessage = obj as IPrintMessage;//對象標識符 關鍵字as 接口標識符 
            if (PrintMessage != null)//c#關鍵字null(空值)
                PrintMessage.Print(); 
        }

  




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM