下面的代码包含两个类:
我希望能够像这样实例化单数和复数类(即我不想要工厂方法GetSmartForm()):
SmartForms smartForms = new SmartForms("all");
SmartForm smartForm = new SmartForm("id = 34");
Run Code Online (Sandbox Code Playgroud)
要合并逻辑,只有复数类才能访问数据库.当被要求实例化时,单数类将简单地实例化一个复数类,然后从复数对象的集合中选择一个对象并成为该对象.
我怎么做?我试图分配this不起作用的对象.
using System.Collections.Generic;
namespace TestFactory234
{
public class Program
{
static void Main(string[] args)
{
SmartForms smartForms = new SmartForms("all");
SmartForm smartForm = new SmartForm("id = 34");
}
}
public class SmartForm
{
private string _loadCode;
public string IdCode { get; set; }
public string Title { get; …Run Code Online (Sandbox Code Playgroud) 如何在另一个方法中访问在构造函数中实例化的对象?(例如下面的对象b)实例化这个对象的最佳方法是什么,这样我的所有类方法都可以访问同一个对象?
public class ClassA{
private final int size;
public ClassA(int N){
size = N;
ClassB b = new ClassB(size);
}
public void doSomething(){
b.doSomething();
}
}
Run Code Online (Sandbox Code Playgroud) 我想从我的bitset表示的数字中减去一个整数.但是,我不知道如何a)将bitset复制到另一个bitset,或者b)重新实例化一个bitset:
bitset<7> bits(5);
int newresult=bits.to_ulong();
newresult=newresult-1;
bits=bitset<7> tempbits(newresult); // of course doesn't work
Run Code Online (Sandbox Code Playgroud)
重要的是我的最终结果是位 bitset.怎么做得好?
我对python相对较新,但我认为我有足够的理解,除了(显然)使用"import"语句的正确方法.我认为这是问题,但我不知道.
我有
from player import player
def initializeGame():
player1 = player()
player1.shuffleDeck()
player2 = player()
player2.shuffleDeck()
Run Code Online (Sandbox Code Playgroud)
和
from deck import deck
class player(object):
def __init__(self):
self.hand = []
self.deck = deck()
def drawCard(self):
c = self.deck.cards
cardDrawn = c.pop(0)
self.hand.append(cardDrawn)
def shuffleDeck(self):
from random import shuffle
shuffle(self.deck.cards)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试初始化游戏时,它说"播放器1尚未被定义",我不确定为什么.在同一个文件中,如果我只使用"player1 = player()"那么它完全没问题,但它拒绝在函数内部工作.有帮助吗?
编辑:添加之前未包含的内容
class deck(object):
def __init__(self):
self.cards = []
def viewLibrary(self):
for x in self.cards:
print(x.name)
def viewNumberOfCards(self, cardsToView):
for x in self.cards[:cardsToView]:
print(x.name)
from deck import deck
class player(object):
def …Run Code Online (Sandbox Code Playgroud) 这是代码的格式:
class C
{
public:
C();
virtual ~C() = 0;
};
class D : public C
{
public:
D();
~D();
};
C::C(){
}
C::~C(){
}
D::D(){
}
D::~D(){
}
int main(){
C *c = new C();
D *d = new D();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试实例化时,c我收到以下错误:
1>c:\main.cpp(59): error C2259: 'C' : cannot instantiate abstract class
Run Code Online (Sandbox Code Playgroud)
我知道我无法调用虚拟析构函数,但在概念上有一些我不知道的东西.有人可以解释一下吗?
我正在编写Acellerated c ++第15章的代码.我或多或少地直接从本书中复制了代码,除了在某些地方他们在头文件的类体中定义了类似构造函数的东西,然后我把它们分开了避免链接错误.
以下是代码; 我试图在Visual Studio 2010中编译它,但不幸的是它失败了.它告诉我它不能创建"String_Pic"和其他派生类(Frame_Pic,HCat_Pic和VCat_Pic)的实例,因为它说它们仍然是抽象类.它说罪魁祸首是"显示"功能,它说是未定义的.但是,我清楚地为每个派生类定义它,如下所示.
这里发生了什么?
标题:
#ifndef _GUARD_PIC_BASE_H
#define _GUARD_PIC_BASE_H
#include "Ptr.h"
#include <iostream>
#include <string>
#include <vector>
class Picture;
class Pic_base {
friend std::ostream& operator<<(std::ostream&, const Picture&);
friend class Frame_Pic;
friend class HCat_Pic;
friend class VCat_Pic;
friend class String_Pic;
typedef std::vector<std::string>::size_type ht_sz;
typedef std::string::size_type wd_sz;
virtual wd_sz width() const = 0;
virtual ht_sz height() const = 0;
virtual void display(std::ostream, ht_sz, bool) const = 0;
public:
virtual ~Pic_base(){ }
protected:
static void pad(std::ostream&, wd_sz, wd_sz);
}; …Run Code Online (Sandbox Code Playgroud) 好吧,我只是想试试C++(我现在做C#),现在我的第一个问题是类实例化或在另一个类中调用方法.
这是我的代码:我的主要程序条目:
#include <iostream>
#include "myClass.h"
using namespace std;
int main()
{
myClass* mc = new myClass();
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试访问的类:
#include "myClass.h"
myClass::myClass()
{
void DoSomething();
{
}
}
myClass::~myClass()
{
}
Run Code Online (Sandbox Code Playgroud)
班级标题:
#pragma once
class myClass
{
public:
myClass();
~myClass();
};
Run Code Online (Sandbox Code Playgroud)
现在你可以看到我已经实例化了这个类,但我无法访问DoSomething()方法.
我一直在寻找stackoverflow,但还没有看到ansswer.注意:我确实在C++中看到了一些好的响应,但没有看到C#.
检查null是否有任何好处,if (reader == null)如下所示,或者导致它为null的问题已导致异常if (reader == null)无法访问(reader == null为true)?
try
{
var reader = new PhotoReader();
if (reader == null)
{
throw new InvalidOperationException("PhotoReader could not be created.");
}
}
catch (Exception ex)
{
// let user know failed object creation, etc.
<...>
}
Run Code Online (Sandbox Code Playgroud) 我遇到了一段有趣的Java代码片段.我研究了newInstance()是什么,它意味着避免调用构造函数并创建对象的新实例.但是看一下示例代码我不明白一件事:为什么不需要构造函数?
public class SimpleContentFragment extends WebViewFragment {
protected static SimpleContentFragment newInstance(String file) {
SimpleContentFragment f=new SimpleContentFragment();
Bundle args=new Bundle();
args.putString(KEY_FILE, file);
f.setArguments(args);
return(f);
}
}
Run Code Online (Sandbox Code Playgroud)
否则此代码中的其他位置是否创建了构造函数.没有
public SimpleContentFragment() {
// Required empty public constructor
}
Run Code Online (Sandbox Code Playgroud)
正如我所料.
那么你能用newInstance澄清静态方法中发生了什么吗?new SimpleContentFragment()当构造函数从未写入时它怎么能调用?
我有一个class Piece我已经放置了默认构造函数private,因为我想在创建对象时只使用特定的构造函数:
class Piece
{
private:
Piece();
public:
Piece (int var1, int var2);
~Piece();
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个class Game有vector of Pieces:
class Game
{
private:
std::vector<Piece> m_pieces;
public:
Game();
~Game();
CreatePieces(); //<-- only here, I will create the Piece objects, not in the constructor
}
Run Code Online (Sandbox Code Playgroud)
现在我想要一个class Foo,其中包含Piece:
class Foo
{
private:
Piece m_piece;//ERROR!!! cannot access private member declared in class 'Piece'
public:
Foo();
~Foo();
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
现在我需要使用默认的构造函数m_piece上Foo …
instantiation ×10
c++ ×5
c# ×2
java ×2
object ×2
android ×1
bitset ×1
constructor ×1
copy ×1
instance ×1
methods ×1
null ×1
oop ×1
pure-virtual ×1
python ×1