天真的继承问题 - Java的 [英] Naive Inheritance Problem - Java

查看:151
本文介绍了天真的继承问题 - Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人儿真好,

我感觉问这么幼稚的问题为难。但我不明白一件事,
我有继承结构是这样,

B扩展A,code我已经写的是如下,

A级

 公共类A {
    私人INT POS = 0;
    大众A(){
        this.pos = 12;
    }
    公众诠释GETPOS(){
        返回this.pos;
    }
}

B类

 公共类B扩展A {
    INT规范= 15;
    大众B(){
        超();
    }
    公众诠释getSpec(){
        返回this.spec;
    }
}

和我多了一个类来测试,这将让我们去我的问题。

类检验

 进口的java.util。*;
公共类的测试{
    公共静态无效的主要(字串[] args){
        B中的新= B();
        ArrayList的< A> C =新的ArrayList< A>();
        c.add(一);
        的System.out.println(c.get(0).getPos());
        的System.out.println(c.get(0).getSpec());
    }
}

问:现在,我创建B的实例,这意味着我可以访问我的父类的方法 GETPOS()和B的自己的方法 getSpec()。但是,如果我创建A型的ArrayList(... B是A型也一样,它扩展了A ...),并添加我的B的实例,它的损失,这是访问它自己的方法的能力。我究竟做错了什么?请问ArrayList的实现是铸造我的B到A的内部?



  

请注意:我的产业的基本理解是父母无法访问
  除非他们孩子的方法得到保护。但是孩子可以访问他们的
  父类的方法。



解决方案

在除了<一提供的答案href=\"http://stackoverflow.com/questions/7369236/naive-inheritance-problem-java/7369252#7369252\">@Matt球和<一个href=\"http://stackoverflow.com/questions/7369236/naive-inheritance-problem-java/7369251#7369251\">@Hovercraft全鳗鱼,你可不必 显式转换,宣布通过实施方法的子类的为 摘要超类方法的。

 公共抽象类A {
    。
    。
    公共抽象INT getSpec();
}


编辑 -

如由 @Kublai汗的所提到的,有必要再进行的 摘要类。

G'day people,

I am feeling embarrass by asking such a naive question. But I can't understand one thing, I have Inheritance structure like this,

B extends A, code I have wrote is as below,

Class A

public class A{
    private int pos = 0;
    public A(){
        this.pos = 12;
    }
    public int getPos(){
        return this.pos;
    }
}

Class B

public class B extends A{
    int spec = 15;
    public B(){
        super();
    }
    public int getSpec(){
        return this.spec;
    }
}

And I have one more class to test, Which will get us to my question.

Class Test

import java.util.*;
public class Test{
    public static void main(String[] args){
        B a = new B();
        ArrayList<A> c = new ArrayList<A>();
        c.add(a);
        System.out.println(c.get(0).getPos());
        System.out.println(c.get(0).getSpec());
    }
}

Question : Now I am creating an instance of B, Which means I can access to my parent class's method getPos() and B's own method getSpec(). But if I create ArrayList with type A(...B is type A too, as it extends A...) and add my B's instance it losses it's ability to access it's own method. What am I doing wrong? Does ArrayList implementation is casting my B to A internally?


Note : My basic understanding of inheritance is parent cannot access child's method except they are protected. But Child can access their parent class's method.

解决方案

In addition to the answers provided by @Matt Ball and @Hovercraft Full Of Eels, you can avoid having to explicitly cast by declaring methods implemented by the subclass as abstract methods in the superclass.

public abstract class A{
    .
    .
    public abstract int getSpec();
}


EDIT-

As mentioned by @Kublai Khan, it is necessary to then make the superclass an abstract class.

这篇关于天真的继承问题 - Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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