Java入门, 泛型编程

来源:互联网 发布:佛山网络推广 编辑:程序博客网 时间:2024/05/03 20:55
package org.lxh.demo16.generics;class Point<T> {private T x;private T y;public void setX(T x){this.x = x;}public void setY(T y){this.y = y;}public T getX(){return this.x;}public T getY(){return this.y;}}public class GenericsDemo {public static void main(String[] args){//Integer PointPoint<Integer> p = new Point<Integer>();p.setX(23);p.setY(56);System.out.println("my point is: (" + p.getX() + "," + p.getY() + ")");//Bill Gates mansionPoint<String> p2 = new Point<String>();p2.setX("47度37\'37.71\"N");p2.setY("122度14\'33.26\"W");System.out.println("Bill Gates mansion location at: (" + p2.getX() + "," + p2.getY() + ")");}}

原创粉丝点击