C# 插入类输出的替代或好方法
正如你们可以从代码中看到的,我创建了三个类:汽车,新模型,主。所有的基本方法都在car类中,我创建了继承类来尝试(继承)。正如您所看到的,我所做的只是输出car类的wheel()方法和newmodel继承类的newrims()方法,以生成一个完整的句子。需要建议以使代码更准确C# 插入类输出的替代或好方法,c#,C#,正如你们可以从代码中看到的,我创建了三个类:汽车,新模型,主。所有的基本方法都在car类中,我创建了继承类来尝试(继承)。正如您所看到的,我所做的只是输出car类的wheel()方法和newmodel继承类的newrims()方法,以生成一个完整的句子。需要建议以使代码更准确 namespace ConsoleApplication4 { public class car { public static void wheel() { Console.W
namespace ConsoleApplication4
{
public class car
{
public static void wheel()
{
Console.WriteLine("The wheels are rolling");
}
public static void doors()
{
Console.WriteLine("The Doors are automatic");
}
public static void engine()
{
Console.WriteLine("The engine of car is runing");
}
public static void oil()
{
Console.WriteLine("the oil is full in tank");
}
}
public class newmodel : car
{
public static void newrims()
{
car.wheel();
Console.WriteLine("And The new rims are rocking");
}
}
class Program
{
static void Main()
{
while (true)
{
Console.WriteLine("Press A to Roll the Wheels baby");
Console.WriteLine("Press B to Open/Close the Doors");
Console.WriteLine("Press C to Start the Car Engine");
Console.WriteLine("Press D to Check the Oil in tank");
Console.WriteLine("Press E to Rims/wheels");
Console.WriteLine("Press F to Exit the vehicle");
char c = Convert.ToChar(Console.ReadLine());
switch (c)
{
case 'a':
{
car.wheel();
break;
}
case 'b':
{
car.doors();
break;
}
case 'c':
{
car.engine();
break;
}
case 'd':
{
car.oil();
break;
}
case 'e':
{
newmodel.newrims();
break;
}
case 'f':
{
Environment.Exit(0);
break;
}
default:
{
Console.WriteLine("Please Enter the Correct Input");
break;
}
}
}
}
}
}
删除所有的静态。。。从类中创建一个Car和/或NewModel实例并使用这些实例 我没有实际的参考资料,但是需要考虑一些事情:
- 如果在大多数函数上使用static,则可能存在设计缺陷
- 您已经创建了一个基类和继承类,这很好
- 不使用静态函数,而是创建一个car对象(即currentCar=newcar(),并创建一个newModel实例)
- 使用这些实例上的函数而不是类,然后可以删除静态关键字
- 当使用一个单一变量(即currentVehicle,您可以从汽车中创建:即currentVehicle=new car())时,您可以稍后将其更改为新车型并使用新车型的功能,如currentVehicle=new newmodel()
- 通常,类是用大写字母编写的,所以Car和NewModel,以及不带大写字母的类的变量/实例:即Car=Car(),NewModel=NewModel()
namespace ConsoleApplication4
{
public class car
{
//note the absence of static keyword..it means it is an instance method
//note the virtual keyword..it means derived classes can override the behavior
public virtual void wheel()
{
Console.WriteLine("The wheels of car are rolling");
}
}
public class newmodel : car
{
//note the override keyword....it means newmodel overrides the wheel function
public override void wheel()
{
//depending on your needs you may or maynot
// need to call the base class function first
base.wheel();
Console.WriteLine("And The new rims are rocking");
}
}
class Program
{
static void Main()
{
//Instead of static methods you create a car object on
//on which you invoke member functions
car currentcar = new car();
while (true)
{
Console.WriteLine("Press A to Roll the Wheels baby");
Console.WriteLine("Press N to switch to new model");
char c = Convert.ToChar(Console.ReadLine());
switch (c)
{
case 'a':
{
currentcar.wheel();
break;
}
case 'n':
{
currentcar = new newmodel();
break;
}
default:
{
Console.WriteLine("Please Enter the Correct Input");
break;
}
}
}
}
}
}
您会注意到,按
a
将调用wheel功能,但这取决于您的汽车是普通的旧车还是新车型,它会在控制台上打印不同的内容。我不确定您到底想做什么,因为您使用的是switch语句来确定要调用哪些方法,而实际上您并不是u按照预期的方式选择继承或多态性。但是,让我在下面给您展示一个示例,说明您如何以不同的方式构造它
namespace ConsoleApplication4
{
public abstract class car
{
public virtual void wheel()
{
Console.WriteLine("The wheels are rolling");
}
public virtual void doors()
{
Console.WriteLine("The Doors are automatic");
}
public virtual void engine()
{
Console.WriteLine("The engine of car is runing");
}
public virtual void oil()
{
Console.WriteLine("the oil is full in tank");
}
}
public class standardmodel : car
{
}
public class newmodel : car
{
public override void wheel()
{
base.wheel();
Console.WriteLine("And The new rims are rocking");
}
}
class Program
{
static void Main()
{
while (true)
{
Console.WriteLine("Press A to Roll the Wheels baby");
Console.WriteLine("Press B to Open/Close the Doors");
Console.WriteLine("Press C to Start the Car Engine");
Console.WriteLine("Press D to Check the Oil in tank");
Console.WriteLine("Press E to Rims/wheels");
Console.WriteLine("Press F to Exit the vehicle");
char c = Convert.ToChar(Console.ReadLine());
Car standard = new standardcar(),
newModel = new newmodel();
switch (c)
{
case 'a':
{
standard.wheel();
break;
}
case 'b':
{
standard.doors();
break;
}
case 'c':
{
standard.engine();
break;
}
case 'd':
{
standard.oil();
break;
}
case 'e':
{
newModel.wheel();
break;
}
case 'f':
{
Environment.Exit(0);
break;
}
default:
{
Console.WriteLine("Please Enter the Correct Input");
break;
}
}
}
}
}
}
但是,总的来说,这并不是一个很好的例子,因为不清楚你想做什么。你可以在上面的代码中添加一些东西,使其更像OO,就是使用工厂来确定要创建哪种类型的
汽车,然后你就可以拥有一辆汽车。我希望这能回答你的问题。你正在寻找base
,而不是汽车
。我正在努力理解你的实际问题……这也是你的家庭作业吗?请尝试添加标签correspondingly@Mudu问题已更新。@sundar不,这不是我的作业:)您在这里收到一条错误消息,对吗?您可以将该错误消息添加到问题中以获得更好的帮助。我认为他也希望从car.wheel()
函数中获得输出。这可能是OP想要的。可能需要强调有关base.wheel()的实际更改。在原始问题中,newmodel
没有wheel()
函数,而是newrims()
函数。这将使它变得更容易,因为使用wheel()就足够了代码>@Default我添加了一些内联代码comments@parapurarajkumar谢谢你对我这样的初学者真的很有帮助:)太好了:)看起来不错我是初学者你能给我一些很好的回复吗?我在上面的回答中加了很多评论(在这种情况下,请把答案投上去,这样其他人也会从中受益。)谢谢,这真的帮了我很大的忙:)