【问题标题】:How do i use abstraction to modify this class so it shows both colors of keys?我如何使用抽象来修改这个类,以便它显示两种颜色的键?
【发布时间】:2015-09-21 13:52:02
【问题描述】:

所以目前,我们的 Key 类只能产生白键。这是因为我已经硬编码了关键图像的文件名(“white-key.png”和“white-key-down.png”)。如何使用抽象来修改 Key 类,使其可以显示白键或黑键?

import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Key extends Actor
{
    private boolean isDown;
    private String key;
    private String sound;
    /**
     * Create a new key.
     */
    public Key(String keyName, String soundFile)
    {  
       key = keyName;
       sound = soundFile;
    }

    /**
     * Do the action for this key.
     */
    public void act()
    {
        if ( !isDown && Greenfoot.isKeyDown(key)) 
        {
            play();
            setImage("white-key-down.png");
            isDown = true;
        }
        if ( isDown && !Greenfoot.isKeyDown(key))
        {
            setImage("white-key.png");
            isDown = false;
        }
    }

    /**
     * Play the note of this key.
     */
    public void play()
    {
        Greenfoot.playSound(sound);
    }
}

【问题讨论】:

    标签: abstraction greenfoot


    【解决方案1】:

    我从您的问题中了解到,您希望 不同 类具有不同的图像,而不是在同一类中更改图像的选项。

    有几种方法可以做到这一点;这里有一个简单的,只是为了给你一个想法:

    import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
    
    abstract public class Key extends Actor
    {
        private boolean isDown;
        private String key;
        private String sound;
    
        abstract String getImageFileName();
        abstract String getDownImageFileName();
    
        /**
         * Create a new key.
         */
        public Key(String keyName, String soundFile)
        {  
           key = keyName;
           sound = soundFile;
        }
    
        /**
         * Do the action for this key.
         */
        public void act()
        {
            if ( !isDown && Greenfoot.isKeyDown(key)) 
            {
                play();
    
                String image = getDownImageFileName();
                setImage(image);
    
                isDown = true;
            }
            if ( isDown && !Greenfoot.isKeyDown(key))
            {
                String image = getImageFileName();
                setImage(image);
                isDown = false;
            }
        }
    
        /**
         * Play the note of this key.
         */
        public void play()
        {
            Greenfoot.playSound(sound);
        }
    }
    

    然后你可以添加新的类,每个类都有自己的图像:

    public class WhiteKey extends Key
    {
        @Override
        String getImageFileName()
        {
            return "white-key.png";
        }
    
        @Override
        String getDownImageFileName()
        {
            return "white-key-down.png";
        }
    }
    
    public class BlackKey extends Key
    {
        @Override
        String getImageFileName()
        {
            return "black-key.png";
        }
    
        @Override
        String getDownImageFileName()
        {
            return "black-key-down.png";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 1970-01-01
      • 2012-10-17
      • 2011-05-30
      • 2014-09-16
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多