淺談JS中的!=、== 、!==、===的用法和區別 JS中Null與Undefined的區別 讀取XML文件 獲取路徑的方式 C#中Cookie,Session,Application的用法與區別? c#反射 抽象工廠
var
num = 1;
var
str =
‘1‘
;
var
test = 1;
test == num
//true 相同類型 相同值
test === num
//true 相同類型 相同值
test !== num
//false test與num類型相同,其值也相同, 非運算肯定是false
num == str
//true 把str轉換為數字,檢查其是否相等。
num != str
//false == 的 非運算
num === str
//false 類型不同,直接返回false
num !== str
//true num 與 str類型不同 意味著其兩者不等 非運算自然是true啦
== 和 != 比較若類型不同,先償試轉換類型,再作值比較,最後返回值比較結果 。
而
=== 和 !== 只有在相同類型下,才會比較其值。(值和類型都相同的情況下比較)
==, 兩邊值類型不同的時候,要先進行類型轉換,再比較。
===,不做類型轉換,類型不同的一定不等。
--JS中Null與Undefined的區別
Undefined:當聲明的變量還未被初始化時,變量的默認值為undefined。
null類型也只有一個值,即null。null用來表示尚未存在的對象,常用來表示函數企圖返回一個不存在的對象。
var re;
typeof(re) ; //返回 結果為undefined
typeof(null); //返回結果為object
null的數據類型是Object對象,那麽JS的五大數據類型是String,Boolean,Number,Null,Undefine
用法:
null表示"沒有對象",即該處不應該有值。典型用法是:
(1) 作為函數的參數,表示該函數的參數不是對象。
(2) 作為對象原型鏈的終點。
舉例:
Object.getPrototypeOf(Object.prototype)
// null
undefined表示"缺少值",就是此處應該有一個值,但是還沒有定義。典型用法是:
(1)變量被聲明了,但沒有賦值時,就等於undefined。
(2) 調用函數時,應該提供的參數沒有提供,該參數等於undefined。
(3)對象沒有賦值的屬性,該屬性的值為undefined。
(4)函數沒有返回值時,默認返回undefined。
舉例:
var i;
i // undefined
function f(x){console.log(x)}
f() // undefined
var o = new Object();
o.p // undefined
var x = f();
x // undefined
----
讀取XML文件
XmlDocument xmlDoc = new XmlDocument();
string filePath = Path.Combine(AppContext.BaseDirectory, "Configuration", sDir, string.Format("{0}.xml", sXmlName));
StringBuilder builder = new StringBuilder();
using (FileStream fs = File.Open(filePath, FileMode.OpenOrCreate))
{
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
builder.Append(sr.ReadToEnd());
}
}
if (builder.Length > 0)
xmlDoc.LoadXml(builder.ToString()); //
return xmlDoc;
註意:
xmldoc.load可以加載文件,比如:xmldoc.load("c:\\a.xml");
而xmldoc.loadXML是加載xml格式的字符串,比如:xmldoc.loadXML("<aab>aaa</aab>")
-----------------------
舉例說明
1: <?xml version="1.0" encoding="utf-8"?>
2: <bookstore>
4: <book Type="必修課" ISBN="7-111-19149-2">
5: <title>數據結構</title>
6: <author>嚴蔚敏</author>
7: <price>30.00</price>
8: </book>
9: <book Type="必修課" ISBN="7-111-19149-3">
10: <title>路由型與交換型互聯網基礎</title>
11: <author>程慶梅</author>
12: <price>27.00</price>
13: </book>
34: </bookstore>
1: // 得到根節點bookstore
2: XmlNode xn = xmlDoc.SelectSingleNode("bookstore");
5: // 得到根節點的所有子節點
6: XmlNodeList xnl = xn.ChildNodes;
8: foreach (XmlNode xn1 in xnl)
9: {
11: // 將節點轉換為元素,便於得到節點的屬性值
12: XmlElement xe = (XmlElement)xn1;
13: // 得到Type和ISBN兩個屬性的屬性值
14: bookModel.BookISBN = xe.GetAttribute("ISBN").ToString();
15: bookModel.BookType = xe.GetAttribute("Type").ToString();
16: // 得到Book節點的所有子節點
17: XmlNodeList xnl0 = xe.ChildNodes;
18: bookModel.BookName=xnl0.Item(0).InnerText;
19: bookModel.BookAuthor=xnl0.Item(1).InnerText;
20: bookModel.BookPrice=Convert.ToDouble(xnl0.Item(2).InnerText);
21: bookModeList.Add(bookModel);
22: }
//獲取根節點
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlElement root = doc.DocumentElement;
//獲取子節點集合
//XmlNodeList xnl = root.ChildNodes;
XmlNodeList personNodes = root.GetElementsByTagName("book"); //兩個table
方法一:SelectNodes
xml樣式:
<?xml version="1.0" encoding="utf-8" ?>
<TableLists>
<Columns Name="A01" >
<Item Id="ID1" Filed="Name" MappingName="姓名" Code="Value" />
<Item Id="ID2" Filed="Name" MappingName="年齡" Code="Value"/>
<Item Id="ID3" Filed="Name" MappingName="性別" Code="Value"/>
<Item Id="ID4" Filed="Name" MappingName="部門" Code="Value"/>
</Columns>
<Columns Name="A02" >
<Item Id="ID21" Filed="Name" MappingName="姓名" Code="Value" />
<Item Id="ID22" Filed="Name" MappingName="年齡" Code="Value"/>
<Item Id="ID23" Filed="Name" MappingName="性別" Code="Value"/>
<Item Id="ID24" Filed="Name" MappingName="部門" Code="Value"/>
</Columns>
</TableLists>
XmlDocument doc = new XmlDocument();
doc.Load(fileRelationPath);
XmlNodeList a01Nodes = doc.SelectNodes("TableLists/Columns"); //2個columns
XmlNodeList a01Node = doc.SelectNodes("//Columns"); //2個columns
XmlNodeList a01Nodes = doc.SelectNodes("TableLists/Columns/Item"); //8個Item
XmlNodeList a01Node = doc.SelectNodes("//Item"); //8個Item
foreach (XmlNode xnode in a01Nodes)
{
//獲取表名字
XmlElement xe = (XmlElement)xnode;
string tableName = xe.GetAttribute("Name").ToString(); //表名字
}
doc.ChildNodes[1].ChildNodes[0].ChildNodes
說明:
doc.ChildNodes的值[0] <?xml version="1.0" encoding="utf-8" ?>
[1] TableLists 根元素節點
---
獲取路徑的方式
static void Main(string[] args) 4 { 5
string str = System.AppDomain.CurrentDomain.BaseDirectory; //獲取程序的基目錄
string filePath = System.IO.Path.Combine(str, "Files", "MyPerson.xml");
6 //獲取當前運行程序的目錄 7 string fileDir = Environment.CurrentDirectory; 8 Console.WriteLine("當前程序目錄:"+fileDir); 9 10 //一個文件目錄 11 string filePath = "C:\\JiYF\\BenXH\\BenXHCMS.xml"; 12 Console.WriteLine("該文件的目錄:"+filePath); 13 14 string str = "獲取文件的全路徑:" + Path.GetFullPath(filePath); //-->C:\JiYF\BenXH\BenXHCMS.xml 15 Console.WriteLine(str); 16 str = "獲取文件所在的目錄:" + Path.GetDirectoryName(filePath); //-->C:\JiYF\BenXH 17 Console.WriteLine(str); 18 str = "獲取文件的名稱含有後綴:" + Path.GetFileName(filePath); //-->BenXHCMS.xml 19 Console.WriteLine(str); 20 str = "獲取文件的名稱沒有後綴:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS 21 Console.WriteLine(str); 22 str = "獲取路徑的後綴擴展名稱:" + Path.GetExtension(filePath); //-->.xml 23 Console.WriteLine(str); 24 str = "獲取路徑的根目錄:" + Path.GetPathRoot(filePath); //-->C:25 Console.WriteLine(str); 26 Console.ReadKey(); 27 28 }
或者
string str = System.AppDomain.CurrentDomain.BaseDirectory;
string filePath = System.IO.Path.Combine(str, "Files", "Lists.xml");
//加載根目錄下XML文件
string filePath = System.IO.Path.Combine(str, "Files", "MyPerson.xml");
doc.Load(filePath);
//獲取根節點
XmlElement root = doc.DocumentElement;
//獲取子節點集合
//XmlNodeList xnl = root.ChildNodes;
XmlNodeList personNodes = root.GetElementsByTagName("Table");
foreach (XmlNode node in personNodes)
{
//XmlNodeList y= node.ChildNodes[0].ChildNodes; //得到是Columns的子節點集合(3個Item)
XmlElement xe = (XmlElement)node;
string tableName = xe.GetAttribute("Name").ToString(); //表名字
XmlElement xeChild = (XmlElement)xe.ChildNodes[0]; //獲取的是Columns
string namee = xeChild.GetAttribute("Col").ToString(); //Columns的屬性名Item
XmlNodeList x = xeChild.ChildNodes; // 得到Columns節點的所有子節點Item
foreach (XmlNode oo in x)
{
XmlElement xeChildd = (XmlElement)oo;
string sID = xeChildd.GetAttribute("Id").ToString(); //Item的屬性名
string sFiled = xeChildd.GetAttribute("Filed").ToString(); //Item的屬性名
string sName = xeChildd.GetAttribute("Name").ToString(); //Item的屬性名
}
}
--
C#中Cookie,Session,Application的用法與區別?
1.Application 儲存在服務端,沒有時間限制,服務器關閉即銷毀(前提是自己沒寫銷毀方法)
2.Session 儲存在服務端,客戶端(瀏覽器)關閉即銷毀(若長時間不使用 且 瀏覽器未關閉的情況下, 默認自動銷毀時間為20分鐘)
3.Cookie 儲存在客戶端,由用戶自己銷毀
application:
程序全局變量對象,對每個用戶每個頁面都有效
session:
用戶全局變量,對於該用戶的所有操作過程都有效
cookie:
客戶端信息存放對象,可以把用戶的信息保存在用戶的本地,
不必總是訪問服務器
Application用於保存所有用戶共用的數據信息,如果被保存的數據在應用程序生存期內根本不會改變或很少改變,用它。但是在asp.net中有個web.config,可能更好點。如果要使用application,一個需要考慮的問題是任何寫操作都有要在application_onstart事件中(Global.asax)中完成。盡管使用application.lock和application.unlock方法來避免操作的同步,但是它串行化了對application的請求,當網站訪問量大時會造成性能瓶頸。因此最好不要用它存取大的數據集。
使用方法:
//存放信息
Application["test"] = "100";
//讀取
String test = Application["test"].ToString();
Session 用於保存每個用戶的專用信息,它的生存期是用戶持續請求時間再加上一段時間(可以在web.config中設置,默認是20分鐘)。Session中的信息保存在服務器的內存中,當然你也可以設置它的保存方法(如存在SQL數據庫中)。由於用戶停止使用程序後它仍然在內存中保持一段時間,因此使用Session對象保存用戶數據的方法效率很低。對於小量的數據。使用Session還是一個不錯的選擇。
//存
Session["user"] = "majcms";
//取
String username = Session["user"].ToString();
在web.config中進行如下配置
<system.web>
<sessionState mode="InProc" timeout="30"/>
</system.web>
Cookie用於保存客戶瀏覽器請求服務器頁面的請求信息,程序員也可以用它保存非敏感性的內容。保存時間可以根據需要設置。如果沒有設置Cookie失效時間,它僅保存至瀏覽器關閉。如果將Cookie設置為Min Value,則表示它永不過期。Cookie存儲量受到很大限制,一般瀏覽器支持最大容量為4096字節。因此不能用來存儲大量數據。由於並非所有瀏覽器都支持Cookie,並且它是以明文方式保存的,所以最好不要保存敏感性的內容。否則會影響網絡安全。
//創建、寫入Cookie
HttpCookie cookie = Request.Cookies["MWS_User"];
if (cookie == null)
{
cookie = new HttpCookie("MWS_User");
}
cookie.Values.Set("UserID", strUserID);
Response.SetCookie(cookie);
二、讀取cookie:
HttpCookie cookie = Request.Cookies["MWS_User"];
if (cookie != null && cookie["UserID"].ToString() != "")
{
Response.Write("cookie=" + cookie["UserID"].ToString());
}
//存
Response.Cookies["name"].Value = "majcms";
//取
String username = Response.Cookies["name"].Value;
---------------------------------------------------------------------
//寫入
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie=new HttpCookie("MyCook");//初使化並設置Cookie的名稱
DateTime dt=DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 1,0,0);//過期時間為1分鐘
cookie.Expires = dt.Add(ts);//設置過期時間
cookie.Values.Add("userid", "userid_value");
cookie.Values.Add("userid2","userid2_value2");
Response.AppendCookie(cookie);
//輸出該Cookie的所有內容
//Response.Write(cookie.Value);//輸出為:userid=userid_value&userid2=userid2_value2
}
//讀取
protected void Button2_Click(object sender, EventArgs e)
{
// HttpCookie cokie = new HttpCookie("MyCook");//初使化
if (Request.Cookies["MyCook"]!=null)
{
//Response.Write("Cookie中鍵值為userid的值:" + Request.Cookies["MyCook"]["userid"]);//整行
//Response.Write("Cookie中鍵值為userid2的值" + Request.Cookies["MyCook"]["userid2"]);
Response.Write(Request.Cookies["MyCook"].Value);//輸出全部的值
}
}
//修改Cookie
protected void Button3_Click(object sender, EventArgs e)
{
//獲取客戶端的Cookie對象
HttpCookie cok = Request.Cookies["MyCook"];
if (cok != null)
{
//修改Cookie的兩種方法
cok.Values["userid"] = "alter-value";
cok.Values.Set("userid", "alter-value");
//往Cookie裏加入新的內容
cok.Values.Set("newid", "newValue");
Response.AppendCookie(cok);
}
}
//刪除Cookie
protected void Button4_Click(object sender, EventArgs e)
{
HttpCookie cok = Request.Cookies["MyCook"];
if (cok != null)
{
if (!CheckBox1.Checked)
{
cok.Values.Remove("userid");//移除鍵值為userid的值 } else { TimeSpan ts = new TimeSpan(-1, 0, 0, 0); cok.Expires = DateTime.Now.Add(ts);//刪除整個Cookie,只要把過期時間設置為現在 } Response.AppendCookie(cok);
}
}
session: 該對象是HttpSession 類型的對象,描述一個客戶端與服務器之間的一次通話時
段,該段時間內包含客戶端的若幹次請求和服務器的相應響應,整個時間段session 對象都
存在。常用來實現購物車之類的存儲當前用戶的數據。不同用戶有各自的不同session 對象。
application: 該對象是ServletContext 類型的對象,描述的是本身Web 程序。該對象在
Web 程序部署到tomcat 服務器時由容器產生,其生命周期至Web 程序從tomcat
服務器卸載出去時消失。是所有客戶端能共享的一個
全局對象,整個系統只有一份。
Cookie以文件的形式保存的請求信息
c#反射
反射的定義:審查元數據並收集關於它的類型信息的能力,元數據(編輯後的基本數據單元)就是一大堆表,編譯器會創建一個類定義表,一個字段定義表,一個方法定義表等,System.Reflection命名空間包含的幾個類,允許你反射(解析)這些元數據的代碼。
簡單來理解:就是編譯器在編譯反射相關的代碼時,會將所有的類、方法、屬性等分別創建一個表,而每個表的元素都對應著一部分元數據中的代碼。當我們利用類名稱字符串去創建這個類中,程序會在保存類中表中找到我們提供的這個類名稱,然後再找到對應的構造函數,並執行。
反射中的類型
MemberInfo-反射類的成員
ConstructorInfo-反射類的構造函數
FieldInfo-反射類的字段
MethodInfo-反射類的構造函數的方法
PropertyInfo-反射類的構造函數的屬性
EventInfo-反射類的構造函數的事件
代碼
using ReflectionInjection.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Reflection
{
class Program
{
static void Main1(string[] args)
{
Assembly assembly = Assembly.Load("TestClass"); //TestClass程序集dll文件
Type[] types = assembly.GetTypes();
Type type = types[0];
string className = "Reflection." + "Student";
string className2 = "Reflection." + "Person"; //Person代表的是這個程序集裏面要查找的類;Reflection 代表的是程序集
// string className3 = "ReflectionInjection.Helper." + "MacButtonn";
Student stu= Assembly.Load("Reflection").CreateInstance(className) as Student;
Person person = Assembly.Load("Reflection").CreateInstance(className2) as Person;
// MacButtonn macButton = Assembly.Load("ReflectionInjection").CreateInstance(className3) as MacButtonn;
Console.ReadKey();
}
static void Main(string[] args)
{
Assembly assembly = Assembly.Load("TestClass");
Type[] types = assembly.GetTypes(); //獲取數據集中所有的數據類型(即我們在dll中定義的類)
Type type = types[0];
object obj1 = Activator.CreateInstance(type);
FieldInfo[] listFinfo = type.GetFields(); //字段
foreach (FieldInfo finfo in listFinfo)
{
var name = finfo.GetValue(obj1); //字段的值
Console.WriteLine("字段的名字 " + finfo.Name+"字段的值"+name);
}
PropertyInfo[] listPropertyInfo = type.GetProperties(); //屬性
foreach (PropertyInfo propertyInfo in listPropertyInfo) {
var name = propertyInfo.GetValue(obj1, null); //屬性的值
Console.WriteLine("屬性的名字 " + propertyInfo.Name);
}
MethodInfo[] minfos = type.GetMethods(); //方法
foreach (MethodInfo me in minfos)
{
if (me.Name == "Run")
{
object o=me.Invoke(obj1, null); //調用方法
string ss = "nihao";
}
}
Console.WriteLine(type.FullName);
Console.ReadKey();
}
}
public class Student
{
public string _name = "dsc";
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Run()
{
return "Hello World";
}
}
}
namespace TestClass
{
class Person
{
public string _name = "dsc";
public string Name { get; set; }
public int Age { get; set; }
public string Run()
{
return "你好,我是測試類";
}
}
}
抽象工廠
工廠方法模式之所以可以解決簡單工廠的模式,是因為它的實現把具體產品的創建推遲到子類中,此時工廠類不再負責所有產品的創建,而只是給出具體工廠必須實現的接口,這樣工廠方法模式就可以允許系統不修改工廠類邏輯的情況下來添加新產品,這樣也就克服了簡單工廠模式中缺點。下面看下工廠模式的具體實現代碼(這裏還是以簡單工廠模式中點菜的例子來實現):
namespace 設計模式之工廠方法模式
{
/// <summary>
/// 菜抽象類
/// </summary>
public abstract class Food
{
// 輸出點了什麽菜
public abstract void Print();
}
/// <summary>
/// 西紅柿炒雞蛋這道菜
/// </summary>
public class TomatoScrambledEggs : Food
{
public override void Print()
{
Console.WriteLine("西紅柿炒蛋好了!");
}
}
/// <summary>
/// 土豆肉絲這道菜
/// </summary>
public class ShreddedPorkWithPotatoes : Food
{
public override void Print()
{
Console.WriteLine("土豆肉絲好了");
}
}
/// <summary>
/// 抽象工廠類
/// </summary>
public abstract class Creator
{
// 工廠方法
public abstract Food CreateFoddFactory();
}
/// <summary>
/// 西紅柿炒蛋工廠類
/// </summary>
public class TomatoScrambledEggsFactory:Creator
{
/// <summary>
/// 負責創建西紅柿炒蛋這道菜
/// </summary>
/// <returns></returns>
public override Food CreateFoddFactory()
{
return new TomatoScrambledEggs();
}
}
/// <summary>
/// 土豆肉絲工廠類
/// </summary>
public class ShreddedPorkWithPotatoesFactory:Creator
{
/// <summary>
/// 負責創建土豆肉絲這道菜
/// </summary>
/// <returns></returns>
public override Food CreateFoddFactory()
{
return new ShreddedPorkWithPotatoes();
}
}
/// <summary>
/// 客戶端調用
/// </summary>
class Client
{
static void Main(string[] args)
{
// 初始化做菜的兩個工廠()
Creator shreddedPorkWithPotatoesFactory = new ShreddedPorkWithPotatoesFactory();
Creator tomatoScrambledEggsFactory = new TomatoScrambledEggsFactory();
// 開始做西紅柿炒蛋
Food tomatoScrambleEggs = tomatoScrambledEggsFactory.CreateFoddFactory();
tomatoScrambleEggs.Print();
//開始做土豆肉絲
Food shreddedPorkWithPotatoes = shreddedPorkWithPotatoesFactory.CreateFoddFactory();
shreddedPorkWithPotatoes.Print();
Console.Read();
}
}
}
淺談JS中的!=、== 、!==、===的用法和區別 JS中Null與Undefined的區別 讀取XML文件 獲取路徑的方式 C#中Cookie,Session,Application的用法與區別? c#反射 抽象工廠