構造方法何時被調用
新建一個對象是被調用。也就是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