@File
2019-10-08T09:34:56.000000Z
字数 450
阅读 79
java
理论
所有小类型向大类型转换,无需任何操作,都能自动转换。
// 从 byte 类型赋值给 long 类型的变量中
long num = (byte)5;
// 创建一个子类实例,赋值给有关系的父类类型的变量中
Parent parent = new Children();
// 把 short 类型的变量引用到 int 类型的变量中
short num1 = 5;
int num2 = num1;
// 把子类的对象实例变量引用到父类类型变量中
Children children = new Children();
Parent parent = children;
// 方法1
public static void method1(long num){};
// 方法2
public static void method2(Parent obj){};
// 给方法1传入一个 byte 类型的值
method1((byte)5);
// 给方法2传入一个子类型的对象实例
Children children = new Children();
method2(children);