1,类的组成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace j1
{
/********** Begin *********/
public 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 *********/
public 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类
class Passenger{
public bool package;
public bool clothing;
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属性值
testing(p1.package);
testing(p1.clothing);
testing(p2.package);
testing(p2.clothing);
/********** 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 bool this[int index]
{
set{
switch(index)
{
case 1:
this.Monday=value;
break;
case 2:
this.Tuesday=value;
break;
case 3:
this.Wednesday=value;
break;
case 4:
this.Thursday=value;
break;
case 5:
this.Friday=value;
break;
}
}
}
/********** 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);
}
}
}