Passing different type than parameter type

前端 未结 4 378
庸人自扰
庸人自扰 2021-01-07 08:50

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)

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 09:29

    This could work in two scenarios:

    • When A is a B, i.e. inheritance or interface implementation, or
    • When A and B are primitive data types, and an implicit conversion exists from A to `B.

    Here is an example:

    void m(long b) {
        ...
    }
    int a = 123;
    m(a); // This compiles and runs correctly
    

提交回复
热议问题