Generics - Introduction to Generics
Introduction to Generics
Let’s take an example to illustrate what generics are.
There are two classes as follows. It is necessary to construct objects of the two classes and print out their respective members x.
public class StringFoo {
private String x;
public String getX() {
return x;
}
public void setX(String x) {
this.
public class DoubleFoo {
private Double x;
public Double getX() {
return x;
}
public void setX(Double x) {
this.x = x;
}
}
If you want to implement the ong, Date, etc. It is really boring to write corresponding classes for type operations.
Therefore, refactor the above two classes into one class, consider the following:
In the above class, the logic of the members and methods are the same, but the types are different. Object is the parent class of all classes, so you can consider using Object as the member type, so that it can be universal.
public class ObjectFoo {
private Object x;
public Object getX() {
return x;
}
public void setX(Object x) {
this.
The calling code is as follows :
public class ObjectFooDemo {
public static void main(String args[]) {
ObjectFoo strFoo = new ObjectFoo();
strFoo.setX("Hello Generics!");
ObjectFoo douFoo = new ObjectFoo() ;
.setX(new Double("33"));
ObjectFoo objFoo = new ObjectFoo();
objFoo.setX(new Object());
String str = (String)strFoo.getX();
Double d = (Double)douFoo.getX();
Object obj = objFoo.getX();
System.out.println("strFoo.getX=" + str);
System.out.println("douFoo.getX=" + d);
System.out.println("strFoo.getX=" + obj);
}
}
The above is the code we wrote without generics, using the top-level base class Object for type declaration , and then pass the value in, and perform forced type conversion when taking it out.
JDK has introduced the concept of generics since 1.5 to elegantly solve such problems. Using generic technology, the code written is as follows:
public class GenericsFoo
private T x;
public T getX() {
return x;
}
public void setX(T x) {
this. x = x;
}
}
The calling code is as follows:
public class GenericsFooDemo {
public static void main(String args[]){
GenericsFoo
strFoo. setX("Hello Generics!");
GenericsFoo
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Do generic functions solve the problem of variadic parameter types in Golang?
Apr 16, 2024 pm 06:12 PM
Generic functions in Go solve the problem of variadic types: generic functions allow type parameters to be specified at runtime. This makes it possible to write functions that can handle parameters of different types. For example, the Max function is a generic function that accepts two comparable parameters and returns the larger value. By using generic functions, we can write more flexible and general code that can handle different types of parameters.
Specific application scenarios of generics in golang
May 04, 2024 am 11:45 AM
Application scenarios of generics in Go: Collection operations: Create collection operations suitable for any type, such as filtering. Data Structures: Write general-purpose data structures such as queues, stacks, and maps to store and manipulate various types of data. Algorithms: Write general-purpose algorithms such as sorting, search, and reduction that can handle different types of data.
What is the impact of Golang generics on function signatures and parameters?
Apr 17, 2024 am 08:39 AM
The impact of generics on Go function signatures and parameters includes: Type parameters: Function signatures can contain type parameters, specifying the types that the function can use. Type constraints: Type parameters can have constraints that specify conditions that they must satisfy. Parameter type inference: The compiler can infer the type of unspecified type parameters. Specifying types: Parameter types can be explicitly specified to call generic functions. This increases code reusability and flexibility, allowing you to write functions and types that can be used with multiple types.
Application of Java generics in Android development
Apr 12, 2024 pm 01:54 PM
The application of generics in Android development enhances code reusability, security and flexibility. The syntax consists of declaring a type variable T that can be used to manipulate type-parameterized data. Generics in action include custom data adapters, allowing the adapter to adapt to any type of custom data object. Android also provides generic list classes (such as ArrayList) and generic methods that allow the manipulation of parameters of different types. The benefits of using generics include code reusability, security, and flexibility, but care needs to be taken to specify the correct bounds and use them in moderation to ensure code readability.
How do Java enum types work with generics?
May 04, 2024 am 08:36 AM
The combination of enumeration types and generics in Java: When declaring an enumeration with generics, you need to add angle brackets, and T is the type parameter. When creating a generic class, you also need to add angle brackets, T is a type parameter that can store any type. This combination improves code flexibility, type safety, and simplifies code.
Advantages and Disadvantages of Java Generics
Apr 12, 2024 am 11:27 AM
Advantages and Disadvantages of Java Generics What are Java Generics? Java generics allow you to create typed collections and classes, which enables them to store objects of any type, not just specific types. This increases code flexibility, reusability, and reduces errors. Advantages Type safety: Generics enforce type safety at compile time, ensuring that there are only data of compatible types in the collection, thereby reducing runtime errors. Reusability: Generic classes and collections can be used with various data types without having to rewrite code. Flexibility: Generics allow the creation of code that can flexibly handle different types of data, improving scalability and maintainability. Concise code: Generics can make the code more concise and readable. API consistency: JavaCollection
What are the upper and lower bounds of Java function generics? how to use?
Apr 26, 2024 am 11:45 AM
Java function generics allow setting upper and lower bounds. Extends specifies that the data type accepted or returned by a function must be a subtype of the specified type, e.g. The lower bound (super) specifies that the data type accepted or returned by a function must be a supertype of the specified type, e.g. The use of generics improves code reusability and security.
What are the limitations of generic functions in Golang?
Apr 16, 2024 pm 05:12 PM
Limitations of Go generic functions: only type parameters are supported, value parameters are not supported. Function recursion is not supported. Type parameters cannot be specified explicitly, they are inferred by the compiler.


