Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
抽象类数组与面向对象设计-Java_Java_Oop_Hashmap_Abstract - Fatal编程技术网

抽象类数组与面向对象设计-Java

抽象类数组与面向对象设计-Java,java,oop,hashmap,abstract,Java,Oop,Hashmap,Abstract,我有一个类组件 abstract class Component{ private componentType m_type; public Component(componentType type) { m_type = type; } } 和2个子类 class AmplifierComponent extends Component{ public AmplifierComponent() { super(

我有一个类组件

abstract class Component{
    private componentType m_type;

    public Component(componentType type)
    {
        m_type = type;
    }
}
和2个子类

class AmplifierComponent extends Component{
    public AmplifierComponent()
    {
        super(componentType.Amp);
        System.out.print(this.m_type);
    }
}

class AttenuatorComponent extends Component{
    public AttenuatorComponent()
    {
        super(componentType.Att);
        System.out.print(this.m_type);
    }
}
我的问题是: 1.我无法实例化任何类型的组件,因为m_类型不可见(这意味着什么?
2.我需要将用户插入到链中的所有组件制作一个数组。我无法创建组件类的数组

有人能帮我设计一下吗?
还是采取一些变通办法


提前谢谢

我不明白您为什么需要类型成员。这看起来是多余的。您只需执行以下操作:

abstract class Component{
}

class AttenuatorComponent extends Component{
    public AttenuatorComponent() {
       // calls the default super constructor
    }
}
并依赖于你的班级表现得体。当您声明了相应的类时,不需要使用类型成员来标识层次结构类型。如果您确实有一个成员变量要求在子类中可见,但客户端不能看到,那么您可以将其设置为受保护的,而不是私有的

组件
如果没有与之相关的功能/数据,则可能是一个接口

您的数组声明如下所示

Component[] components = new Component[20];
components[0] = new AttenuatorComponent();

多态性意味着您可以遍历这个组件数组,调用在
组件上声明(但不一定由
实现)的适当方法,并调用适当的子类方法。

我不明白您为什么需要类型成员。这看起来是多余的。您只需执行以下操作:

abstract class Component{
}

class AttenuatorComponent extends Component{
    public AttenuatorComponent() {
       // calls the default super constructor
    }
}
并依赖于你的班级表现得体。当您声明了相应的类时,不需要使用类型成员来标识层次结构类型。如果您确实有一个成员变量要求在子类中可见,但客户端不能看到,那么您可以将其设置为受保护的,而不是私有的

组件
如果没有与之相关的功能/数据,则可能是一个接口

您的数组声明如下所示

Component[] components = new Component[20];
components[0] = new AttenuatorComponent();

多态性意味着您可以遍历这个组件数组,调用
组件上声明(但不一定由
组件实现)的适当方法,并且将调用合适的子类方法。

将m_type设置为protected,以便能够从子类中看到它。

将m_type设置为protected,以便能够从子类中看到它

abstract class Component {
    private componentType m_type;

    public Component(componentType type)
    {
        m_type = type;
    }

    public componentType getType()
    {
        return this.m_type;
    }
}

class AmplifierComponent extends Component{
    public AmplifierComponent()
    {
        super(componentType.Amp);
        System.out.print(super.getType());
    }
}

class AttenuatorComponent extends Component{
    public AttenuatorComponent()
    {
        super(componentType.Att);
        System.out.print(super.getType());
    }
}
这样,您可以读取m_类型,但无法更改它。 您还可以保护getType()命令,因此只能通过继承它的类访问它

这样,您可以读取m_类型,但无法更改它。
您还可以使getType()命令受保护,因此只能通过继承它的类访问它。

使m_type受保护而不是私有,以便子类可以看到它。使m_type受保护而不是私有,以便子类可以看到它。+1,如果代码<组件/<代码>的子类不共享任何相同的实现函数,那么考虑使用接口而不是抽象类。为什么不需要类型?当我遍历数组时,如何确定它是什么类型的组件?只需对其调用一个方法即可。java的多态性将决定正确的实现方法,根据类+ 1的类型,如果<>代码>组件< /C>的子类不共享任何相同的实现函数,也考虑使用接口代替抽象类。为什么类型不需要?当我遍历数组时,如何确定它是什么类型的组件?只需对其调用一个方法即可。Java的多态性将根据类的类型为您确定正确的实现方法