在我的代码中,我被指示从GetCustomer
类的CustomerManager
类的Customer
类的public class customerManager
{
private static int currentCusNo;
private int maxCustomers;
private int numCustomers;
customer[] cList;
public customerManager(int maxCust, int seed)
{
currentCusNo = seed;
maxCustomers = maxCust;
numCustomers = 0;
cList = new customer[maxCustomers];
}
public bool addcustomers(string fN, string lN, string ph)
{
if (numCustomers > maxCustomers) return false;
customer m = new customer(fN, lN, ph, currentCusNo);
currentCusNo++;
cList[numCustomers] = m;
numCustomers++;
return true;
}
public int findCustomer(int cusID)
{
for (int x = 0; x < numCustomers; x++)
{
if (cList[x].getID() == cusID)
{
return x;
}
}
return -1;
}
public bool customerExist(int cusID)
{
for (int x = 0; x < numCustomers; x++)
{
if (cList[x].getID() == cusID)
{
return true;
}
}
return false;
}
public string customerlist()
{
string y = " ";
for (int x = 0; x < numCustomers; x++)
{
y += "\nFirst Name: " + cList[x].getFirstName() + "\nLast name: " + cList[x].getLasttName() + "\nCustomer ID: " + cList[x].getID();
}
return y;
}
public customer GetCustomer(int cID)
{
for (int x = 0; x < numCustomers; x++)
{
}
}
}
方法中返回一个对象。我是面向对象编程的新手,所以我对如何执行此操作没有任何想法。有人可以帮我吗?
客户经理类
public class customer
{
private int customerID;
private string firstName;
private string lastName;
private string phone;
public customer(string fN, string lN, string ph, int cId)
{
customerID = cId;
firstName = fN;
lastName = lN;
phone = ph;
}
public int getID() { return customerID; }
public string getFirstName() { return firstName; }
public string getLasttName() { return lastName; }
public string getPhone() { return phone; }
public string toString()
{
string s = "";
s +="First Name: " + firstName + "\nLast Name: " + lastName + "\nPhone: " + phone + "\nCustomer ID: " + customerID;
return s;
}
}
}
客户分类
<div id="footer" style="height: 20px; width: 100%; position: absolute; bottom: 0;">footer</div>
index.php
<?php
// This page has litte content so the footer is at bottom as expected
<?php require_once 'footer.php';?>
?>
otherpage.php
<?php
// This page has much more content, such as headers, menus, tables, so, to see all its contents we must scroll the page. And guess what? The footer stands in the middle of screen, instead of bottom (not what we wanted!)
<?php require_once 'footer.php';?>
?>
}
答案 0 :(得分:2)
当有人在C#中说“返回一个对象”时,通常表示:“返回对我想要的东西的引用”。幸运的是,类始终总是作为引用存储(至少出于本讨论的目的),因此这非常简单。
代码将与您的findCustomer
方法几乎相同,但是它不返回客户索引,而是返回客户参考。
public customer GetCustomer(int cID)
{
for (int x = 0; x < numCustomers; x++)
{
customer testCustomer = cList[x];
if (testCustomer.getID() == cusID)
{
return testCustomer;
}
}
return null;
}
我明确地将testCustomer
变量放入其中,以便
cList
是一系列客户,您可以拉出他们的引用。最后还有一些有用的提示:
Customer
)get
数据处理方法是不常见的,通常它们只是通过属性公开答案 1 :(得分:0)
使用您的代码...
public customer GetCustomer(int cID)
{
int index = findCustomer(cID);
if (index != 1)
return cList[index];
return null;
}
找到所需cID的索引,并在此索引中返回列表项;如果找不到,则返回null。
或者...
复制并粘贴findCustomer代码,将return x;
和return cList[x];
分别由-1
和null
更改;
答案 2 :(得分:0)
看起来它与您的findCustomer
方法非常相似,只是返回实际的int
对象而不是返回cutomer
。如果我遵循与您课堂其余部分相同的模式,则看起来会像这样:
public customer GetCustomer(int cID)
{
for (int x = 0; x < numCustomers; x++)
{
if (cList[x].getID() == cID)
{
return cList[x];
}
}
return null;
}
但是应该说,如果您使用的是System.Linq
,则只需一行就可以完成相同的方法:
public customer GetCustomer(int cID)
{
return cList.Where(x => x.getID() == cID).SingleOrDefault();
}
干杯
JM