请看下面的代码
游戏对象.h
#pragma once
class GameObject
{
protected:
int id;
public:
int instances;
GameObject(void);
~GameObject(void);
virtual void display();
};
Run Code Online (Sandbox Code Playgroud)
游戏对象.cpp
#include "GameObject.h"
#include <iostream>
using namespace std;
static int value=0;
GameObject::GameObject(void)
{
value++;
id = value;
}
GameObject::~GameObject(void)
{
}
void GameObject::display()
{
cout << "Game Object: " << id << endl;
}
Run Code Online (Sandbox Code Playgroud)
回合.h
#pragma once
#include "GameObject.h"
class Round :
public GameObject
{
public:
Round(void);
~Round(void);
};
Run Code Online (Sandbox Code Playgroud)
圆形.cpp
#include "Round.h"
#include "GameObject.h"
#include <iostream>
using namespace std;
Round::Round(void)
{ …Run Code Online (Sandbox Code Playgroud) 我想(使用NUnit)测试以下类,特别是protected方法ProtectedMethod。
public class Foo
{
protected bool ProtectedMethod()
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
为了访问受保护的方法,我编写了一个Foo以这种方式继承的测试类:
[TestFixture]
internal class FooTestable : Foo
{
[Test]
public void ProtectedMethod_Test()
{
bool result = ProtectedMethod();
Assert.That(result);
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
FooTestable does not have a default constructor
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
这是测试受保护方法的最佳方法吗?
阅读这个关于方法排序的问题,我思考了在Python中将受保护的方法放在哪里以及它们应该是私有的_method(self)还是公共的method(self)。我知道 Python 不提供受保护方法的语言功能。
私有:按照约定,以下划线开头的属性是私有的。通常仍然可以从外部访问它们,但不应该。用下划线开头的受保护方法感觉很奇怪,因为不清楚子类实际上重写了该方法而不是声明它自己的实现细节。
Public:没有下划线,更有可能有人会查看基类以查看该方法是否已经存在。因此,这对于子类化的人来说更好。然而,想要使用子类的人不知道该方法只是一个实现细节,可能会尝试从外部调用它。
在 Python 中定义受保护方法的首选方法是什么?
我理解使clone和finalize方法保护的目的,我想理解为什么hashcode()和equals方法没有被声明为protected
我正在尝试为受保护的方法编写一个包装类,我希望对其进行单元测试.我的问题是,原始方法被声明为protected static new阻止我访问基本方法,因为它是静态的.
有没有其他方法来编写这个包装器方法?如果没有哪个其他选项我必须在不改变其范围的情况下对此方法进行单元测试?
编辑:已添加代码:
public class DerivedClassToTest : BaseClass
{
protected static new Type_A MehodeToTest()
{
Type_A A = new Type_A
{
//DoSomething...
};
return A;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何测试MehodeToTest?
我试图从我的Unitest中的DerivedClassToTest派生,所以我可以访问Protected方法但它的静态因此我不能调用base.MethodeToTest.我如何访问MethodeToTest
谢谢.
class A {
public:
A();
int get();
void set();
};
protected int A::var;
Run Code Online (Sandbox Code Playgroud)
好像它会起作用.但是,它"期望受保护之前的不合格身份".这样做的正确方法是什么?
我使用此代码来获取嵌入字体:
/// <summary>
/// Returns an Embedded Font
/// </summary>
/// <param name="ImagePath">String begins with namespace e.g MyProgram.Image.png</param>
/// <returns></returns>
public static Font GetEmbeddedFont(string FontPath, float Size)
{
Font _font = null;
Thread getFontThread = new Thread(() => GetFont(FontPath, Size, out _font));
getFontThread.Start();
getFontThread.Join();
return _font;
}
#region GetFont
private static void GetFont(string FontPath, float Size, out Font FontOut)
{
Font fnt = null;
Assembly asm = Assembly.GetExecutingAssembly();
Stream resStream = asm.GetManifestResourceStream(FontPath);
if (null != resStream)
{
//
// GDI+ …Run Code Online (Sandbox Code Playgroud) 我班上有这样的功能
public function browseNodeLookup($nodeId)
{
return $this->returnData(
$this->performSoapRequest("BrowseNodeLookup", $params)
);
}
Run Code Online (Sandbox Code Playgroud)
我这样用它
$response = $amazonEcs->browseNodeLookup($node);
var_dump($response);
Run Code Online (Sandbox Code Playgroud)
现在对象var_dump看起来像这样
object(SoapFault)#279 (10) {
["message":protected]=>
string(178) "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."
}
Run Code Online (Sandbox Code Playgroud)
我想回应那个消息.谁能告诉我怎么样?
我试过这样的
echo $response->message;
Run Code Online (Sandbox Code Playgroud)
但是因为它受到保护我会收到错误.
我将如何制作一个ElectricPump并在该对象中分配值"rate".我的当前代码出现以下错误:
../src/Boat.cpp:144:7: error: assignment of read-only member 'Pump::rate'
Run Code Online (Sandbox Code Playgroud)
以下是我的班级声明:
class Pump
{
protected:
Pump(float rate);
virtual void pump(Boat &) = 0;
const float rate;
};
class ElectricPump : public Pump
{
public:
ElectricPump();
virtual void pump(Boat &);
};
Run Code Online (Sandbox Code Playgroud)
执行:
Pump::Pump(float r) : rate(r){}
ElectricPump::ElectricPump(){
rate = 5.0;
}
Run Code Online (Sandbox Code Playgroud) 我对继承如何运作非常模糊,只是想确保我正在朝着正确的方向前进.
根据我的理解,我理解这一点
我知道子类可以看到父类的受保护变量.
我的问题:它是否相反?