`
bulote
  • 浏览: 1305286 次
文章分类
社区版块
存档分类
最新评论

构造方法何时被调用

 
阅读更多

新建一个对象是被调用。也就是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("徐越");

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics