第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(); } } }
第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(); } } }
第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"); } } } }
第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(); } } }
第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); } } }