美文网首页
设计模式:静态代理

设计模式:静态代理

作者: Codes作业本 | 来源:发表于2020-09-28 21:26 被阅读0次

    静态代理

    第一步:首先实现钢笔的接口

    interface PenInterface {
        void write();
        void draw();
    }
    

    第二步:学生的手实现钢笔接口

    public class StudentHand implements PenInterface{
    
        @Override
        public void write() {
            System.out.println("write() 写字方法调用");
            
        }
    
        @Override
        public void draw() {
            System.out.println("draw() 画画方法调用");
        }
    
    }
    

    第三步:学生实现钢笔接口的功能

    public class Students implements PenInterface {
    
        PenInterface mStudentHand;
    
        public Students(PenInterface pen) {
            mStudentHand = pen;
        }
    
        @Override
        public void write() {
            mStudentHand.write();
        }
    
        @Override
        public void draw() {
            mStudentHand.draw();
        }
    
    }
    

    Main方法中调用,通过学生,去实现钢笔的功能

    public static void main(String[] args) {
        PenInterface pen = new StudentHand();
        Students student = new Students(pen);
        student.draw();
        student.write();
    }
    

    相关文章

      网友评论

          本文标题:设计模式:静态代理

          本文链接:https://www.haomeiwen.com/subject/cvmjuktx.html