Java中的泛型构造函数和接口
泛型使类、接口和方法考虑所有作为参数动态给出的(引用)类型。这确保了类型安全。通用类参数在实例变量的类名后的尖括号“<>”中指定。
泛型构造函数与泛型方法相同。对于 public 关键字之后和类名之前的泛型构造函数,必须放置类型参数。定义泛型构造函数后,可以使用任何类型的参数调用构造函数。构造函数是初始化新创建对象的代码块。它是一个没有返回类型的实例方法。构造函数的名称与类名称相同。构造函数可以是泛型的,尽管它的类不是泛型的。
执行:
例子
Java
// Java Program to illustrate Generic constructors
// Importing input output classes
import java.io.*;
// Class 1
// Generic class
class GenericConstructor {
// Member variable of this class
private double v;
// Constructor of this class where
// T is typename and t is object
GenericConstructor(T t)
{
// Converting input number type to double
// using the doubleValue() method
v = t.doubleValue();
}
// Method of this class
void show()
{
// Print statement whenever method is called
System.out.println("v: " + v);
}
}
// Class 2 - Implementation class
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Display message
System.out.println("Number to Double Conversion:");
// Creating objects of type GenericConstructor i.e
// og above class and providing custom inputs to
// constructor as parameters
GenericConstructor obj1
= new GenericConstructor(10);
GenericConstructor obj2
= new GenericConstructor(136.8F);
// Calling method - show() on the objects
// using the dot operator
obj1.show();
obj2.show();
}
}
Java
// Java Program to illustrate Generic interfaces
// Importing java input output classes
import java.io.*;
// An interface that extends Comparable
interface MinMax > {
// Declaring abstract methods
// Method with no body is abstract method
T min();
T max();
}
// Class 1 - Sub-class
// class extending Comparable and implementing interface
class MyClass >
implements MinMax {
// Member variable of 'MyClass' class
T[] values;
// Constructor of 'MyClass' class
MyClass(T[] obj) { values = obj; }
// Now, defining min() and max() methods
// for MimMax interface computation
// Defining abstract min() method
public T min()
{
// 'T' is typename and 'o1' is object_name
T o1 = values[0];
// Iterating via for loop over elements using
// length() method to get access of minimum element
for (int i = 1; i < values.length; i++)
if (values[i].compareTo(o1) < 0)
o1 = values[i];
// Return the minimum element in an array
return o1;
}
// Defining abstract max() method
public T max()
{
// 'T' is typename and 'o1' is object_name
T o1 = values[0];
// Iterating via for loop over elements using
// length() method to get access of minimum element
for (int i = 1; i < values.length; i++)
if (values[i].compareTo(o1) > 0)
o1 = values[i];
// Return the maximum element in an array
return o1;
}
}
// Class 2 - Main class
// Implementation class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom entries in an array
Integer arr[] = { 3, 6, 2, 8, 6 };
// Create an object of type as that of above class
// by declaring Integer type objects, and
// passing above array to constructor
MyClass obj1 = new MyClass(arr);
// Calling min() and max() methods over object, and
// printing the minimum value from array elements
System.out.println("Minimum value: " + obj1.min());
// printing the maximum value from array elements
System.out.println("Maximum value: " + obj1.max());
}
}
Number to Double Conversion:
v: 10.0
v: 136.8000030517578
输出说明:这里GenericConstructor()声明了一个泛型类型的参数,它是 Number 的子类。可以使用任何数字类型(如 Integer、Float 或 Double)调用GenericConstructor() 。因此,尽管GenericConstructor()不是泛型类,但它的构造函数是泛型的。
通用接口 在Java中是处理抽象数据类型的接口。接口帮助从表示细节独立操作Java集合。它们用于在Java形成层次结构中实现多重继承。它们与Java类不同。这些仅包括所有抽象方法,只有静态和最终变量。唯一可以创建接口的引用,而不是对象,与类不同,这些不包含任何构造函数、实例变量。这涉及到“implements”关键字。这些类似于泛型类。
通用接口的好处如下:
- 这是针对不同的数据类型实现的。
- 它允许对实现接口的数据类型施加约束,即边界。
句法:
interface interface-Name < type-parameter-list > {//....}
class class-name implements interface-name {//...}
实现:下面的例子创建了一个接口“MinMax”,它涉及非常基本的方法,如 min()、max() 只是为了说明它们返回 给定对象的最小值和最大值。
例子
Java
// Java Program to illustrate Generic interfaces
// Importing java input output classes
import java.io.*;
// An interface that extends Comparable
interface MinMax > {
// Declaring abstract methods
// Method with no body is abstract method
T min();
T max();
}
// Class 1 - Sub-class
// class extending Comparable and implementing interface
class MyClass >
implements MinMax {
// Member variable of 'MyClass' class
T[] values;
// Constructor of 'MyClass' class
MyClass(T[] obj) { values = obj; }
// Now, defining min() and max() methods
// for MimMax interface computation
// Defining abstract min() method
public T min()
{
// 'T' is typename and 'o1' is object_name
T o1 = values[0];
// Iterating via for loop over elements using
// length() method to get access of minimum element
for (int i = 1; i < values.length; i++)
if (values[i].compareTo(o1) < 0)
o1 = values[i];
// Return the minimum element in an array
return o1;
}
// Defining abstract max() method
public T max()
{
// 'T' is typename and 'o1' is object_name
T o1 = values[0];
// Iterating via for loop over elements using
// length() method to get access of minimum element
for (int i = 1; i < values.length; i++)
if (values[i].compareTo(o1) > 0)
o1 = values[i];
// Return the maximum element in an array
return o1;
}
}
// Class 2 - Main class
// Implementation class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom entries in an array
Integer arr[] = { 3, 6, 2, 8, 6 };
// Create an object of type as that of above class
// by declaring Integer type objects, and
// passing above array to constructor
MyClass obj1 = new MyClass(arr);
// Calling min() and max() methods over object, and
// printing the minimum value from array elements
System.out.println("Minimum value: " + obj1.min());
// printing the maximum value from array elements
System.out.println("Maximum value: " + obj1.max());
}
}
Minimum value: 2
Maximum value: 8
输出说明:这里接口声明为类型参数T,其上界为Java.lang中的Comparable。这说明了如何根据对象类型比较对象。上面的 T 由 MyClass 声明并进一步传递给 MinMax,因为 MinMax 需要一个实现 Comparable 的类型,并且实现 class(MyClass) 应该具有相同的边界。
Note: Once a bound is established, it is not necessary to state it again in implements clause. If a class implements generic interface, then class must be generic so that it takes a type parameter passed to interface.