重写子类中的成员数据,在超类实现中使用?

发布于 2024-08-05 19:57:05 字数 893 浏览 1 评论 0原文

在Java中,是否可以重写子类中的成员数据,并使该重写版本成为超类实现中使用的数据?

换句话说,这就是我想要发生的事情,但它没有发生:

abstract public class BasicStuff {
        protected String[] stuff = { "Pizza", "Shoes" };

        public void readStuff() {
                for(String p : stuff) { 
                        system.out.println(p); 
                }
        }
}

..

public class HardStuff extends BasicStuff {
        protected String[] stuff = { "Harmonica", "Saxophone", "Particle Accelerator" };
}

此调用:

HardStuff sf = new HardStuff();
sf.readStuff();

... 打印 PizzaShoes。我希望它打印后者。

我认识到这是相当糟糕的分层面向对象实践;我需要它来处理非常特殊的情况,因为我正在使用 XML 配置和反射做一些事情。

有没有修改器可以实现这种情况?

是的,我确实认识到可以使用包装器来解决我的子类中的这个问题,即通过指示 stuff[] 的内容现在存储在具有不同名称的数组中,例如。我只是想保持简单,并且原则上很好奇。

预先非常感谢!

In Java, is it possible to override member data in a subclass and have that overridden version be the data used in a super class's implementation?

In other words, here's what I am trying to get to happen, and it's not happening:

abstract public class BasicStuff {
        protected String[] stuff = { "Pizza", "Shoes" };

        public void readStuff() {
                for(String p : stuff) { 
                        system.out.println(p); 
                }
        }
}

..

public class HardStuff extends BasicStuff {
        protected String[] stuff = { "Harmonica", "Saxophone", "Particle Accelerator" };
}

This invocation:

HardStuff sf = new HardStuff();
sf.readStuff();

... prints Pizza and Shoes. I want it to print the latter instead.

I recognise that this is rather poor hierarchical OO practice; I needed it for a very specific case as I am doing something with XML configuration and reflection.

Is there a modifier that can make this happen?

And yes, I do recognise that there are wrappers one can use to get around this problem in my subclass, i.e. by indicating that the contents of stuff[] are now stored in an array with a different name, for instance. I'm just trying to keep this simple, and am curious in principle.

Thanks a lot in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江心雾 2024-08-12 19:57:05

我相信您必须插入一个访问器方法,即

    for(String p : getStuff()) { 

在超类中使用:,并

    protected String[] getStuff() { return stuff; }

在有 protected String[] stuff 重新定义的地方添加:。

重写实际上适用于方法,而不是数据(至少在 Java 模型中是这样;其他一些语言的做法有所不同),因此要获得重写效果,您必须插入一个方法(通常是一个非常简单的访问器,就像这里)。它根本没有让事情变得复杂,它只是一种非常简单的方法来指示 Java 编译器本质上使用您所需的行为所需的“额外的间接级别”。

I believe you must interpose an accessor method, i.e., use:

    for(String p : getStuff()) { 

in the superclass, and add:

    protected String[] getStuff() { return stuff; }

wherever you have a protected String[] stuff redefinition.

Overriding really applies to methods, not data (at least, that is so in the Java model; some other languages do things differently), and so to get the override effect you must interpose a method (typically a dirt-simple accessor, like here). It doesn't really complicate things at all, it's just a very simple way to instruct the Java compiler to use, intrinsically, the "extra level of indirection" that your desired behavior requires.

§普罗旺斯的薰衣草 2024-08-12 19:57:05

这样你就可以用定义的东西隐藏父变量的东西。

尝试为初始化块(或构造函数)中的内容赋予值:

abstract public class BasicStuff {
  protected String[] stuff;

  {
    stuff = new String[] { "Pizza", "Shoes" };
  }

  public void readStuff() {
    for(String p : stuff) { 
      System.out.println(p); 
    }
  }
}

..

public class HardStuff extends BasicStuff {
  {
    stuff = new String[] { "Harmonica", "Saxophone", "Particle Accelerator" };
  }
}

This way you are hiding the parent variable stuff with the defined stuff.

Try giving value to stuff in the initialization block (or in the constructor):

abstract public class BasicStuff {
  protected String[] stuff;

  {
    stuff = new String[] { "Pizza", "Shoes" };
  }

  public void readStuff() {
    for(String p : stuff) { 
      System.out.println(p); 
    }
  }
}

..

public class HardStuff extends BasicStuff {
  {
    stuff = new String[] { "Harmonica", "Saxophone", "Particle Accelerator" };
  }
}
千里故人稀 2024-08-12 19:57:05

如果要打印派生类中定义的 String Stuff 数组,则需要通过重新定义 HardStuff 类中的方法来重写 HardStuff 类中的 readStuff 方法。由于 readStuff 方法是在抽象类 BasicStuff 中定义的,因此它只会打印 BasicStuff 类的成员。因此,在派生类中也添加相同的方法。
您可以在下面找到完整的代码..

class BasicStuff {
    protected String[] stuff = { "Pizza", "Shoes" };

    public void readStuff() {
            for(String p : stuff) { 
                    System.out.println(p); 
            }
    }
}


public class HardStuff extends BasicStuff {
    protected String[] stuff = 
              { "Harmonica", 
                "Saxophone", 
                "Particle Accelerator" 
              };

    public void readStuff() {
            for(String p : stuff) { 
                    System.out.println(p); 
            }
    }
    public static void main(String []arg)
    {
     HardStuff sf = new HardStuff();
        sf.readStuff();
    }
}

If you want to print the array of the String Stuff defined in derived class then you need to override the method readStuff in the class HardStuff by redefining the method in the class HardStuff. As the method readStuff was defined in the abstract class BasicStuff, hence it would only print the members of the class BasicStuff. Hence, add the same method in the derieved class too.
Below you can find the complete code..

class BasicStuff {
    protected String[] stuff = { "Pizza", "Shoes" };

    public void readStuff() {
            for(String p : stuff) { 
                    System.out.println(p); 
            }
    }
}


public class HardStuff extends BasicStuff {
    protected String[] stuff = 
              { "Harmonica", 
                "Saxophone", 
                "Particle Accelerator" 
              };

    public void readStuff() {
            for(String p : stuff) { 
                    System.out.println(p); 
            }
    }
    public static void main(String []arg)
    {
     HardStuff sf = new HardStuff();
        sf.readStuff();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文