首页 > 代码库 > code回顾
code回顾
public class Singleton
{
private volatile static Singleton _instance = null;
private static readonly object lockHelper = new object();
private Singleton(){}
public static Singleton GetInstance()
{
if(_instance == null)
{
lock(lockHelper)
{
if(_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
}
3. 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
答:public class MainClass
{
public static void Main()
{
Console.WriteLine(Foo(30));
}
public static int Foo(int i)
{
if (i <= 0)
return 0;
else if(i > 0 && i <= 2)
return 1;
else return Foo(i -1) + Foo(i - 2);
}
}
10.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m
答:
int Num = Convert.ToInt32(this.TextBox1.Text);
int Sum = 0 ;
for (int i = 0 ; i < Num + 1 ; i++)
{
if((i%2) == 1)
{
Sum += i ;
}
else
{
Sum = Sum - I ;
}
}
System.Console.WriteLine(Sum.ToString());
System.Console.ReadLine() ;
61.产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
int[] intArr=new int[100];
ArrayList myList=new ArrayList();
Random rnd=new Random();
while(myList.Count<100)
{
int num=rnd.Next(1,101);
if(!myList.Contains(num))
myList.Add(num);
}
for(int i=0;i<100;i++)
intArr[i]=(int)myList[i];
public static Singleton GetInstance()
{
if (singleton == null)
{
lock (syncObject)
{
if (singleton == null)
{
singleton = new Singleton();
}
}
}
return singleton;
}
从上面的这个 GetInstance()中可以看出这个单例类的唯一实例是在第一次调用 GetInstance()时实例化的,
所以此为懒汉式单例。
上面介绍了饿汉式单例,到这里来理解懒汉式单例的话,就容易多了,懒汉式单例由于人懒,
所以其自己是不会主动实例化单例类的唯一实例的,而饿汉式的话,则刚好相反,
其由于肚子饿了,所以到处找东西吃,人也变得主动了很多,所以根本就不需要别人来催他实例化单例类的为一实例,
其自己就会主动实例化单例类的这个唯一类。
饿汉式单例类
public sealed class Singleton
{
private static readonly Singleton singleton = new Singleton();
private Singleton()
{
}
public static Singleton GetInstance()
{
return singleton;
}
}
$("#Val9").click(function () {
WdatePicker({ skin: ‘whyGreen‘, dateFmt: ‘yyyy-MM‘, minDate: new Date().Format("yyyy-MM") });
});
$("#Val9").val(new Date().Format("yyyy-MM"));
$("#frm").validate({
rules: {
Val5: "required",
Val2: {
required: true,
remote: {
type: "get",
url: "/Form/CheckData",
data: {
tel: function () { return $("#Val2").val(); }
},
dataType: "json"
}
}
},
errorPlacement: function (error, element) {
var p = $("<p />").append(error);
p.appendTo(element.parent());
},
messages: {
Val9: "请输入应缴月份",
Val1: "请输入姓名",
Val2: {
required: ‘请输入手机号码‘,
remote: ‘该手机号在该月已经存在记录‘
},
Val3: "请输入支部",
Val5: "请输入应缴金额"
}
});
IEnumerable<DataRow> queryforJump = dt.AsEnumerable().Except(dtJump.AsEnumerable(), DataRowComparer.Default);
IEnumerable<DataRow> r = queryforJump.Except(dtCover.AsEnumerable(), DataRowComparer.Default);
public static AppSearch SearchApp(string appName, int pageIndex, int pageSize)
{
string strAppServerHost = "http://183.56.132.225:10000/";
//string url = !string.IsNullOrEmpty(appName)
// ? string.Format("{0}?key=name,,{1}&filt_type=templateid,l4m4||l8m8&max_doc_len=1024&pagesize={2}&page={3}&sort_type=sort_subtime_desc", strAppServerHost, appName, pageSize, pageIndex)
// : string.Format("{0}?key=tidalias,,4&max_doc_len=1024&pagesize={1}&page={2}&sort_type=sort_subtime_desc", strAppServerHost, pageSize, pageIndex);
string url = !string.IsNullOrEmpty(appName)
? string.Format("{0}?key=name,,{1}&max_doc_len=1024&pagesize={2}&page={3}&sort_type=sort_subtime_desc", strAppServerHost, appName, pageSize, pageIndex)
: string.Format("{0}?key=tidalias,,4&max_doc_len=1024&pagesize={1}&page={2}&sort_type=sort_subtime_desc", strAppServerHost, pageSize, pageIndex);
// "183.56.132.225:10000/?key=name,,文明办表单&max_doc_len=1024&pagesize=20&page=1&sort_type=sort_subtime_desc"
LogHelper.Fatal(string.Format("●●●搜索URL:{0}", url));
WebRequest request = WebRequest.Create(url);
try
{
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GBK")))
{
string ret = reader.ReadToEnd();
if (ret.Length == 0) LogHelper.Error("应用分页搜索失败");
else
{
AppSearch dto = JsonConvert.DeserializeObject<AppSearch>(ret);
return dto;
}
}
}
}
}
}
catch (Exception ex)
{
LogHelper.Error("应用分页搜索异常");
}
return null;
}
code回顾