如何创建“延伸”的类两个现有的具体课程 [英] How to create a class that "extends" two existing concrete classes

查看:108
本文介绍了如何创建“延伸”的类两个现有的具体课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常清楚,它可以在接口的帮助下完成,我已经做了很多次。但这一次我的情况有很大的不同。我有 class A class B ,我需要创建另一个类C 它扩展了A和B,因为C应该具有这两个功能,并且还注意到A和B不相关,所以即使我不能说A可以扩展 class B



我很困惑我现在该怎么办我知道我们不能改变java ...但至少会有一些可能的方法。即使是最近也可以...请帮助我。



添加更多详细信息: - B类是一个标准API,而 class A 是一个常见的异常类,需要由所有异常类继承。



相关问题:




解决方案

使用对象组合



这是还倡导有效的Java,项目16:喜欢组合继承



Java限制一个类从havin g 是一个相关到两个(不相关)的类。它不是,而是限制一个类不具有两个不相关类的 功能



< hr>

  class A {
void doAStuff(){
System.out.println(A-method) ;
}
}

class B {
void doBStuff(){
System.out.println(B-method);
}
}

//包裹A和B对象。
class C {
A aObj;
B bbb;

void doAStuff(){
aObj.doAStuff();
}

void doBStuff(){
bObj.doBStuff();
}
}

(或者你可以有 C类扩展A ,并且只为 B 创建包装方法,但请记住,应该说,C是一个A







我有一个设计模式需要遵守,并且必须延续


这是你可能知道的完全不可能然而,您可以为 A B 创建代理类,将功能委托给 C -instance。例如,将这两个方法添加到上面的 C -class中:

  class C {

// ...

public A asAObject(){
return new A(){
@Override
void doAStuff(){
C.this.doAStuff();
}
};
}

public B asBObject(){
return new B(){
@Override
void doBStuff(){
C. this.doBStuff();
}
};
}
}


I very well know that it can be done with the help of interfaces and i have done it many times. But this time my situation is quite difference. I have class A , class B and i need to create another class C which extends both A and B because C should have boths functionality and also note that A and B are not inter related so even i cant say A may extend class B.

I am quite confused what should i do right now. I know we cant change java... but at least there would be some way possible. Even the nearest may also do... please help me out.

Adding more details:- Class B is a standard API while class A is a common exception class that need to be inherited by all exception classes.

Related question:

解决方案

This is typically solved using object composition.

This is also advocated in Effective Java, Item 16: Favor composition over inheritance.

Java restricts a class from having an is a-relation to two (unrelated) classes. It does not however restrict a class from having the functionality of two unrelated classes.


class A {
    void doAStuff() {
        System.out.println("A-method");
    }
}

class B {
    void doBStuff() {
        System.out.println("B-method");
    }
}

// Wraps an A and a B object.
class C {
    A aObj;
    B bObj;

    void doAStuff() {
        aObj.doAStuff();
    }

    void doBStuff() {
        bObj.doBStuff();
    }
}

(Alternatively you could have class C extends A and only create wrapper methods for B, but keep in mind that it should make sense to say C is an A.)


I have a design pattern that need to be followed and for that its compulsory to extend

This is, as you probably know completely impossible. You could however create proxy classes for A and B that delegate the functionality to the C-instance. For instance by adding these two methods to the C-class above:

class C {

    // ...

    public A asAObject() {
        return new A() {
            @Override
            void doAStuff() {
                C.this.doAStuff();
            }
        };
    }

    public B asBObject() {
        return new B() {
            @Override
            void doBStuff() {
                C.this.doBStuff();
            }
        };
    }
}

这篇关于如何创建“延伸”的类两个现有的具体课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆