C#上机练习题代码如何实现
C#上机练习题代码如何实现,面向对象,继承相关,使用抽象工厂模式实现
using System;
namespace ConsoleApp1
{
class Program
{
public abstract class Factory { public abstract Animal Create(); }
public class MonkeyFactory : Factory
{
public override Animal Create() { return new Monkey(); }
}
public class TiggerFactory : Factory
{
public override Animal Create() { return new Tigger(); }
}
public class BirdFactory : Factory
{
public override Animal Create() { return new Bird(); }
}
public abstract class Animal
{
public abstract void Eat();
public abstract void DoAction();
public abstract void Sound();
}
public class Monkey : Animal
{
public override void Eat()
{
Console.WriteLine("吃香蕉");
}
public override void DoAction()
{
Console.WriteLine("上树");
}
public override void Sound()
{
Console.WriteLine("吱吱");
}
}
public class Tigger : Animal
{
public override void Eat()
{
Console.WriteLine("吃肉");
}
public override void DoAction()
{
Console.WriteLine("奔跑");
}
public override void Sound()
{
Console.WriteLine("哇哇");
}
}
public class Bird : Animal
{
public override void Eat()
{
Console.WriteLine("吃虫");
}
public override void DoAction()
{
Console.WriteLine("飞走");
}
public override void Sound()
{
Console.WriteLine("叽叽喳喳");
}
}
static void Main(string[] args)
{
Console.Write("请输入动物名称:");
var name = Console.ReadLine().Trim() ;
Factory f;
if (name == "老虎") f = new TiggerFactory();
else if (name == "猴子") f = new MonkeyFactory();
else f = new BirdFactory();
var a = f.Create();
while (true)
{
Console.Write("请输入指令:e d w,其他退出:");
var cmd = Console.ReadLine();
switch (cmd)
{
case "e":a.Eat(); break;
case "d":a.DoAction(); break;
case "w":a.Sound(); break;
default:cmd = "";break;
}
if (cmd == "") break;
}
}
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!需求:
抽象工厂模式最早的应用之一是用来创建在不同操作系统的图形环境下都能够运行的应用程序,例如同时支持Windows和Linux系统。在每一个操作系统中,都有一个由图形构件组成的构建家族,可以通过一个抽象角色给出功能定义,而由具体子类给出不同操作系统下的具体实现,现在系统中包含了两个产品等级结构,分别是Button和Text,同时包含两个产品族Windows和Linux,请使用抽象工厂模式设计一个系统满足以上条件。
抽象工厂类:
public interface IGUIFactory
{
IButtonComponent CreateButtonComponent();
ITextComponent CreateTextComponent();
}
具体工厂类:
namespace NewFactory.AbstructFactory.Question7
{
public class WindowsGUIFactory : IGUIFactory
{
public IButtonComponent CreateButtonComponent()
{
return new WindowsButton();
}
public ITextComponent CreateTextComponent()
{
return new WindowsText();
}
}
}
namespace NewFactory.AbstructFactory.Question7
{
public class LinuxGUIFactory : IGUIFactory
{
public IButtonComponent CreateButtonComponent()
{
return new LinuxButton();
}
public ITextComponent CreateTextComponent()
{
return new LinuxText();
}
}
}
抽象产品类:
namespace NewFactory.AbstructFactory.Question7
{
public interface IButtonComponent
{
}
}
namespace NewFactory.AbstructFactory.Question7
{
public interface ITextComponent
{
}
}
具体产品类:
这里可以看到我多提了两个父类,原因是可以通过这个父类和反射来减少代码的书写。
namespace NewFactory.AbstructFactory.Question7
{
public class WindowsButton : IButtonComponent, IWindows
{
}
}
namespace NewFactory.AbstructFactory.Question7
{
public class WindowsText : ITextComponent, IWindows
{
}
}
namespace NewFactory.AbstructFactory.Question7
{
public class LinuxButton : ILinux, IButtonComponent
{
}
}
namespace NewFactory.AbstructFactory.Question7
{
public class LinuxText : ILinux, ITextComponent
{
}
}
可以使用C#内置的String类中的IndexOf方法来实现统计某一字符在字符串中出现的次数。
具体实现步骤如下:
获取用户输入的字符串,以及要统计出现次数的字符。
定义一个计数变量count,用于记录字符出现的次数。
使用String类的IndexOf方法搜索字符串中第一次出现要统计的字符的位置,如果找到,则将count加1,然后从该位置之后的子串继续搜索,直到整个字符串中没有要统计的字符。
返回count,即为所求的字符在字符串中出现的次数。
代码示例:
string input = Console.ReadLine();
char target = Console.ReadKey().KeyChar;
int count = 0;
int index = input.IndexOf(target);
while (index != -1)
{
count++;
index = input.IndexOf(target, index + 1);
}
Console.WriteLine("字符 '{0}' 在字符串中出现了 {1} 次。", target, count);
其中,第一个读取用户输入的字符串的语句使用Console.ReadLine(),第二个读取要统计的字符的语句使用Console.ReadKey().KeyChar,可以直接获取用户输入的字符而无需按下回车确认。如果需要提示用户输入要统计的字符,则可以使用Console.WriteLine("请输入要统计的字符:")和Console.ReadKey().KeyChar。