閱讀999 返回首頁    go 阿裏雲 go 技術社區[雲棲]


構造方法何時被調用

 

新建一個對象是被調用。也就是new的時候;
如:
public class A

{
int i;
String c;
public A(){ }                  //無參構造方法
public A(int i,String c) // 兩參構造方法
{
   this.i = i;
   this.c = c;  
}
public static void main(String[] args)

{
   A a = new A() ;               //調用了無參構造方法;
  
   A a1 = new A(5,"vieri");//調用了兩參構造方法
}
}

 

 

 

稍微高級一點的用法

        string path = null;

        public XDocument doc;

        public StudnetXMLDB(string filepath)
        {
            this.path = filepath;
            doc = XDocument.Load(path);
        }

        public static StudnetXMLDB ReadStudentXMLDB()
        {
            StudnetXMLDB newstuDB = new StudnetXMLDB(Application.StartupPath +

                                                     @"\config\StudentInformation.xml");
            return newstuDB;
        }

         

        public void Remove(string name)
        {
            doc.Element("Students").Elements("student").Where(ss => ss.Attribute("Name").Value ==

                                                                                  name).Remove();
            doc.Save(path);
        }

 

         要是調用Remove方法的時候,可以這樣表示

        StudnetXMLDB.ReadStudentXMLDB().Remove("xy");

最後更新:2017-04-02 22:15:58

  上一篇:go ADO.NET下的SqlBulkCopy類執行數據庫間批量複製操作
  下一篇:go 該用Tryparse了