帮忙纠正这个代码,涉及到对象的调用,不会..>_<
<%@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>
<%@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>
{
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.
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。编译器错误消息: CS0117: “ASP.aspnet_engine_object_aspx.Car”并不包含“MyEngine”的定义源错误:行 112: Engine MyEngine=new Engine("Xeer", "070","V6");
行 113:
行 114: AlexEngine=ShumaCar.MyEngine.Name;
行 115: RobEngine=ShumaCar.MyEngine.Name;
行 116: MyEngine=ShumaCar.MyEngine.Name;
修改以后还是不行啊,这个代码应该怎么改???<%@page language="C#" runat="server" Debug="true"%>
<script runat="server">
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.";
} }
}
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 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 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");
AlexEngine=ShumaCar.MyEngine.Name;
RobEngine=ShumaCar.MyEngine.Name;
MyEngine=ShumaCar.MyEngine.Name;
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("Ferrari", "050", "V10");
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write("<hr>Attempting to start ShumaCar with RobEngine:");
ShumaCar.MyEngine(RobEngine);
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write("<hr>Attempting to start ShumaCar with MyEngine:");
ShumaCar.MyEngine(MyEngine);
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write("<br/>Attempting to stop ShumaCar:");
}
</script>
{
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.";
} }
}
<%@page language="C#" runat="server" Debug="true"%>
<script runat="server">
public class Engine
{
private string name;
private string rpm;
private string serialno;
public Engine(string Name, string Rpm, string SerialNo)
{
name=Name;
rpm=Rpm;
serialno=SerialNo;
}
public static string Name//是不是这样?不行啊
{
get
{
return name;
}
}
public static string Rpm
{
get
{
return rpm;
}
}
public static string SerialNo
{
get
{
return serialno;
}
}
行 113:
行 114: AlexEngine=ShumaCar.MyEngine.Name; 上边的Name是Engine实例MyEngine的属性,
而MyEngine又是ShumaCar的属性
so
先构造Engine属性,如第一行代码,再将它赋给ShumaCar的MyEngine属性
之后再使用ShumaCar.MyEngine.Name
里只有一个objEngine
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
编译器错误消息: CS0117: “ASP.aspnet_engine_object_aspx.Car”并不包含“MyEngine”的定义<%@page language="C#" runat="server" Debug="true"%>
<script runat="server">
public class Engine
{
private string name;
private string rpm;
private string serialno;
private string Name2;
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.";
} }
}
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 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 Page_Load()
{
Car ShumaCar=new Car();
Engine AlexEngine=new Engine("Ferrari", "050", "V10");
Engine RobEngine=new Engine("Fdd", "060", "V3");
Engine MyEngine=new Engine("Xeer", "070", "V6");
ShumaCar.objEngine=new Engine("Xeer", "070","V6"); ShumaCar.objEngine=new Engine("Ferrari", "050", "V10");
ShumaCar.objEngine=new Engine("Fdd", "060", "V3");
ShumaCar.objEngine=new Engine("Xeer", "070", "V6");
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("Ferrari", "050", "V10");
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write(" <hr>Attempting to start ShumaCar with RobEngine:");
ShumaCar.MyEngine(RobEngine);
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write(" <hr>Attempting to start ShumaCar with MyEngine:");
ShumaCar.MyEngine(MyEngine);
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write(" <br/>Attempting to stop ShumaCar:");
}</script>
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(147,5): error CS1502: 与“System.Web.HttpResponse.Write(string)”最匹配的重载方法具有一些无效参数
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(147,20): error CS1503: 参数“1”: 无法从“void”转换为“string”
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(151,29): error CS0119: “ASP.aspnet_engine_object_aspx.Car.MyEngine()”是一个“方法”,这在给定的上下文中无效
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(155,29): error CS0119: “ASP.aspnet_engine_object_aspx.Car.MyEngine()”是一个“方法”,这在给定的上下文中无效
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(21,9): error CS0127: 由于“ASP.aspnet_engine_object_aspx.Engine.SwitchOn(string, string, string)”返回 void,返回关键字后面不得有对象表达式
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(25,9): error CS0127: 由于“ASP.aspnet_engine_object_aspx.Engine.SwitchOn(string, string, string)”返回 void,返回关键字后面不得有对象表达式
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(29,9): error CS0127: 由于“ASP.aspnet_engine_object_aspx.Engine.SwitchOn(string, string, string)”返回 void,返回关键字后面不得有对象表达式
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(53,11): error CS1002: 应输入 ;
c:\inetpub\wwwroot\ASPNET\engine_object.aspx(64,18): error CS0103: 当前上下文中不存在名称“count”帮忙啊,高手呢??
<%@page language="C#" runat="server" Debug="true"%>
<script runat="server">
public class Engine
{
private string name;
private string rpm;
private string serialno;
private string Name2;
public Engine(string Name, string Rpm, string SerialNo)//如果这里有参数名称,那么实例化一个engine的时候也要在括号内表明参数名称(并非参数)
{
name=Name;
rpm=Rpm;
serialno=SerialNo;
} public void SwitchOn(string Name, string Rpm, string SerialNo)
{
if(Name=="Xeer" && Rpm=="070" && SerialNo=="V6")
{
return "The engine is running.";
}
else if(Name=="Ferrari" && Rpm=="050" && SerialNo=="V10")
{
return "The engine is not running.";
}
else if(Name=="Fdd" && Rpm=="060" && SerialNo=="V3")
{
return "The engine is not running.";
}
}
}
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 MyEngine()
{
get
{
return engine;
}
}
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 Page_Load()
{
Car ShumaCar=new Car();
Engine AlexEngine=new Engine("Ferrari", "050", "V10");
Engine RobEngine=new Engine("Fdd", "060", "V3");
Engine MyEngine=new Engine("Xeer", "070", "V6");
ShumaCar.objEngine=new Engine("Xeer", "070","V6"); 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:"); Response.Write(ShumaCar.objEngine.SwitchOn("Ferrari", "050", "V10"));
Response.Write(" <hr>Attempting to start ShumaCar with RobEngine:");
ShumaCar.objEngine=new Engine("Fdd", "060", "V3");
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write(" <hr>Attempting to start ShumaCar with MyEngine:");
ShumaCar.objEngine=new Engine("Xeer", "070", "V6");
Response.Write(ShumaCar.MyEngine.SwitchOn);
Response.Write(" <br/>Attempting to stop ShumaCar:");
}</script>