1. 程式人生 > WINDOWS開發 >C#類(一):類和物件(EduCoder實訓題目)

C#類(一):類和物件(EduCoder實訓題目)

第1關:類的組成

技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace j1
{
    /********** Begin *********/
    class Phone{
    string name = "MI";
    string model = "MIX2";
    int price = 2999;

    public void showPhoneInformation(){
        Console.WriteLine(
"Phone name:" + name); Console.WriteLine("Phone model:" + model); Console.WriteLine("Phone price:" + price); } } /********** End *********/ public class myCaller { public static void Main(string[] args) { Phone myPhone = new Phone(); myPhone.showPhoneInformation(); } } }
View Code

第2關:建構函式和解構函式

技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace J2
{
    /********** Begin *********/
    class myDevice
    {
        string ip;

    public myDevice()
        {
            ip = "103.67.193.190";
        }
        
public myDevice(string _ip) { ip = _ip; } public void showIp() { Console.WriteLine(ip); } } /********** End *********/ public class myCaller { public static void Main(string[] args) { myDevice phone = new myDevice(); myDevice computer = new myDevice("103.67.193.195"); phone.showIp(); computer.showIp(); } } }
View Code

第3關:private、protected、public、internal的區別

技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace J3
{
    /********** Begin *********/
    //Passenger類
    public class Passenger
    {
        public bool package;
        private bool clothing;
        public bool getClothing()
        {
            return clothing;
        }
        public Passenger()
        {
            
        }
        public Passenger(bool _package,bool _clothing)
        {
            package = _package;
            clothing = _clothing;
        }
    }
    /********** End *********/

    class Worker
    {
        public static void Main(string[] args)
        {
            Passenger p1 = new Passenger(true,true);
            Passenger p2 = new Passenger(true,false);

            /********** Begin *********/
            //使用函式testing檢視p1、p2屬性值
            Worker.testing(p1.package);
            Worker.testing(p1.getClothing());
            Worker.testing(p2.package);
            Worker.testing(p2.getClothing());
            /********** End *********/

        }
        //手動檢測函式
        public static void testing(bool thing)
        {
            if (thing != true)
            {
                Console.WriteLine("Dangerous");
            }
            else
            {
                Console.WriteLine("Pass");
            }
        }
    }
}
View Code

第4關:this關鍵字

技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace J4
{
    class Schedule
    {
        bool Monday;
        bool Tuesday;
        bool Wednesday;
        bool Thursday;
        bool Friday;

        /********** Begin *********/
        //使用this索引器補全程式碼
        public void setResult(bool Monday,bool Tuesday,bool Wednesday,bool Thursday,bool Friday)
        {
            this.Monday = Monday;
            this.Tuesday = Tuesday;
            this.Wednesday = Wednesday;
            this.Thursday = Thursday;
            this.Friday = Friday;
        }
        /********** End *********/
        public void getResult()
        {
            Console.WriteLine("Monday:" + Monday);
            Console.WriteLine("Tuesday:" + Tuesday);
            Console.WriteLine("Wednesday:" + Wednesday);
            Console.WriteLine("Thursday:" + Thursday);
            Console.WriteLine("Friday:" + Friday);
        }
    }
    class myCaller
    {
        public static void Main(string[] args)
        {
            Schedule s = new Schedule();
            s[1] = true;
            s[2] = true;
            s[3] = false;
            s[4] = false;
            s[5] = true;

            s.getResult();

        }
    }
}
View Code

第5關:static靜態物件

技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace J5
{
    public class BigEvent
    {
        private string time;
        private string location;
        /********** Begin *********/
        public static int counter = 0;
        public BigEvent()
        {
            counter++;
        }
        /********** End *********/
    }

    public class myCaller
    {
        public static void Main(string[] args)
        {
            BigEvent b1 = new BigEvent();
            BigEvent b2 = new BigEvent();
            BigEvent b3 = new BigEvent();

            Console.WriteLine(BigEvent.counter);
        }
    }
}
View Code