• java 代理 agency


    java并没有对其提供直接的支持,这是继承和组合的中庸之道,因为我们将一个成员对象置于所要构造的类中(组合),但与此同时我们在新类中暴露了该成员的所有方法(就像继承),使用代理时可以拥有更多的控制力,因为我们可以选择提供在成员对象中的方法的某个子集;

    package object;
    
     class SpaceShipControls{
        void up(int velocity){}
        void down(int velocity){}
        void left(int velocity){}
        void right(int velocity){}
        void forward(int velocity){}
        void back(int velocity){}
        void turboBoost(){}
    }
     /*//继承 extends
    public class SpaceShip extends SpaceShipControls{
        private String name;
        public SpaceShip(String name) {this.name = name    ;}
        public String toString(){return name;}
        public static void main(String[] args)
        {
            SpaceShip protector = new SpaceShip("NSEA Protector");
            protector.forward(100);
        }
    }*/
    //代理 delegation
    public class SpaceShipDelegation{
        private String name;
        private SpaceShipControls controls = new SpaceShipControls();
        public SpaceShipDelegation(String name)
        {
            this.name = name;
        }
        //Delegated methods
        public void back(int velocity){ //使用代理时可以拥有更多的控制力,因为我们可以选择提供在成员对象中的方法的某个子集;
            controls.back(velocity);    //我们可以只提供back这一个方法
        }
        public void down(int velocity)
        {
            controls.down(velocity);
        }
        public void forward(int velocity)
        {
            controls.forward(velocity);
        }
        public void left(int velocity)
        {
            controls.left(velocity);
        }
        public void right(int velocity)
        {
            controls.right(velocity);
        }
        public void turboBoost()
        {
            controls.turboBoost();
        }
        public void up (int  velocity)
        {
            controls.up(velocity);
        }
        public static void main(String[] args)
        {
            SpaceShipDelegation protector =
                    new SpaceShipDelegation("NSEA Protector");
            protector.forward(100);
        }
    }///:~
      
  • 相关阅读:
    js中对new Date() 中转换字符串方法toLocaleString的使用
    安装sass时遇到Failed to build gem native extension
    访问mapper方法提示invalid bound statement (not found)原因总结
    A query was run and no Result Maps were found for the Mapped Statement
    VS常用快捷键
    查看python和NumPy版本和安装路径
    Mybatis报错: There is no getter for property named xxx
    Map集合中get不存在的key值
    MySQL中DATA类型数据和DATATIME类型数据的比较
    shell 数组操作
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10176522.html
Copyright © 2020-2023  润新知