12、 递归、泛型、增强for循环、方法重写

2019-06-21  本文已影响0人  浮生若梦OvO

一、递归

二、泛型

1、public class Tool2<QQ> {//泛型定义在类上
          public void show(QQ qq) {
          System.out.println(qq);
     }
}

2、public class Tool2Test {//测试类
     public static void main(String[] args) {

          Tool2<String> t = new Tool2<String>();
          t.show("hello");
          Tool2<Integer> t2 = new Tool2<Integer>();
          t2.show(10);
     }
}

b:泛型方法

1、public class Tool {
     public <BYD> void show(BYD byd) {//泛型定义在方法上
          System.out.println(byd);
     }
}

2、public class ToolTest {//泛型测试类
     public static void main(String[] args) {
          Tool t = new Tool();
          t.show("hello");//此处泛型为String类型
          t.show(10);//此处泛型为int型
     }
}

c:泛型接口:

1、public interface Inter<BMW> //泛型定义在接口上
{
     public abstract void show(BMW bmw);
  }
2、public class InterImpl<BWM> implements Inter<BWM>//实现类使用泛型
 {
     public void show(BWM bmw) //复写方法
{
          System.out.println(bmw);
     }
}
3、public class InterTest//测试类
 {
     public static void main(String[] args) 
{
          InterImpl<String> ii = new InterImpl<String>();//泛型测试
          ii.show("hello");
     }
}

三、增强for循环

四、方法重写

     public boolean equals(Object obj) {
            //为了提高代码效率
            if( this== obj){
                 return true;
           }
           
            //为了提高程序的健壮性
            //加一个判断:判断传递过来的对象是否是Student类型的。
            //怎么判断?
            //格式:对象名 instanceof 类名 判断对象是否是该类的对象,返回boolean类型
            if(!( obj instanceof Student)){
                 return false;
           }
           
            /*
            * this -- s1
            * obj -- s2
            */
           Student s =  (Student) obj;
            //重写equals一般就是为了比较某个类的多个对象的所有成员变量值是否相同
            //如果成员变量是基本类型,就用==比较
            //如果成员变量时引用类型,就用该变量对应的引用类型的equals()方法
            return this. age == s. age && this. name.equals( s. name);
     }
上一篇 下一篇

猜你喜欢

热点阅读