Multiple inheritance design issue in Java

前端 未结 7 1426
醉梦人生
醉梦人生 2020-12-09 13:03

How do you deal with having only single inheritance in java? Here is my specific problem:

I have three (simplified) classes:

public abstract class A         


        
7条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 13:40

    /** * First example */

    class FieldsOfClassA {
        public int field1;
        public char field2;
    }
    
    interface IClassA {
        public FieldsOfClassA getFieldsA();
    }
    
    class CClassA implements IClassA {
        private FieldsOfClassA fields;
    
        @Override
        public FieldsOfClassA getFieldsA() {
            return fields;
        }
    }
    
    /**
     * seems ok for now
     * but let's inherit this sht
     */
    
    
    class FieldsOfClassB {
    
        public int field3;
        public char field4;
    }
    
    interface IClassB extends IClassA {
    
        public FieldsOfClassA getFieldsA();
        public FieldsOfClassB getFieldsB();
    }
    
    class CClassB implements IClassB {
    
        private FieldsOfClassA fieldsA;
        private FieldsOfClassB fieldsB;
    
        @Override
        public FieldsOfClassA getFieldsA() {
            return fieldsA;
        }
    
        @Override
        public FieldsOfClassB getFieldsB() {
            return fieldsB;
        }
    }
    

    /**

    • wow this monster got bigger

    • imagine that you will need 4 lvl of inheritance

    • it would take so much time to write this hell

    • I'm even not talking that user of those iface will think

    • what fields i will need fieldsA fieldsB fieldsC or another one

    So composition does not work here and your pathetic tries are useless

    When u think about Oject Oriented programming

    u need BIG models with 6-7 lvls of multiple inheritance

    because that is good test and because corresponds to models of real life or math models tested by civilization for 4 thousands years.

    If your models require 2 lvl of inheritance stop pretending u using OO

    U can easily implement it with any language even procedural one like C or Basic language */

提交回复
热议问题