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

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),





