帮忙纠正这个代码,涉及到对象的调用,不会..>_<
<%@page language="C#" runat="server" Debug="true"%>
<script runat="server">
public class Car
{
private string color;
private int gear;
private int ignition;
private Engine engine;
public Engine objEngine
{
get
{
return engine;
}
set
{
engine=value;
}
}
public void Engine(string Name, int Rpm, string SerialNo)
{
name=Name;
rpm=Rpm;
serialno=SerialNo;
}
public static int Count
{
get
{
return count;
}
}
public string Color
{
get
{
return color;
}
set
{
color=value;
}
}
public int Gear
{
get
{
return gear;
}
}
public void ChangeGear(int direction)
{
if(direction<0)gear-=1;
if(direction>0)gear+=1;
if(gear>5)gear=5;
if(gear<-1)gear=-1;
}
public void ChangeGear(string direction)
{
if(direction.ToLower()=="down")
{
ChangeGear(-1);
}
if(direction.ToLower()=="up")
{
ChangeGear(+1);
}
}
public void SwitchOn(string Name)
{
if(Name==JayEngine)
{
return "The engine is running.";
}
else
{
return "The engine is not running.";
}
}
public void Page_Load()
{
Car ShumaCar=new Car();
ShumaCar.objEngine=new Engine("Xeer, 070,V6");
Engine AlexEngine=new Engine("Ferrari, 050, V10");
Engine RobEngine=new Engine("Fdd, 060, V3");
Engine MyEngine=new Engine("Xeer, 070,V6");
JayEngine=ShumaCar.MyEngine.Name;
JayEngine=ShumaCar.MyEngine.SwitchOn();
Response.Write("<b>New object 'ShumaCar' created.</b>");
Response.Write("<br/>Color:"+ShumaCar.Color);
Response.Write("<br/>Gear:" +ShumaCar.Gear);
ShumaCar.Color="Black";
ShumaCar.ChangeGear(+1);
Response.Write("<br/><b>Properties updated.</b>");
Response.Write("<br/>Color:" +ShumaCar.Color);
Response.Write("<br/>New gear:"+ShumaCar.Gear);
ShumaCar.Color="Black";
ShumaCar.ChangeGear(+1);
Response.Write("<br/><b>Properties updated.</b>");
Response.Write("<br/>Color:" +ShumaCar.Color);
Response.Write("<br/>New gear:"+ShumaCar.Gear);
ShumaCar.ChangeGear("Up");
Response.Write("<br/><b>Shifted 'up' one gear.</b>");
Response.Write("<br/>New gear:"+ShumaCar.Gear);
Response.Write("<hr>Attempting to start ShumaCar with AlexEngine:");
ShumaCar.MyEngine(AlexEngine);
Response.Write(MyCar.IsRunning);
Response.Write("<hr>Attempting to start ShumaCar with RobEngine:");
ShumaCar.MyEngine(RobEngine);
Response.Write(ShumaCar.IsRunning);
Response.Write("<hr>Attempting to start ShumaCar with MyEngine:");
ShumaCar.MyEngine(MyEngine);
Response.Write(ShumaCar.IsRunning);
Response.Write("<br/>Attempting to stop ShumaCar:");
ShumaCar.EngineOff();
Response.Write(ShumaCar.IsRunning);
}
</script>
------解决方案--------------------------------------------------------
public void Engine(string Name, int Rpm, string SerialNo)
{
name=Name;
rpm=Rpm;
serialno=SerialNo;
}
有问题,
既然 有
private Engine engine;
public Engine objEngine
{
get
{
return engine;
}
set
{
engine=value;
}
}
那就证明你在别的地方定义了 Engine 类,
但是这里又不论类的写个类的构造函数 public void Engine(string Name, int Rpm, string SerialNo) (还写错了);
应该把 方法 public void Engine(string Name, int Rpm, string SerialNo)
放到 定义Engine类的地方,并且去掉void.
------解决方案--------------------------------------------------------
把Engine 字段构造成属性再访问
public class Engine
{
private string name; //构造成属性
private string rpm;
private string serialno;
public Engine(string Name, string Rpm, string SerialNo)//如果这里有参数名称,那么实例化一个engine的时候也要在括号内表明参数名称(并非参数)
{
name=Name;
rpm=Rpm;
serialno=SerialNo;
}
public void SwitchOn()
{
if(Name=="Xeer" && Rpm=="070" && SerialNo=="V6")
{
return "The engine is running.";
}
else
{
return "The engine is not running.";
}
}
}
------解决方案--------------------------------------------------------
Engine MyEngine=new Engine("Xeer", "070","V6");
行 113:
行 114: AlexEngine=ShumaCar.MyEngine.Name;
上边的Name是Engine实例MyEngine的属性,
而MyEngine又是ShumaCar的属性
so
先构造Engine属性,如第一行代码,再将它赋给ShumaCar的MyEngine属性
之后再使用ShumaCar.MyEngine.Name
------解决方案--------------------------------------------------------
ShumaCar.objEngine=new Engine("Xeer, 070,V6");
Engine AlexEngine=new Engine("Ferrari, 050, V10"); 这样的地方,你都要ShumaCar.objEngine==new Engine("Ferrari, 050, V10");这样来
Engine RobEngine=new Engine("Fdd, 060, V3");
Engine MyEngine=new Engine("Xeer, 070,V6");
调用的时候都是
ShumaCar.objEngine。Name