Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

java继承的问题

假设有2个接口

IA(第三方定义的,不能修改)
IB(自己写的)

对应的有2个实现
A(第三方定义的,不能修改)
B(自己写的)

现在我想写一个类C,拥有A和B的并集方法,怎么写简单


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

生成两个对象,代理过去就好,参考如下代码:

package com.bixuebihui;
import org.junit.Test;
public class ProxyTest {
    
    interface IA{
        void methodA();
    }
    
    public static class A implements IA{
        @Override
        public void methodA() {
            System.out.println("A");
        }
    }
    interface IB{
        void methodB();
    }
    
    public static class B implements IB{
        @Override
        public void methodB() {
            System.out.println("B");
        }
    }
    
    interface IAB extends IA,IB{}
    
    @Test
    public void testProxy() {
    
        IAB ab = new IAB() {
            A a = new A();
            B b = new B();
            
            @Override
            public void methodA() {
                a.methodA();
            }
            
            @Override
            public void methodB() {
                b.methodB();
            }
        };
        ab.methodA();
        ab.methodB();
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...