如果我有一个定义为 Person 的类,它概述了人员的字段和一个只包含人员列表的人员,如下所示:
public class Person
{
public Person(){}
public int personID {get;set;}
public string name {get;set;}
public string address {get;set;}
}
public class People
{
public People(){}
public List<Person> peoples {get;set;}
public List<Person> getPeople()
{
List<Person> p = new List<Person>();
//create database connection
SqlConnection conn = new SqlConnection()
string SQLSelct = @"select * from people where active=1";
//open datareader
Person onePerson = new Person();
//write data to each property type and add data to list
p.Add(onePerson);
return p;
}
}
调用从数据库中获取人员的数据库方法。
static void main(string[] args)
{
People p = new People();
p.getPeople();
}
这就是我在 OOP 方面的做法吗?我刚刚开始在我的编码中使用这些技术,通常我会这样做来获取列表:
从 People 类中删除 getPeople() 并在 main 中执行此操作:
List<Person> people = getPeople();
static void main(string[] args)
{
List<Person> people = getPeople();
}
private static List<Person> getPeople()
{
List<Person> p = new List<Person>();
//create database connection
//open datareader
//load data into list
Person onePerson = new Person();
p.Add(onePerson);
return p;
}