Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 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 Answer

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();
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...