如何更改 Java 中派生类的成员函数的签名

发布于 2024-07-08 15:08:51 字数 658 浏览 11 评论 0原文

首先,我正在扩展现有的类结构,并且无法更改原始类结构,但需要注意的是:

我想这样做:

class a
{
   int val;

   ... // usual constructor, etc...

   public int displayAlteredValue(int inp)
   {
     return (val*inp);
   }
}

class b extends a
{
   ... // usual constructor, etc...

   public in displayAlteredValue(int inp1, int inp2)
   {
     return (val*inp1*inp2);
   }
}

正如我之前所说,我无法更改 class a 并且我想保留函数名称displayAlteredValue 而不是创建一个新函数。 如果可以做到这一点,我只需要将 a 的一些实例化更改为 b 的实例化。 我不想花费大量时间来替换对 displayAlteredValue 的许多函数调用。 (是的,我确实意识到存在诸如搜索和替换之类的事情,但是由于其他原因,这样做会有问题)。

有任何想法吗?

Firstly I'm extending an existing class structure and cannot alter the original, with that caveat:

I would like to do this:

class a
{
   int val;

   ... // usual constructor, etc...

   public int displayAlteredValue(int inp)
   {
     return (val*inp);
   }
}

class b extends a
{
   ... // usual constructor, etc...

   public in displayAlteredValue(int inp1, int inp2)
   {
     return (val*inp1*inp2);
   }
}

As I said before I cannot alter class a and I want to maintain the function name displayAlteredValue rather than making a new function.
If this can be done I only have to change a few instantiations of a to instantiations of b. I don't want to spend a lot of time replacing the many function calls to displayAlteredValue. (And yes I do realise there are such things as search and replace however for other reasons, doing that would be problematic).

Any ideas?

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

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

发布评论

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

评论(7

故事↓在人 2024-07-15 15:08:51

您可以重载派生类中的函数。 所以你上面指定的应该有效。 刚刚写了一个快速测试,它成功了。

public class DerivedOverload {

    /**
     * @param args
     */
    public static void main(String[] args) {
        A classA = new A(); 

        B classB = new B();

        System.out.println("DerivedOverload.main() classA.displayAlteredValue(2) : " + classA.displayAlteredValue(2));

        System.out.println("DerivedOverload.main() classA.displayAlteredValue(2) : " + classB.displayAlteredValue(2,2));
    }



}


class A
{
   int val = 2;

   A(){

   }

   public int displayAlteredValue(int inp)
   {
     return (val*inp);
   }
}

class B extends A
{
   B(){

   }

   public int displayAlteredValue(int inp1, int inp2)
   {
     return (val*inp1*inp2);
   }
}

系统出。
DerivedOverload.main() classA.displayAlteredValue(2) : 4
DerivedOverload.main() classA.displayAlteredValue(2) : 8

You can overload a function in a derived class. So what you have specified above should work. Just wrote a quick test and it worked.

public class DerivedOverload {

    /**
     * @param args
     */
    public static void main(String[] args) {
        A classA = new A(); 

        B classB = new B();

        System.out.println("DerivedOverload.main() classA.displayAlteredValue(2) : " + classA.displayAlteredValue(2));

        System.out.println("DerivedOverload.main() classA.displayAlteredValue(2) : " + classB.displayAlteredValue(2,2));
    }



}


class A
{
   int val = 2;

   A(){

   }

   public int displayAlteredValue(int inp)
   {
     return (val*inp);
   }
}

class B extends A
{
   B(){

   }

   public int displayAlteredValue(int inp1, int inp2)
   {
     return (val*inp1*inp2);
   }
}

System out.
DerivedOverload.main() classA.displayAlteredValue(2) : 4
DerivedOverload.main() classA.displayAlteredValue(2) : 8

音盲 2024-07-15 15:08:51

我不清楚问题是什么。 您绝对可以创建一个子类 b ,它具有相同名称但不同签名的方法。

您可能遇到的一个可能的问题是,编译器需要知道对象的类型是 b,而不是 a,才能调用仅存在于 b 中的方法。

因此,这样的代码将不起作用:

// this works because b is a subclass of a
a anObject = new b();

// this will not compile because the declared type of anObject is a
int x = anObject.getValue( 1, 2 );

您必须将变量的声明更改为类型 b,或者每当您想要调用 2 参数方法时将其强制转换为 b。

I'm not clear what the issue is. You absolutely can create a subclass b which has a method of the same name but different signature.

One possible issue you might be running into is that the compiler needs to know that the type of the object is b, not a, in order to call the method that only exists in b.

So code like this won't work:

// this works because b is a subclass of a
a anObject = new b();

// this will not compile because the declared type of anObject is a
int x = anObject.getValue( 1, 2 );

You would have to either change the declaration of the variable to type b, or cast it to b whenever you want to call the 2-argument method.

自在安然 2024-07-15 15:08:51

如果将其添加到派生类中会怎样

public int displayAlteredValue(int inp)
{
  return super.displayAlteredValue(inp);
}

what if you add this to your derived class

public int displayAlteredValue(int inp)
{
  return super.displayAlteredValue(inp);
}
兮子 2024-07-15 15:08:51

您编写的代码按原样编译。 您基本上只是重载了该方法。 您发布的代码没有完成哪些您想要完成的任务? 您是否试图阻止人们使用单个参数调用 b.displayAlteredValue ? 如果是这样,你不能(在编译时)这样做,因为它会违反里氏可替代性原则。

您可以抛出异常,但这并不是很好。

或者,您可以使用组合而不是继承 - 但实际上我们需要更多地了解情况才能知道这是否合适。

The code you've written compiles as-is. You've basically just overloaded the method. What does the code you posted not accomplish, that you want to accomplish? Are you trying to stop people from calling b.displayAlteredValue with a single argument? If so, you can't do that (at compile time) as it would violate Liskov's Substitutability Principle.

You could throw an exception, but that wouldn't be terribly nice.

Alternatively, you could use composition instead of inheritance - but really we'd need to know more about the situation to know whether that was suitable.

橪书 2024-07-15 15:08:51

你所拥有的对我来说看起来不错。

类 b 的实例将有权访问该方法的两个版本。 对 1 个参数版本的调用将自动路由到 a 类。 对 2-arg 版本的调用将转到类 b。

虽然 a 类的实例只能访问 1-arg 版本,但我认为没有办法解决这个问题。

What you have looks fine to me.

Instances of class b will have access to both versions of the method. Calls to the 1-argument version will be automatically routed to class a. Calls to the 2-arg version will go to class b.

Instances of class a will only have access to the 1-arg version though, but I don't think there's a way around that.

情释 2024-07-15 15:08:51

或者,您可以考虑使用组合而不是继承,因为您要扩展的类来自不同的包。

http://www.artima.com/lejava/articles/designprinciples4.html

Alternatively, you could consider favoring composition over inheritance since the class you are extending comes from a different package.

http://www.artima.com/lejava/articles/designprinciples4.html

萌化 2024-07-15 15:08:51

您可以在类 B 中编写如下代码:

public int displayAlteredValue(int inp) 
{
    return -1;
}

并在函数上指示 -1 是错误返回。 对于 int 来说,这实际上不起作用,但如果您返回一个类,则如果它们调用该方法的单参数版本,您可能会返回 null。

You could write the code in class B as follows:

public int displayAlteredValue(int inp) 
{
    return -1;
}

and indicate on the function that -1 is an error return. For int this doesn't really work, but if you returned a class, you could return null if they call the single parameter version of the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文