Java Object-oriented generics

Source: Internet
Author: User
Tags java se

This first knowledge combing blog, I hope I can continue to summarize the knowledge.

This is the video you see in the Geek College, the catalogue is as follows, pretty clear. Then I'll remember what I said in the video.

Java object-oriented generics: Understanding Generics

A new feature of Java SE 1.5, the essence of generics is a parameterized type that can represent any data type.

Java object-oriented generics: using in constructors

Class gen<t>{

private T value;

No angle brackets are required for generics in Gen (T value) {//constructor

This.value=value;

}

public void SetValue (T t) {

this.value=t;

}

Public T GetValue () {

  return this.value;

      }

}

Public Class Demo () {

public static void Main (string[] args) {

gen<string> g = new gen<string> ("test");

Output G.getvalue ();

}

}

Java object-oriented generics: Specify multiple generics (typically two)

  

Class gen<t,k>{

Private T take;

Private K key;

Gen (T take,k key) {///constructor does not need angle brackets for generics

This.take=take;

This.key=key;

}

... set get method

}

Public Class Demo () {

public static void Main (string[] args) {

gen<string,integer> g = new gen<string,integer> ();

G.settake ("test");

G.setkey (10);

}

}

Java object-oriented generics: wildcard characters

When you want to specify a generic type for a parameter in a method, use the "? "For example, because the parameter is info<t>

Java object-oriented generics: generic interface

Add <T> after interface name

Interface gen<t>{

public void say ();

}

1. Class Genimpl implements gen<string>{

  Public Void Say () {

}

}

2.Class genimpl<t> implements gen<t>{

  Public Void Say () {

}

}

Specifying the data type when using Genimpl specifically

Java object-oriented generics: generic methods

Public <T> T method name (T t)

Returns the type of the T type assigned to the call, int i = tell (10);

Java object-oriented generics: generic arrays

public static <t>void Tell (T arr[]) {

for (int i=0;i<arr.length;i++) {

System.out.print (Arr[i])

}

}

Integer arr[]={1,2,3,4};

Tell (ARR);

http://blog.csdn.net/flfna/article/details/6576394

Java Object-oriented generics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.