当前位置:网站首页>Decorator (1)

Decorator (1)

2020-11-08 21:03:49 8Years

// The specific application of decorators ;
// for example , Amplify a voice 2 times   
//  Implementation process :1. Voice interface ,2. Human beings ,3. The microphone class 
// 4. Pass the human class into the microphone class , So that the sound can be amplified ( So there's a constructor in the loudspeaker ( It's used to introduce people into this object ))

public class testDerector {

    public static void main(String[] args) {
        Person p= new Person();

        p.say();

        Amplifile amplifile=new Amplifile(p);
        amplifile.say();

    }

    interface Say {
        void say();
    }


    public static class Person implements Say{


        private int voice=10;

        @Override
        public void say() {
            System.out.println(this.getVoice());

        }
        public int getVoice() {
            return voice;
        }
        public void setVoice(int voice) {
            this.voice = voice;
        }

    }

    static class Amplifile implements Say{

        private Person p;

        public Amplifile(Person p) {
            this.p = p;
        }
        @Override
        public void say() {
            System.out.println(" Loudspeakers "+p.getVoice()*2);
        }
    }
}

Output results             10    Loudspeakers 20

版权声明
本文为[8Years]所创,转载请带上原文链接,感谢