为了节省一些写作,你为了节省一些阅读,我构建了一个基本的例子来帮助我解释我想要实现的目标.
假设您有一个同时使用堆栈类和队列类的程序,并且这两个类具有相同的方法/函数,最佳选择可能是创建ab抽象类并在两个类中继承它.如果类是通用的,那么抽象类也必须是通用的.所以这是一个抽象类如何看起来的例子:
public abstract class Sequence <T>
{
public abstract void push(T value);
public abstract T pop();
public abstract T peek();
public abstract int size();
public abstract bool isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
我们假设以下是Stack需要继承Sequence该类的类:
public class Stack<T>
{
private List<T> stk;
public Stack ()
{
stk = new List<T>();
}
public void push(T value)
{
stk.Add(value);
}
public T pop()
{
T topValue = peek();
stk.RemoveAt(size () - 1);
return topValue;
}
public T peek()
{
return stk[size() - …Run Code Online (Sandbox Code Playgroud) 我编写了以下程序来匹配C++中的正则表达式
#include <regex.h>
#include <iostream>
using namespace std;
/*
* Match string against the extended regular expression in
* pattern, treating errors as no match.
*
* return true for match, false for no match
*/
bool match(const char *string, char *pattern)
{
int status; regex_t re;
if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
return false;
/* report error */
status = regexec(&re, string, (size_t) 0, NULL, 0);
regfree(&re);
if (status != 0) {
return false; /* report error */
} …Run Code Online (Sandbox Code Playgroud)