Basic Java Review: Generic

Source: Internet
Author: User

Introduction: design a Point class that represents coordinates. This class has two fields: horizontal and vertical. Three expressions of coordinates are required.

(1) Integer

(2) decimal

(3) String

/** Use wildcard to indicate coordinate points * when defining a class, T is used to represent a type, which only represents a placeholder. * However, the real type is not determined yet, */public class Point <T> {// private T x; // private T y; public T getX () {return x ;} public void setX (T x) {this. x = x;} public T getY () {return y;} public void setY (T y) {this. y = y ;}}

Test class:

Public class PointDemo {public static void main (String [] args) {// when a generic type is specified, only the reference type (package class of the basic data type) can be specified) point <Integer> p = new Point <> (); p. setX (1); p. setX (2); Point <Double> p2 = new Point <> (); p2.setX (2.0); p2.setY (3.0 ); point <String> p3 = new Point <String> (); p3.setX ("3"); p3.setY ("4 ");}}

 

The generic type appears at the beginning of Java 5 to support the creation of classes that can be parameterized by type.

The generic model ensures the type security and maintainability of large applications.

If multiple fields in a class need different generic declarations, you can specify multiple generic classes when declaring the class:

public interface IDAO<PK, T> {PK add(T t);void remove(PK id);void update(PK id, T t);T get(PK id);}

Use wildcard: The wildcard type must be matched before it can be passed during application transfer; otherwise, the compilation fails.

Use "?", Indicates an unknown generic object.

 

Upper and lower limits of generics:

Upper Limit: <? Extends Numer> indicates that the parameter type can only be of this I type or a subclass of this type.

Lower limit: <? Super String> indicates that the parameter type can only be of this type or its parent class.

 

Generic method:

/*** <T> indicates that the Declaration uses T to represent a type. The scope is only in this method class * @ param t */public static <T> void show (T t) {System. out. println (t. getClass (); // get the real type of the object}

 

Generic interface:

(1) Declare the generic DAO interface and let the subclass inherit

import java.io.Serializable;import java.util.List;public interface IGenericDAO<T> {void add(T o);void remove(Serializable id);void update(Serializable id,T newObj);T get(Serializable id);List<T> getAll();}

(2) Declare the subclass DAO interface and inherit the generic interface

public interface IDeptDAO extends IGenericDAO<Dept> {}

(3) Declare the implementation class of the generic DAO Interface

public class GenericDAOImpl<T> implements IGenericDAO<T>{@Overridepublic void add(T o) {}@Overridepublic void remove(Serializable id) {}@Overridepublic void update(Serializable id, T newObj) {}@Overridepublic T get(Serializable id) {return null;}@Overridepublic List<T> getAll() {return null;}}

(4) Declare the implementation class of the subclass Interface

public interface IDeptDAO extends IGenericDAO<Dept>{}

 

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.