我正在重写我的项目,因此将来编辑起来更容易,并且想要实现界面。
我已经实现了该接口,但是它在MainWindow中不起作用,我无法调用该方法。
所以我尝试使用PalindromeChecker作为默认实现
PalindromeChecker = new PalindromeChecker();
,所以我可以调用该方法,但是它不起作用。
interface ICheckPalindrome
{
bool IsPalindrome(string text);
}
public class PalindromeChecker : ICheckPalindrome
{
/// <summary>
/// Method for checking if the word/text is a palindrome.
/// </summary>
public bool IsPalindrome(string text)
{
......
//Code
}
}
}
namespace TextChecker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
lblInput.Foreground = Brushes.ForestGreen;
lblResult.Foreground = Brushes.ForestGreen;
lblTitel.Foreground = Brushes.ForestGreen;
}
/// <summary>
/// User input and checking the input if the word a palindrome is.
/// </summary>
private void InputText_TextChanged(object sender, TextChangedEventArgs e)
{
string text = InputText.Text;
bool isPalindrome = PalindromeChecker.IsPalindrome(text);
OutputText.Text = text + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");
if (InputText.Text == string.Empty)
{
OutputText.Clear();
}
}
private void ButtonClicked(object sender, RoutedEventArgs e)
{
SubWindow subWindow = new SubWindow();
subWindow.Show();
}
}
}
public class PalindromeChecker : ICheckPalindrome
{
/// <summary>
/// Method for checking if the word/text is a palindrome.
/// </summary>
public bool IsPalindrome(string text)
{
int min = 0;
int max = text.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = text[min];
char b = text[max];
if (a != b)
{
return false;
}
min++;
max--;
}
}
}
我真的很困在这里,在此先感谢您。
答案 0 :(得分:3)
据我们所知,您不希望接口,类 instances 检查回文(您是否真的想实现几种算法< / em>可供选择?),但是使用static
方法:
// Let's class be partial one: if you want to add a method it it
// you don't have to modify this code but
// add a chunk public partial static class MyStringRoutine {...}
public partial static class MyStringRoutine {
public static bool IsPalindrome(string text) {
//DONE: do not forget about special cases
if (string.IsNullOrEmpty(text))
return true;
for (int i = 0; i < text.Length / 2; ++i)
if (text[i] != text[text.Length - 1 - i])
return false;
return true;
}
}
然后您可以使用它:
private void InputText_TextChanged(object sender, TextChangedEventArgs e) {
if (string.IsNullOrEmpty(InputText.Text))
OutputText.Clear();
else {
string suffix = MyStringRoutine.IsPalindrome(InputText.Text)
? "is a palindrome"
: "is NOT a palindrome";
OutputText.Text = $"{InputText.Text} {suffix}";
}
}
如果必须实现ICheckPalindrome
接口并因此要使用类实例,则必须创建实例:
private void InputText_TextChanged(object sender, TextChangedEventArgs e) {
if (string.IsNullOrEmpty(InputText.Text))
OutputText.Clear();
else {
// You have to create the instance (checker)
ICheckPalindrome checker = new PalindromeChecker();
// IsPalindrome is the instance method; you can't call is as
// PalindromeChecker.IsPalindrome
string suffix = checker.IsPalindrome(InputText.Text)
? "is a palindrome"
: "is NOT a palindrome";
OutputText.Text = $"{InputText.Text} {suffix}";
}
}
答案 1 :(得分:1)
这可能会有所帮助:
$filename = 'foo.bar';
答案 2 :(得分:0)
从您发布的代码看来,接口ICheckPalindrome
的访问权限似乎比PalindromeChecker
少。不知道编译器将如何以及为什么让它溜走,但是您应该尝试将代码更改为
public interface ICheckPalindrome
{
bool IsPalindrome(string text);
}
因此,接口将反映其实现方式(或相反)。