上海格宇设计培训学校

试听课 + 活动课
填写信息优先获取试听课

位置:学校首页 > 学校动态>上海哪有好的Java培训机构地址

上海哪有好的Java培训机构地址

上海格宇老师认为,真正有效率的,是线下脱产学习,但如果确实不方便线下学习,上海格宇老师也准备了Java基础培训线上视频资料,这次我们选取其中关于Java泛型的一些知识,希望能够对大家有所帮助。


泛型

Java的泛型是编译器提供的语法糖,称之为:类型参数搽除,先看一下语法,然后总结一点规律:

泛型方法

测试代码

1 static void puts(T msg) {

2 println(msg);

3 }

4

5 static void println(Object msg) {

6 System.out.println("Object:" + msg);

7 }

8

9 static void println(String msg) {

10 System.out.println("String:" + msg);

11 }

调用泛型方法

1 System.out.println("generic method test");

2 puts("hello");

3 Program. puts("hello");

输出的结果是

1 generic method test

2 Object:hello

3 Object:hello

泛型类

测试代码

1 class TestGenericClass {

2 T value;

3

4 void setValue(T value) {

5 this.value = value;

6 }

7 }

调用代码

1 System.out.println("generic class test");

2 System.out.println(t.value);

输出结果

1 generic class test

2 1

泛型接口

测试代码

1 interface TestInterface {

2 void test(T item);

3 }

4

5 class TestInterfaceImp1 implements TestInterface {

6

7 @Override

8 public void test(String item) {

9 System.out.println(item);

10 }

11 }

12

13 class TestInterfaceImp2 implements TestInterface {

14

15 @Override

16 public void test(T item) {

17 System.out.println(item);

18 }

19 }

调用代码

1 System.out.println("generic interface test");

2 TestInterface testInterface1 = new TestInterfaceImp1();

3 testInterface1.test("hi");

4 for (Method item : testInterface1.getClass().getMethods()) {

5 if (item.getName() == "test") {

6 System.out.println(item.getParameterTypes()[0].getName());

7 }

8 }

9

10 TestInterface testInterface2 = new TestInterfaceImp2<>();

11 testInterface2.test("hi");

12 for (Method item : testInterface2.getClass().getMethods()) {

13 if (item.getName() == "test") {

14 System.out.println(item.getParameterTypes()[0].getName());

15 }

16 }

输出结果

1 generic interface test

2 hi

3 java.lang.String

4 java.lang.Object

5 hi

6 java.lang.Object

类型参数约束

测试代码

1 class Animal {

2 }

3

4 class Dog extends Animal {

5 }

6

7 class Base {

8 public void test(T item) {

9 System.out.println("Base:" + item);

10 }

11 }

12

13 class Child extends Base {

14

15 @Override

16 public void test(Dog item) {

17 System.out.println("Child:" + item);

18 }

19 }

调用代码

1 System.out.println("bounded type parameters test");

2 Base base = new Child();

3 base.test(new Dog());

4 for (Method item : base.getClass().getMethods()) {

5 if (item.getName() == "test") {

6 System.out.println(item.getParameterTypes()[0].getName());

7 }

8 }

输出结果

1 bounded type parameters test

2 Child:Dog@533c2ac3

3 Dog

4 Animal

类型搽除过程

将泛型定义中的类型参数去掉。

class Base {

public void test(T item) {

System.out.println("Base:" + item);

}

}

将T换成extends指定的约束类型,默认是Object。

1 class Base {

2 public void test(Animal item) {

3 System.out.println("Base:" + item);

4 }

5 }

如果有非泛型类型继承或实现了泛型基类或接口,而且进行了重写,根据情况,编译器会自动生成一些方法。

1 class Child extends Base {

2 @Override

3 public void test(Animal item) {

4 this.test((Dog)item);

5 }

6

7 public void test(Dog item) {

8 System.out.println("Child:" + item);

9 }

10 }

根据泛型参数的实际参数搽除调用代码。

1 System.out.println("bounded type parameters test");

2 Base base = new Child();

3 base.test(new Dog());

4 for (Method item : base.getClass().getMethods()) {

5 if (item.getName() == "test") {

6 System.out.println(item.getParameterTypes()[0].getName());

领取试听课
温馨提示:为不影响您的学业,来校区前请先电话或QQ咨询,方便我校安排相关的专业老师为您解答
版权所有:搜学搜课(www.soxsok.com) 技术支持:搜学搜课网