Share ước chung trong java

#Java #estimation #Software #development #Programming ### Java Generics

Java Generics là một cách để viết mã có thể hoạt động với các loại dữ liệu khác nhau.Điều này có thể hữu ích khi bạn không biết bạn sẽ làm việc với loại dữ liệu nào hoặc khi bạn muốn viết mã có thể được sử dụng với nhiều loại dữ liệu.

Để sử dụng thuốc generic, bạn khai báo một tham số loại trong khai báo lớp, phương thức hoặc giao diện.Tham số loại này sau đó có thể được sử dụng để chỉ định loại dữ liệu mà lớp, phương thức hoặc giao diện có thể hoạt động.

Ví dụ: mã sau tuyên bố một lớp gọi là `list` có thể hoạt động với bất kỳ loại dữ liệu nào:

`` `java
Danh sách lớp công khai <T> {

riêng tư t [] các yếu tố;

Danh sách công khai () {
this.elements = new t [0];
}

bổ sung void công khai (phần tử t) {
this.elements = mảng.copyof (this.elements, this.elements.length + 1);
this.elements [this.elements.length - 1] = phần tử;
}

công khai T get (int index) {
trả về cái này.elements [index];
}

}
`` `

Lớp `list` hiện có thể được sử dụng để tạo danh sách bất kỳ loại dữ liệu nào.Ví dụ: mã sau tạo một danh sách các chuỗi:

`` `java
Danh sách <String> chuỗi = danh sách mới <> ();
chuỗi.add ("Xin chào");
chuỗi.add ("thế giới");

System.out.println (String.get (0));// Xin chào
System.out.println (String.get (1));// Thế giới
`` `

Java Generics cũng có thể được sử dụng để tạo các phương thức chung.Phương pháp chung là một phương pháp có thể được gọi với các loại đối số khác nhau.

Ví dụ: phương thức sau đây lấy một danh sách bất kỳ loại dữ liệu nào và trả về tổng của các phần tử trong danh sách:

`` `java
Tĩnh công khai <T> Double Sum (Danh sách <T> Danh sách) {
Double Sum = 0;
for (t phần tử: list) {
sum += phần tử.doubleValue ();
}
trả về tổng;
}
`` `

Phương thức `Sum` có thể được gọi với bất kỳ loại danh sách nào.Ví dụ: mã sau đây tổng hợp một danh sách các số nguyên:

`` `java
Danh sách <Ergeger> Integers = new ArrayList <> ();
số nguyên.ADD (1);
số nguyên.ADD (2);
số nguyên.ADD (3);

Double sum = sum (số nguyên);
System.out.println (tổng);// 6
`` `

Java Generics có thể là một công cụ mạnh mẽ để viết mã linh hoạt và có thể hoạt động với các loại dữ liệu khác nhau.

### hashtags

* #Java
* #Generics
* #phần mềm
* #phát triển
* #Programming
=======================================
#Java #estimation #Software #development #Programming ### Java Generics

Java generics are a way to write code that can work with different types of data. This can be useful when you don't know what type of data you will be working with, or when you want to write code that can be used with multiple types of data.

To use generics, you declare a type parameter in the declaration of a class, method, or interface. This type parameter can then be used to specify the type of data that the class, method, or interface can work with.

For example, the following code declares a class called `List` that can work with any type of data:

```java
public class List<T> {

private T[] elements;

public List() {
this.elements = new T[0];
}

public void add(T element) {
this.elements = Arrays.copyOf(this.elements, this.elements.length + 1);
this.elements[this.elements.length - 1] = element;
}

public T get(int index) {
return this.elements[index];
}

}
```

The `List` class can now be used to create lists of any type of data. For example, the following code creates a list of strings:

```java
List<String> strings = new List<>();
strings.add("Hello");
strings.add("World");

System.out.println(strings.get(0)); // Hello
System.out.println(strings.get(1)); // World
```

Java generics can also be used to create generic methods. A generic method is a method that can be called with different types of arguments.

For example, the following method takes a list of any type of data and returns the sum of the elements in the list:

```java
public static <T> double sum(List<T> list) {
double sum = 0;
for (T element : list) {
sum += element.doubleValue();
}
return sum;
}
```

The `sum` method can be called with any type of list. For example, the following code sums a list of integers:

```java
List<Integer> integers = new ArrayList<>();
integers.add(1);
integers.add(2);
integers.add(3);

double sum = sum(integers);
System.out.println(sum); // 6
```

Java generics can be a powerful tool for writing code that is flexible and can work with different types of data.

### Hashtags

* #Java
* #Generics
* #Software
* #development
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top