我有一个基本的表单,我想通过调用ActionResultView的关联Controller类中的方法来处理表单内的按钮.以下是表单的以下HTML5代码:
<h2>Welcome</h2>
<div>
<h3>Login</h3>
<form method="post" action= <!-- what goes here --> >
Username: <input type="text" name="username" /> <br />
Password: <input type="text" name="password" /> <br />
<input type="submit" value="Login">
<input type="submit" value="Create Account"/>
</form>
</div>
<!-- more code ... -->
Run Code Online (Sandbox Code Playgroud)
相应的Controller代码如下:
[HttpPost]
public ActionResult MyAction(string input, FormCollection collection)
{
switch (input)
{
case "Login":
// do some stuff...
break;
case "Create Account"
// do some other stuff...
break;
}
return View();
}
Run Code Online (Sandbox Code Playgroud) 尝试使用"Head First Design Patterns"一书(用Java编写)中的代码在C#中实现装饰器模式.
我刚刚开始使用C#,因此我仍然对语法不熟悉,所以我不确定为什么我不能让下面的注释代码行工作.
这是Decorator模式中的第一个抽象基类及其派生类:
using System;
public abstract class Beverage
{
private String m_description;
// get a description of the beverage
public virtual String Description { get { return m_description; } }
// calculate cost of the beverage
public abstract double Cost();
}
// HouseBlend coffee implements Beverage
public class HouseBlend : Beverage
{
// Constructor
public HouseBlend() { m_description = "House Blend"; }
// calculate base cost of House Blend
public override double Cost() { return 0.89; …Run Code Online (Sandbox Code Playgroud) 我有一个项目依赖于从另一个包导入的结构,我将其称为TheirEntity.
在下面的示例中,我(咳咳)嵌入TheirEntity了MyEntity,它是 的扩展TheirEntity,并添加了功能。
但是,我不想TheirEntity在MyEntity结构中导出,因为我宁愿消费者不TheirEntity直接访问。
我知道 Go 嵌入与经典 OOP 中的继承不同,所以这可能不是正确的方法,但是是否可以将嵌入结构指定为“私有”,即使它们是从另一个包导入的?如何以一种更惯用的方式实现同样的事情?
// TheirEntity contains functionality I would like to use...
type TheirEntity struct {
name string
}
func (t TheirEntity) PrintName() {
fmt.Println(t.name)
}
func NewTheirEntity(name string) *TheirEntity {
return &TheirEntity{name: name}
}
// ... by embedding in MyEntity
type MyEntity struct {
*TheirEntity // However, I don't want to expose
// TheirEntity directly. How to embed this …Run Code Online (Sandbox Code Playgroud) 我正在尝试动态地在MainWindow类中创建布局.我有四个用网格布局对象放置的框架.每个帧包含一个自定义ClockWidget.我想在调整主窗口大小时相应地调整ClockWidget对象的大小,所以我需要将它们添加到布局中.但是,我需要在运行时执行此操作,因为对象本身是在运行时创建的.我尝试以编程方式完成此操作,但下面注释掉的代码尝试创建新布局会导致程序崩溃.正确执行此操作的步骤是什么?
头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ClockView.h"
namespace Ui{
class MainWindow;
}
class QLayout;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void populateViewGrid();
private:
Ui::MainWindow *ui;
ClockView *clockView_1;
ClockView *clockView_2;
ClockView *clockView_3;
ClockView *clockView_4;
QLayout *layout_1;
QLayout *layout_2;
QLayout *layout_3;
QLayout *layout_4;
};
#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)
实施文件:
#include <QVBoxLayout>
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
populateViewGrid();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::populateViewGrid() …Run Code Online (Sandbox Code Playgroud) 在"Clojure in Action"中进行以下示例(p.63):
(defn basic-item-total [price quantity]
(* price quantity))
(defn with-line-item-conditions [f price quantity]
{:pre [(> price 0) (> quantity 0)]
:post [(> % 1)]}
(apply f price quantity))
Run Code Online (Sandbox Code Playgroud)
评估REPL:
(with-line-item-conditions basic-item-total 20 1)
Run Code Online (Sandbox Code Playgroud)
结果抛出以下异常:
Don't know how to create ISeq from: java.lang.Long
[Thrown class java.lang.IllegalArgumentException]
Run Code Online (Sandbox Code Playgroud)
在评估应用程序后,似乎抛出了异常.
exception clojure first-class-functions higher-order-functions
我知道这是一个常见的问题,但是尽管mysql-connector在IntelliJ IDEA中添加了我的库,IDE仍然无法找到该类.正如您在下面的屏幕截图中看到的,我已经将mysql-connectorjar 添加为我的项目的库,但它似乎没有看到它存在:



除了'将库添加到项目'之外,我还没有找到解决方案.好像在某个地方有一个缺失的步骤......
我很难理解为什么我会收到此错误.我指的是Josuttis的STL书和其他资源,似乎我在下面声明我的迭代器的方式应该有效:
#ifndef LRU_H
#define LRU_H
#include <queue>
#include <iterator>
class LRU
{
public:
LRU(); // default constructor
LRU(int); // constructor with argument
~LRU(); // destructor
// Methods
//
void enqueue(int); // add datum to the queue
void dequeue(); // remove datum from the queue
void replace(); // replacement algorithm
void displayQueue() const; // display contents of queue
private:
// Member Data
//
const int MAX_SIZE;
int m_currentCount;
std::queue<int> m_buffer;
std::queue<int>::const_iterator iter;
};
#endif
Run Code Online (Sandbox Code Playgroud)
但是我声明我的const_iterator的行会生成以下编译器错误:
In file included from main.cpp:10:
lru.h:41: …Run Code Online (Sandbox Code Playgroud) 调用派生类中定义的方法时,我收到编译器错误.编译器似乎认为我所指的对象是基类类型:
weapon = dynamic_cast<Weapon*>(WeaponBuilder(KNIFE)
.name("Thief's Dagger")
.description("Knife favored by Thieves")
.attack(7) // error: class Builder has no member called attack
.cost(10) // error: class Builder has no member called cost
.build());
Run Code Online (Sandbox Code Playgroud)
实际上,Builder不包含任何一个attack或cost:
class Builder
{
protected:
string m_name;
string m_description;
public:
Builder();
virtual ~Builder();
virtual GameComponent* build() const = 0;
Builder& name(string);
Builder& description(string);
};
Run Code Online (Sandbox Code Playgroud)
但派生类WeaponBuilder确实:
enum WeaponType { NONE, KNIFE, SWORD, AXE, WAND };
class WeaponBuilder : public Builder
{
int …Run Code Online (Sandbox Code Playgroud) 这个方法:
void LRU::displayQueue() const
{
for(iter = m_buffer.begin(); iter != m_buffer.end(); ++iter)
std::cout << (*iter) << " ";
std:: cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
lru.cpp:58: error: passing 'const std::_Deque_iterator<int, const int&, const int*>' as 'this' argument of 'std::_Deque_iterator<int, const int&, const int*>& std::_Deque_iterator<int, const int&, const int*>::operator=(const std::_Deque_iterator<int, const int&, const int*>&)' discards qualifiers
Run Code Online (Sandbox Code Playgroud)
m_buffer并iter在我的头文件中声明,其中缓冲区被声明为deque类型int并且iter是常量迭代器:
// ...
std::deque<int> m_buffer;
std::deque<int>::const_iterator iter;
// ...
Run Code Online (Sandbox Code Playgroud)
取出const的displayQueue方法将消除编译器错误,但由于在这个功能应该不修改任何数据deque,我想保持我的代码"常量纠正",以更加明确.当我的迭代器是一个时,为什么会导致错误 const_iterator …
我试图通过键访问地图数据结构的元素,但我得到编译器错误.我使用typedef定义了我的地图数据结构,以简化地图实例化的语法.如您所见,键是类型string,数据是自定义GameComponent对象:
typedef map<string, GameComponent*> ComponentMap;
typedef map<string, GameComponent*>::iterator ComponentMapIter;
typedef map<string, GameComponent*>::const_iterator ComponentMapCIter;
Run Code Online (Sandbox Code Playgroud)
在派生类中GameComponent,我正在创建标准的Composite模式方法以及存储在我的map中的每个唯一GameComponent对象的访问器.但是,使用数组下标运算符访问访问器中的对象会导致编译器错误:
void Character::add(const string& key, GameComponent* comp)
{
m_components->insert( make_pair(key, comp) );
}
void Character::remove(const string& key)
{
m_components->erase(key);
}
Armor* Character::getArmor() const
{
// ERROR:
return static_cast<Armor*>(m_components["Armor"]);
}
Weapon* Character::getWeapon() const
{
// ERROR:
return static_cast<Weapon*>(m_components["Weapon"]);
}
Attributes* Character::getAttributes() const
{
// ERROR:
return static_cast<Attributes*>(m_components["Attributes"]);
}
Run Code Online (Sandbox Code Playgroud)
编译器错误的输出显示"无效类型"错误,让我摸不着头脑:
/Users/Dylan/Desktop/RPG/character.cpp: In member function 'Armor* Character::getArmor() const':
/Users/Dylan/Desktop/RPG/character.cpp:66: error: invalid types 'ComponentMap* const[const …Run Code Online (Sandbox Code Playgroud)