阅读999 返回首页    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了