传递与参数类型不同的类型 [英] Passing different type than parameter type

查看:133
本文介绍了传递与参数类型不同的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个变量 a A a 声明,方法 m void m(B b)。有什么方法可以调用 m(a)吗?或者这可能永远不会起作用,因为 a 类型为 A b 类型 B

If I would have a variable a declared by A a and a method m with void m(B b). Is there any way that calling m(a) would work? Or can this never work because a is of type A and b of type B.

推荐答案

如果 A 扩展 B ,它可以作为参数传递给该函数。如果没有并且 A 包含适用于 B 的值,则必须创建 B <的实例/ code>并使用 A 实例的必需值填充它。

If A extends B, it can be passed as argument to that function. If not and A contains values suitable for B you have to create an instance of B and fill it with the required values of the instance of A.

这篇关于传递与参数类型不同的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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