gpt4 book ai didi

oop - 如何将组合与继承结合使用?

转载 作者:行者123 更新时间:2023-12-04 09:57:18 26 4
gpt4 key购买 nike

我将尝试在一个简单的例子中提出我的问题......

假设我有一个抽象基类 Car。汽车有一个基本的引擎对象。我在抽象 Car 类中有一个方法 StartEngine() ,它将引擎的启动委托(delegate)给 Engine 对象。

如何允许 Car 的子类(例如 Ferrari)将 Engine 对象声明为特定类型的引擎(例如 TurboEngine)?我需要另一个汽车类(TurboCar)吗?

我继承了一个普通的旧 Engine 对象,我无法在我的 Car 子类中将它重新声明(或覆盖)为 TurboEngine。

编辑:我知道我可以将 Engine 的任何子类插入到我的 Ferrari 类中的 myEngine 引用中......但是我如何调用只有 TurboEngine 公开的方法?因为 myEngine 是作为基础引擎继承的,所以不包括任何涡轮增压的东西。

谢谢!

最佳答案

抽象工厂模式正是针对这个问题。 Google GoF Abstract Factory {您的首选语言}

在下文中,请注意如何使用具体工厂来生成“完整”对象(enzo、civic)或使用它们来生成相关对象的“族”(CarbonFrame + TurboEngine、WeakFrame + WeakEngine)。最终,您总是会得到一个 Car 对象,该对象以特定类型的行为响应Accelerator()。

     using System;


abstract class CarFactory
{
public static CarFactory FactoryFor(string manufacturer){
switch(manufacturer){
case "Ferrari" : return new FerrariFactory();
case "Honda" : return new HondaFactory();
default:
throw new ArgumentException("Unknown car manufacturer. Please bailout industry.");
}
}

public abstract Car createCar();
public abstract Engine createEngine();
public abstract Frame createFrame();

}

class FerrariFactory : CarFactory
{
public override Car createCar()
{
return new Ferrari(createEngine(), createFrame());
}

public override Engine createEngine()
{
return new TurboEngine();
}

public override Frame createFrame()
{
return new CarbonFrame();
}
}

class HondaFactory : CarFactory
{
public override Car createCar()
{
return new Honda(createEngine(), createFrame());
}

public override Engine createEngine()
{
return new WeakEngine();
}

public override Frame createFrame()
{
return new WeakFrame();
}
}

abstract class Car
{
private Engine engine;
private Frame frame;

public Car(Engine engine, Frame frame)
{
this.engine = engine;
this.frame = frame;
}

public void accelerate()
{
engine.setThrottle(1.0f);
frame.respondToSpeed();
}

}

class Ferrari : Car
{
public Ferrari(Engine engine, Frame frame) : base(engine, frame)
{
Console.WriteLine("Setting sticker price to $250K");
}
}

class Honda : Car
{
public Honda(Engine engine, Frame frame) : base(engine, frame)
{
Console.WriteLine("Setting sticker price to $25K");
}
}

class KitCar : Car
{
public KitCar(String name, Engine engine, Frame frame)
: base(engine, frame)
{
Console.WriteLine("Going out in the garage and building myself a " + name);
}
}

abstract class Engine
{
public void setThrottle(float percent)
{
Console.WriteLine("Stomping on accelerator!");
typeSpecificAcceleration();
}

protected abstract void typeSpecificAcceleration();
}

class TurboEngine : Engine
{
protected override void typeSpecificAcceleration()
{
Console.WriteLine("Activating turbo");
Console.WriteLine("Making noise like Barry White gargling wasps");
}
}

class WeakEngine : Engine
{
protected override void typeSpecificAcceleration()
{
Console.WriteLine("Provoking hamster to run faster");
Console.WriteLine("Whining like a dentist's drill");
}
}

abstract class Frame
{
public abstract void respondToSpeed();
}

class CarbonFrame : Frame
{
public override void respondToSpeed()
{
Console.WriteLine("Activating active suspension and extending spoilers");
}
}

class WeakFrame : Frame
{
public override void respondToSpeed()
{
Console.WriteLine("Loosening bolts and vibrating");
}
}

class TestClass
{
public static void Main()
{
CarFactory ferrariFactory = CarFactory.FactoryFor("Ferrari");
Car enzo = ferrariFactory.createCar();
enzo.accelerate();

Console.WriteLine("---");
CarFactory hondaFactory = CarFactory.FactoryFor("Honda");
Car civic = hondaFactory.createCar();
civic.accelerate();

Console.WriteLine("---");
Frame frame = hondaFactory.createFrame();
Engine engine = ferrariFactory.createEngine();
Car kitCar = new KitCar("Shaker", engine, frame);
kitCar.accelerate();

Console.WriteLine("---");
Car kitCar2 = new KitCar("LooksGreatGoesSlow", hondaFactory.createEngine(), ferrariFactory.createFrame());
kitCar2.accelerate();
}
}

关于oop - 如何将组合与继承结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/279158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com