说我有以下课程:
class Animal
{
public long Id { get; set; }
public string Name { get; set; }
}
class Dog:Animal
{
public void sniffBum()
{
Console.WriteLine("sniff sniff sniff");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个实例Animal,我该如何将其转换为Dog?像这样的东西:
Animal a = new Animal();
if ( some logic to determine that this animal is a dog )
{
Dog d = (Dog)a;
d.sniffBum();
}
Run Code Online (Sandbox Code Playgroud)
基本上我不能使用接口.我将永远有一个Animal像我这样的数据库.Dog没有任何参数Animal,只有新方法.
我可以创建一个新Dog对象,并传递值,(或者有一个带有类型的构造函数Animal),但这看起来很麻烦.
在C++中,在动态绑定期间,请考虑以下示例...
class Base
{
virtual void fun()
{
cout<<"Base";
}
};
class Derived : Base
{
void fun()
{
cout<<"Derived";
}
};
int main()
{
Base *bptr;
Derived d;
bptr=&d;
bptr->fun();
}
Run Code Online (Sandbox Code Playgroud)
由于虚拟关键字/动态绑定的声明,上述函数的输出是"Derived".
根据我的理解,将创建一个包含虚函数地址的虚拟表(Vtable).在这种情况下,为派生类创建的虚拟表指向继承的虚拟表fun().并且bptr->fun()将会得到解决,bptr->vptr->fun();.这指向继承的基类函数本身.我不完全清楚如何调用派生类函数?
#include <iostream>
using namespace std;
int prim( long long x ) {
int s = 0;
for( long long i = 1; i <= x ; i++ ) {
if( x % i == 0 ) {
s++;
}
}
if( s == 2 ) {
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
int main() {
long long A = 600851475143;
long long i = 2;
long long C = 0;
while( i < (A/2) ) {
while( A % i …Run Code Online (Sandbox Code Playgroud) 我有一个从Web服务获得的Json字符串; 它有一个集合列表,每个集合代表一个对象,例如:
[ // Root List
[ // First Collection : Team Object
{
"id": 1,
"team_name": "Equipe Saidi",
"is_active": true,
"last_localisation_date": "2015-05-06T13:33:15+02:00"
},
{
"id": 3,
"team_name": "Equipe Kamal",
"is_active": true,
"last_localisation_date": "2015-05-06T09:22:15+02:00"
}
],
[// Second Collection : user Object
{
"id": 1,
"login": "khalil",
"mobile_password": "####",
"first_name": "Abdelali",
"last_name": "KHALIL",
"email": "KHALIL@gmail.com",
"role": "DR",
"is_active": true,
"charge": false
},
{
"id": 2,
"login": "ilhami",
"mobile_password": "####",
"first_name": "Abdellah",
"last_name": "ILHAMI",
"email": "ILHAMI@gmail.com",
"role": "DR",
"is_active": true,
"charge": …Run Code Online (Sandbox Code Playgroud) 我一直致力于静态类型语言(C/C++,Java).我一直在玩Clojure,我真的很喜欢它.
我担心的一件事是:说我有一个窗口,需要3个模块作为参数,并且需求发生变化,我需要将另一个模块传递给函数.我只是更改了函数,编译器在我使用它的任何地方都会抱怨.但在Clojure中,在调用函数之前不会抱怨.我可以做一个正则表达式搜索和替换,但似乎有机会错过一个调用,它将被忽视,直到该函数实际被调用.你们怎么处理这个?
我已经创建了自己的异常,但是当我尝试使用它时,我会收到一条消息,说它无法转换为我的异常
我有一个像这样的界面
public interface MyInterface
{
public OtherClass generate(ClassTwo two, ClassThree three) throws RetryException;
}
Run Code Online (Sandbox Code Playgroud)
其他像这样的
public class MyGenerator{
public class generate (ClassTwo two, ClassThree three){
try{
}catch(MyException my)
}
}
Run Code Online (Sandbox Code Playgroud)
最后是另一个类中的方法
public Object evaluate(String expression, Map values) throws FirstException, RetryException
{
try{
}catch (Exception x){
if(x instanceof FirstException){
throw new FirstException()
}
else{
RetryException retry= (RetryException)x;
retry.expression = expression;
retry.position = position;
retry.operator = tokens[position];
retry.operand = 1;
throw retry;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个try catch块在最后一个方法是进行数学运算,我想在上面捕获一个除零异常RetryException.
我知道之间的区别的auto,auto&,const auto和const auto&(例如在"每个"循环),但有一两件事令我惊讶的是:
std::string bla;
const std::string& cf()
{
return bla;
}
int main (int argc, char *argv[])
{
auto s1=cf();
const std::string& s2=cf();
s1+="XXX"; // not an error
s2+="YYY"; //error as expected
}
Run Code Online (Sandbox Code Playgroud)
那么有人可以告诉我何时x表达式auto x = fun();中的类型与返回值的类型不是同一类型fun()?
我测试过boost::property_tree它很好:我可以加载XML,提取元素,保存XML等等.但是,是否可以生成XML并打印它?我不想保存它.
void debug_settings::load(const std::string &filename) {
using boost::property_tree::ptree;
ptree pt;
read_xml(filename, pt);
m_file = pt.get<std::string>("debug.filename");
m_level = pt.get("debug.level", 0);
BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.modules"))m_modules.insert(v.second.data());
}
void debug_settings::save(const std::string &filename) {
using boost::property_tree::ptree;
ptree pt;
pt.put("debug.filename", m_file);
pt.put("debug.level", m_level);
BOOST_FOREACH(const std::string &name, m_modules)pt.add("debug.modules.module", name);
write_xml(filename, pt);
}
Run Code Online (Sandbox Code Playgroud)
这是我用来加载和保存XML的函数.我们有任何方法来展示它吗?
Doxygen 文档应该放在包含防护之前还是之后?在名称空间之前或内部?
假设标头包含单个类 ( ) 的声明context,这就是我在这里记录的内容。
#ifndef CONTEXT_HPP
#define CONTEXT_HPP
#include <string>
/**
* The application context interface.
*/
namespace test {
class context { ... };
}
Run Code Online (Sandbox Code Playgroud) 我在使用 Codeblocks 创建和添加 makefile 项目时遇到了一些问题。我创建了一个包含 3 个文件的项目: main.cpp; 查看.cpp; 查看.h.
主.cpp:
#include <iostream>
#include "View.h"
using namespace std;
int main(int argc, char** argv) {
View view;
view.box();
}
Run Code Online (Sandbox Code Playgroud)
查看.cpp:
#include <iostream>
#include "View.h"
using namespace std;
void View::box()
{
int i=3;
switch(i)
{
case 1:
break;
case 2:
break;
case 3:
break;
}
cout<<"AAAA";
};
Run Code Online (Sandbox Code Playgroud)
查看.h:
#ifndef VIEW_H_INCLUDED
#define VIEW_H_INCLUDED
class View
{
//// ****************************
//// ---------------------------
//// ---------------------------
public :
void box();
//// ****************************
};
#endif
Run Code Online (Sandbox Code Playgroud)
和 Makefile:
all …Run Code Online (Sandbox Code Playgroud)