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;
}
}
}
}
}
}
答案 0 :(得分:2)
您的案例中的继承示例将是这样的
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
将调用滚轮功能,但根据您的汽车是普通旧车还是新车型,它会向控制台打印不同的东西。
答案 1 :(得分:1)
删除所有静态...从类中创建Car和/或NewModel实例并使用实例。
我没有实际的参考资料,但有些事情需要考虑:
答案 2 :(得分:0)
我不确定你要做什么,因为你正在使用switch语句来确定调用哪些方法,而你实际上并没有按照预期的方式使用继承或多态。但是,让我向您展示一个如何以不同方式构建它的示例。
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的东西是使用工厂来确定要创建哪种类型的car
,然后您就可以拥有一辆汽车。我希望这能回答你的问题。