java应用技术
2(1)
一、泛型
1.在泛型中不能用基础数据类型如int只能用Integer
2.运用泛型的最大好处是避免了类型转型异常,泛型玩的是引用数据类型。
3.构造方法使用泛型
4.指定多个泛型类型class Person<T,V>
5.在申明时指定泛型的类型,如果没指定就会在编译时出现警告,但仍然可以执行,因为没指定类型就都是Object类型
Person<String,Integer>p =new Person<String,Integer>();
Person p =new Person();
6.泛型与子类继承的限制(在泛型中不能用父类来接受子类)
7.泛型通配符(一般用在参数传递时)
二、泛型接口
案例:
package cn.java.java2;
public class TestDemo2 {
public static void main(String[] args){
RealNet1 r1= new RealNet1();
r1.test();
}
}
interface Net<T>{
public T test();
}
/*class RealNet<T> implements Net<T>{
@Override
public T test() {
// TODO Auto-generated method stub
System.out.println(123+123);
return null;
}
}*/
class RealNet1 implements Net<Number>{
@Override
public Number test() {
// TODO Auto-generated method stub
System.out.println(123);
return null;
}
}
三、泛型方法
案例:
//泛型方法
package cn.java.java2;
public class TestDemo3 {
public static void main(String[] args) {
UtilTestDemo ut1 = new UtilTestDemo();
System.out.println(ut1.test(133));
}
}
class UtilTestDemo{
public<T> T test(T a){
return a;
}
}
四、泛型数组
案例:
package cn.java.java2;
public class TestDemo4 {
public static void main(String[] args) {
Integer[] num = {1,2,3,4};
Number[] s = {3,5.6,7,6};
ArrayTestDemo atd = new ArrayTestDemo();
atd.fun1(num);
atd.fun1(s);
}
}
class ArrayTestDemo{
public <T> void fun1(T[] a){
for(T x:a){
System.out.println(x);
}
}
public <T> T[] fun2(T[] a){
return a;
}
}
五、泛型的嵌套
案例:
//泛型的嵌套
package cn.java.java2;
public class TestDemo5 {
public static void main(String[] args) {
Teacher<Student<String>> t = new Teacher<Student<String>>();
t.teacher(new Student<String>("haoren","dadoudou"));
}
}
class Teacher<T>{
public void teacher(T a){
System.out.println("教:"+a);
}
}
class Student<V>{
private String name;
private V enjoy;
public Student(String name, V enjoy) {
this.name = name;
this.enjoy = enjoy;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public V getEnjoy() {
return enjoy;
}
public void setEnjoy(V enjoy) {
this.enjoy = enjoy;
}
@Override
public String toString() {
return name+"\n" + "兴趣:"+enjoy ;
}
}
2(1)
一、泛型
1.在泛型中不能用基础数据类型如int只能用Integer
2.运用泛型的最大好处是避免了类型转型异常,泛型玩的是引用数据类型。
3.构造方法使用泛型
4.指定多个泛型类型class Person<T,V>
5.在申明时指定泛型的类型,如果没指定就会在编译时出现警告,但仍然可以执行,因为没指定类型就都是Object类型
Person<String,Integer>p =new Person<String,Integer>();
Person p =new Person();
6.泛型与子类继承的限制(在泛型中不能用父类来接受子类)
7.泛型通配符(一般用在参数传递时)
二、泛型接口
案例:
package cn.java.java2;
public class TestDemo2 {
public static void main(String[] args){
RealNet1 r1= new RealNet1();
r1.test();
}
}
interface Net<T>{
public T test();
}
/*class RealNet<T> implements Net<T>{
@Override
public T test() {
// TODO Auto-generated method stub
System.out.println(123+123);
return null;
}
}*/
class RealNet1 implements Net<Number>{
@Override
public Number test() {
// TODO Auto-generated method stub
System.out.println(123);
return null;
}
}
三、泛型方法
案例:
//泛型方法
package cn.java.java2;
public class TestDemo3 {
public static void main(String[] args) {
UtilTestDemo ut1 = new UtilTestDemo();
System.out.println(ut1.test(133));
}
}
class UtilTestDemo{
public<T> T test(T a){
return a;
}
}
四、泛型数组
案例:
package cn.java.java2;
public class TestDemo4 {
public static void main(String[] args) {
Integer[] num = {1,2,3,4};
Number[] s = {3,5.6,7,6};
ArrayTestDemo atd = new ArrayTestDemo();
atd.fun1(num);
atd.fun1(s);
}
}
class ArrayTestDemo{
public <T> void fun1(T[] a){
for(T x:a){
System.out.println(x);
}
}
public <T> T[] fun2(T[] a){
return a;
}
}
五、泛型的嵌套
案例:
//泛型的嵌套
package cn.java.java2;
public class TestDemo5 {
public static void main(String[] args) {
Teacher<Student<String>> t = new Teacher<Student<String>>();
t.teacher(new Student<String>("haoren","dadoudou"));
}
}
class Teacher<T>{
public void teacher(T a){
System.out.println("教:"+a);
}
}
class Student<V>{
private String name;
private V enjoy;
public Student(String name, V enjoy) {
this.name = name;
this.enjoy = enjoy;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public V getEnjoy() {
return enjoy;
}
public void setEnjoy(V enjoy) {
this.enjoy = enjoy;
}
@Override
public String toString() {
return name+"\n" + "兴趣:"+enjoy ;
}
}