Raw use of parameterized class
时间: 2023-09-28 07:09:24 浏览: 529
The raw use of a parameterized class refers to using the class without specifying the type parameter. In languages like Java or C#, parameterized classes allow you to create classes that can work with different types. However, if you use the class without specifying the type parameter, it is considered a raw use.
For example, let's say we have a generic class called `Box` that can hold any type of object:
```java
public class Box<T> {
private T value;
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
}
```
If we use this class without specifying the type parameter, it becomes a raw use:
```java
Box box = new Box(); // Raw use of Box
box.setValue("Hello"); // No type safety
String value = (String) box.getValue(); // Type casting required
```
In the above example, `Box box = new Box();` is a raw use because we are not specifying the type parameter. This results in the loss of type safety, as we can assign any type of value to the `value` field without any compiler checks. We also need to perform explicit type casting when retrieving the value.
It is generally recommended to avoid raw uses of parameterized classes as they bypass the type safety provided by generics and can lead to runtime errors or unexpected behavior.
阅读全文