小编Jon*_*han的帖子

使用策略模式的模式可以避免混凝土策略中的重复代码?

我对设计模式比较陌生,在下面的例子中,我使用的是我认为的策略模式.但是,我在一些,而不是所有的具体策略中重复自己,并想知道有没有办法避免这种情况?注意ACommand和CCommand在做一些独特的事情之前是如何拥有相同的代码的.

public interface Command 
{
    public boolean execute(CommandSender sender, String[] args);
    public String getName();
    //...
}

public abstract class PlayerCommand implements Command 
{
    protected BukkitPlugin plugin = BukkitPlugin.getInstance();

    private String name;
    //...

    public PlayerCommand(String name) 
    {
        this.name = name;
    }

    public String getName() 
    {
        return this.name;
    }

    //...
}
Run Code Online (Sandbox Code Playgroud)

ACommand

    public class ACommand extends PlayerCommand
    {
        public ACommand()
        {
            super("A");
        }

        public boolean execute(CommandSender sender, String[] args)
        {
            Player player = (Player) sender;
            PlayerInventory inventory = …
Run Code Online (Sandbox Code Playgroud)

java design-patterns

4
推荐指数
1
解决办法
1032
查看次数

如何使用SimpleXML获取带有命名空间的节点的属性?

youtube.xml

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">  

    <entry>
        ...
        <yt:duration seconds="1870"/>
        ...
    </entry>

</feed>
Run Code Online (Sandbox Code Playgroud)

update_videos.php

$source = 'youtube.xml';

// load as file
$youtube = new SimpleXMLElement($source, null, true);

foreach($youtube->entry as $item){
    //title works
    echo $item->title;

    //now how to get seconds? My attempt...
    $namespaces = $item->getNameSpaces(true);
    $yt = $item->children($namespaces['yt']);
    $seconds = $yt->duration->attributes();
    echo $seconds['seconds'];
    //but doesn't work :(
}   
Run Code Online (Sandbox Code Playgroud)

php xml namespaces simplexml

3
推荐指数
2
解决办法
1万
查看次数

如何在每10行SQL查询后显示文本?

假设我有一个查询"从用户中选择用户名".我想将此查询输出到PHP页面,但在每隔10行后我想显示自己的自定义文本.有任何想法吗?它将从第11个记录继续.

php sql mysqli

2
推荐指数
1
解决办法
2678
查看次数

按日期排序(varchar)?

我想按日期订购.

例如

table_date
February 2011
January 2011
December 2010
Run Code Online (Sandbox Code Playgroud)

我已经尝试过了:

SELECT distinct(table_date) FROM tables ORDER BY table_date DESC
Run Code Online (Sandbox Code Playgroud)

bur它不起作用.

我得到了这个:

January 2011
February 2011
December 2010
Run Code Online (Sandbox Code Playgroud)

你能帮我吗?

mysql

2
推荐指数
4
解决办法
2万
查看次数

如何让这些表堆叠在一起?

如何在没有额外空间的情况下让这些桌子相互滑动?

在此输入图像描述

<table border="0">
    <tbody>
        <tr>
            <th>And another</th>
        </tr>
        <tr>
            <td>Some stuff in the table. </td>
        </tr>
        <tr>
            <td>Some stuff in the table. </td>
        </tr>
        <tr>
            <td>Some stuff in the table. </td>
        </tr>
    </tbody>
</table>

<table border="0">
    <tbody>
        <tr>
            <th>And another</th>
        </tr>
        <tr>
            <td>Some stuff in the table. </td>
        </tr>
        <tr>
            <td>Some stuff in the table. </td>
        </tr>
    </tbody>
</table>

<table border="0">
    <tbody>
        <tr>
            <th>And another</th>
        </tr>
        <tr>
            <td>Some stuff in the table. </td>
        </tr>
        <tr>
            <td>Some stuff in the table. …
Run Code Online (Sandbox Code Playgroud)

html css

2
推荐指数
1
解决办法
1万
查看次数

如何将innerHTML与createTextNode一起使用?

我正在尝试创建一个chrome扩展,并通过使用XMLHttpRequest解析RSS提要,然后将解析后的标记(例如title&)存储description到数组中来实现此目的.

在解析了feed并将令牌存储到数组中之后,我遍历数组并使用DOM将它们显示在chrome扩展弹出窗口中.它的工作原理但问题是每个description都有HTML标签,并显示如下:

<p><img src="http://www.documentarywire.com/cover/college-inc.jpg" width="90" height="100" style="float:left; padding: 0 10px 20px 0"/></p>Even in lean times, the $400 billion business of higher education is booming. Nowhere is this more true than in one of the fastest-growing &#8212; and most controversial &#8212; sectors of the industry: for-profit colleges and universities that cater to non-traditional students, often confer degrees over the Internet, and, along the way, successfully capture billions [...]

HTML标记来自feed,我对它们没有任何问题我想要做的就是将它们实际解释为HTML而不是文本.我相信这是因为我正在使用createTextNode?我应该使用innerHTML?我不知道如何在下面的例子中使用innerHTML? …

html javascript dom

2
推荐指数
1
解决办法
7069
查看次数

如何使用PrimeFaces SelectOneMenu ItemLabel和ItemValue与列表?

我知道在SelectOneMenu中显示信息的正确方法是通过获取对象列表并使用它的属性,如下所示:

<p:selectOneMenu id="player" value="">  
    <f:selectItem itemLabel="Select" itemValue="" />  
    <f:selectItems value="#{players}"
       var="player"
       itemLabel="#{player.name}"
       itemValue="#{player.id}"/> 
</p:selectOneMenu> 
Run Code Online (Sandbox Code Playgroud)

但是,如果我没有一个球员名单,如果我有这样的东西怎么办?我想让它像这样工作:

//PlayerManager
public List<String> getPlayerNames() {
    String[] names = new String[] {"player1", "player2"};
    return Arrays.asList(names);
}

public List<String> getPlayerIds() {
   String[] ids = new String[] {"1", "2"};
   return Arrays.asList(ids);
}

<p:selectOneMenu id="player" value="">  
    <f:selectItem itemLabel="Select" itemValue="" />  
    <f:selectItems value="#{playerManager.playerNames}"
       var="player"
       itemLabel="#{playerManager.playerNames}"
       itemValue="#{playerManager.playerIds}"/> 
</p:selectOneMenu> 
Run Code Online (Sandbox Code Playgroud)

java jsf primefaces

2
推荐指数
1
解决办法
2万
查看次数

等到每个循环结束?

在forEach循环后直接添加回调不起作用.我是node.js的新手,所以任何帮助都会受到赞赏,谢谢.

这是一个Minecraft机器人,记录玩家登录和注销时间.在我从服务器被踢之后我想在尝试重新加入之前结束所有玩家会话但是在下面的代码中即使我使用async.series它仍然不等到第一个功能完成 - 这是由于我的回调的位置?

bot.on('kicked', function(reason) {
  console.log("I got kicked for", reason, "lol");
  var timestamp = getTimestamp();

    async.series([
      function(callback) {
        console.log("logging out all players");
        logoutAllPlayers(timestamp, function(finished) {
          console.log("logged out all players " + finished);
          if (finished) {
            callback();
          }
        });
      },
      function(callback) { //don't execute until previous function has run completely
        //rejoin here
        bot = mineflayer.createBot(options);
        bindlisteners(bot);
      }
    ]);
});
Run Code Online (Sandbox Code Playgroud)

...

回调的正确位置在哪里?

function logoutAllPlayers(timestamp, callback) {
      findOnlinePlayers(function(onlinePlayers) {
          if (onlinePlayers.length > 0) {
              onlinePlayers.forEach(function(player) {
                var playerId = …
Run Code Online (Sandbox Code Playgroud)

javascript node.js async.js

2
推荐指数
1
解决办法
2930
查看次数

帮助数据库设计

嘿,我对数据库设计很陌生,并且很难尝试解决这个问题.我有两个表Team和Fixtures.球队拥有多排橄榄球队,而Fixture每排有2支足球队(主队和客队).我想将团队ID链接到home_team和away_team,但它不允许我这样做.请告诉我如何解决这个问题.

这是我的表/关系的图像 http://i49.tinypic.com/288qwpg.jpg

database ms-access database-design

1
推荐指数
1
解决办法
1096
查看次数

为什么我收到此错误?致命错误:找不到类'TestCase'

即使我已经包含了TestCase类?

    <?php

require_once("PHPUnit/Autoload.php");
require_once("PHPUnit/Framework/TestCase.php");
require_once("PHPUnit/Framework/TestSuite.php");

class WidgetSession {
    public function __construct($one, $two){}
    public function login() {}
    public function isLoggedIn() {return null;}
    public function getUser(){
        return new WidgetUser();
    }
}

class WidgetUser{
    public $first_name = "";
    public $last_name = "";
    public $email = "";

    public function isSalesPerson() {return null;}
    public function isSalesManager() {return null;}
}

class TestWidgetSession extends TestCase {

    private $_session;
    function setUp(){
        $dsn = array(
            'phptype' => "pgsql",
            'hostspec' => "localhost",
            'database' => "widgetworld",
            'username' => "wuser",
            'password' => …
Run Code Online (Sandbox Code Playgroud)

php phpunit wamp

1
推荐指数
1
解决办法
3165
查看次数