如何获取对象父对象的实例

发布于 2024-09-27 11:26:09 字数 229 浏览 3 评论 0原文

Java 有没有办法从该对象获取该对象的父类的实例?

前任。

public class Foo extends Bar {

     public Bar getBar(){
          // code to return an instance of Bar whose members have the same state as Foo's
     }

}

Is there a way in Java to get an instance of my object's parent class from that object?

ex.

public class Foo extends Bar {

     public Bar getBar(){
          // code to return an instance of Bar whose members have the same state as Foo's
     }

}

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

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

发布评论

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

评论(6

冷月断魂刀 2024-10-04 11:26:09

没有内置的方法可以做到这一点。您当然可以编写一个方法,该方法将采用 Foo 并创建一个已使用相关属性初始化的 Bar。

  public Bar getBar() {
       Bar bar = new Bar();
       bar.setPropOne(this.getPropOne());
       bar.setPropTwo(this.getPropTwo());
       return bar;
  }

另一方面,继承意味着 Foo 是一个 Bar,所以你可以这样做

 public Bar getBar() {
      return this;
   }

There's no built-in way to do this. You can certainly write a method that will take a Foo and create a Bar that's been initialized with the relevant properties.

  public Bar getBar() {
       Bar bar = new Bar();
       bar.setPropOne(this.getPropOne());
       bar.setPropTwo(this.getPropTwo());
       return bar;
  }

On the other hand, what inheritance means is that a Foo is a Bar, so you could just do

 public Bar getBar() {
      return this;
   }
左秋 2024-10-04 11:26:09

长话短说:

return this;

如果您想返回一个副本,请在 Bar 上创建一个接收另一个 Bar 的复制构造函数。

public Bar(Bar that) {
    this.a = that.a;
    this.b = that.b;
    ...
}

Long story short:

return this;

If you want to return a copy, then create a copy constructor on Bar that receives another Bar.

public Bar(Bar that) {
    this.a = that.a;
    this.b = that.b;
    ...
}
萌辣 2024-10-04 11:26:09

this this 是 bar 的一个实例,简单的就是“return this;”但如果您需要一个不同的对象,也许您可​​以实现 java.lang.Clonable 和“return this.clone();”

this this is an instance of bar, the simple thing is just "return this;" but if you need a distinct object, perhaps you could implement java.lang.Clonable and "return this.clone();"

梦魇绽荼蘼 2024-10-04 11:26:09

如果您的类扩展了 Bar,那么它就是 Bar 本身的一个实例。所以

public Bar getBar() {
  return (Bar) this;
}

应该这样做。
如果你想要一个“不同的实例”,你可以尝试:

public Bar getBar() {
   return (Bar) this.clone();
}

If your class extends Bar, it is an instance of Bar itself. So

public Bar getBar() {
  return (Bar) this;
}

should do it.
If you want a "different instance", you can try:

public Bar getBar() {
   return (Bar) this.clone();
}
蓝天白云 2024-10-04 11:26:09

由于 Foo is-a Bar,你可以这样做:

return this;

这只会返回当前对象的父实例。

Since Foo is-a Bar, you can do this:

return this;

This will only return the parent instance of current object.

夏末染殇 2024-10-04 11:26:09

您可以使用反射

    package com;

    class Bar {

        public void whoAreYou(){
            System.out.println("I'm Bar");
        }

    }

    public class Foo extends Bar{

        public void whoAreYou() {
            System.out.println("I'm Foo");
        }

        public Bar getBar() {
            try {
                return (Bar)this.getClass().getSuperclass().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            throw new RuntimeException();
        }

        public static void main() {
            Foo foo = new Foo();
            foo.whoAreYou();
            foo.getBar().whoAreYou();
        }
    }

You can use reflection

    package com;

    class Bar {

        public void whoAreYou(){
            System.out.println("I'm Bar");
        }

    }

    public class Foo extends Bar{

        public void whoAreYou() {
            System.out.println("I'm Foo");
        }

        public Bar getBar() {
            try {
                return (Bar)this.getClass().getSuperclass().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            throw new RuntimeException();
        }

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