OO设计问题.
我想将单例功能继承到不同的类层次结构中.这意味着他们需要在每个层次结构中拥有自己的单例实例.
这是我想要做的一个简短的例子:
class CharacterBob : public CCSprite, public BatchNodeSingleton {
...
}
class CharacterJim : public CCSprite, public BatchNodeSingleton {
...
}
class BatchNodeSingleton {
public:
BatchNodeSingleton(void);
~BatchNodeSingleton(void);
CCSpriteBatchNode* GetSingletonBatchNode();
static void DestroySingleton();
static void initBatchNodeSingleton(const char* asset);
protected:
static CCSpriteBatchNode* m_singletonBatchNode;
static bool singletonIsInitialized;
static const char* assetName;
};
Run Code Online (Sandbox Code Playgroud)
此代码将导致Jim和Bob共享BatchNodeSingleton的受保护成员.我需要他们各自拥有自己的套装.什么是一个好的解决方案?可以通过assetName作为键查找的指针集合?
真的很感激你的想法.
我有两个班GenericNameValue,并SpecificNameValue与继承GenericNameValue.
我有一个带参数的函数List<GenericNameValue>.我希望能够传入List<SpecificNameValue>.该函数对该函数的特殊属性不起作用SpecificNameValue.
这样做的最佳方法是什么?
public class GenericNameValue
{
public string FieldName{get;set;}
public string FieldValue{get;set;}
}
public class SpecificNameValue : GenericNameValue
{
public string SpecificFieldValue{ get; set; }
}
public static UtitlityClass
{
public string CombineAllFields(List<GenericNameValue> mylist)
{
//.... do stuff with each item
}
}
//......Example of calling the utilityclass
string stuff = UtilityClass.CombineAllFields(mySpecificNameValue);
Run Code Online (Sandbox Code Playgroud)
那么我缺少一个特定的语法吗?我应该使用像Abstracts这样的东西吗?
对不起,这只是导致我头疼一段时间的事情之一,并希望一个优雅的解决方案.
关于泛化和继承的研究让我感到困惑,两者都给出了相似的含义,好像某些东西被继承了......但是无法弄清楚实际的差异.
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Run Code Online (Sandbox Code Playgroud)
从上面可以看出,如果我们说这MyClass是类并且public static void main(String[] args) 是方法,那么我想知道代码的哪一部分将被视为对象.
如果我们说类有对象和对象都有方法,那么在上面的代码中,哪个部分是对象?这里有没有创建对象?
在我的C++程序中,我想创建一个具有宽度,高度,区域等属性的对象.我还想声明使用和更新此属性的方法.
我希望"set"和"get"这些方法以某种方式列在名为WidthManipulator的头文件,名称空间或子类(无论如何是可能的)中.
我想以这种方式创建我的结构的原因是我想使用"get"名称作为另一个类的另一个方法,比如HeightManipulator.
但对于嵌套类,我得到了Rectangle :: WidthManipulator :: Get()的"非静态成员函数非法调用"错误.我也不想创建Manipulator对象,因为这些类没有属性,只是使用和更新父属性的方法......还有一件事,我想使用void返回,这是我自己的一个很好的理由.
class Rectangle{
public:
int width,height;
int area;
int widthreturned;
class WidthManipulator{
public:
void Set(int x){width = x;}
void Get(){widthreturned = width};
};
};
Run Code Online (Sandbox Code Playgroud)
我该如何处理我的问题?我的结构应该是什么?
我有一个自定义类结构如下.
public interface Stuff { }
public Thing : Stuff
{
public new String ToString() { return "That's the thing!"; }
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的代码的其他部分,我有一个方法,将String对象作为参数.第一行编译,而第二行不编译.我认为在发送对象时默认调用ToString.并且,继承自Object类的Stuff应该已经实现了ToString(在我的情况下,也被我的实现所掩盖).
Thing thing = new Thing();
MustHaveString(thing.ToString());
MustHaveString(thing);
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我正在Apple的Objective-C指南中进行面向对象编程.在Dynamism - > Dynamic Binding的主题下,有三个概念:
我几乎了解动态和后期绑定之间的差异,但静态绑定会让人感到困惑.有人可以用Objective-C或C++中的例子解释这三个概念之间的差异吗?
注意:在您考虑将此问题标记为重复之前,没有任何问题可以同时描述这三个问题.
我正在用Java开发一个基本的纸牌游戏,我已经准备了一组Managers在它们之间进行交互的子类(like PlayerManager interacting with DeckManager)但是,要实现这一点,我需要在每个需要它的类中放置一个Manager.所以,例如:
if PlayerManager needs to a card from a deck --> executes drawCard() inside DeckManager,
so inside PlayerManager there's a DeckManager object.
Run Code Online (Sandbox Code Playgroud)
问题是,如果这是正确的方法,我不会这样做,因为每次Manager需要另一个时,我都必须在需要它的人中创建所需的经理.
有没有更好的办法?我想过做一个包含所有的东西Managers并制作它的类static,但我不知道它是否在Java中是好的.
我正在经历一些面向对象的学习设计问题,我遇到了这个问题,其中书籍目录中需要一个 Book 对象。这是问题的解决方案中建议的 Book 对象。
public class Book {
private long ID;
private String details;
private static Set<Book> books;
public Book(long iD, String details) { ... }
public static void addBook(long iD, String details){
books.add(new Book(iD, details));
}
public void update() { }
public static void delete(Book b) { books.remove(b); }
public static Book find(long id){
for (Book b : books)
if(b.getID() == id) return b;
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
一方面,这个Book对象对我来说看起来非常好,因为它包含修改/获取有关书籍对象的信息以及有关书籍的数据所需的所有方法。因此,当使用 OOP 的定义时,这看起来很棒,因为这就是对象应该是什么。
但在我 1-2 年的编程生涯中,我一直认为创建、删除的方式。修改一个对象应该通过一个服务层来完成,在这种情况下本质上是一个 BookService 类,它包含使用不包含这些 …
我目前正在尝试将我们的页面模板转换为OOP,我感觉我为导航类提出的内容并不是完全正确的.
drawNav吗?getMenuBar- > generateMenuBar- > generateMenuItems结构太崩溃?它应该只是getMenuBar,并将所有内容放入generateMenuBar()和generateMenuItems()放入getMenuBar()?我调用类和方法的方式:
$drawNav = new drawNav();
$breadcrumbTrail = $drawNav->getBreadcrumbTrail();
$menuBar = $drawNav->getMenuBar();
Run Code Online (Sandbox Code Playgroud)
代码:
class drawNav {
public function __construct() {
//I’ve not nothing to put here…
}
public function getMenuBar()
{
return $this->generateMenuBar();
}
public function getBreadcrumbTrail()
{
return $this->generateBreadcrumbTrail();
}
public function getSocialMediaButtons()
{
return $this->generateSocialMediaButtons();
}
private function generateSocialMediaButtons()
{
//return the HTML code with the social media …Run Code Online (Sandbox Code Playgroud)