zoukankan      html  css  js  c++  java
  • 泛型(二)

      上一次我分享了使用继承来实现泛型,今天讲一下使用接口类型表示泛型。

    只有在使用Object已有的那些方法能够表示所执行的操作时,才能使用Object表示泛型,例如要比较一些图形的面积大小时,用Object无法实现这个功能,这时我们可以写一个Shape类

    实现Comparable接口,通过重写compareTo方法来实现这个功能。

    public class Shape implements Comparable<Shape >{
    
        private Integer proportion;
    
        //含参构造方法
        public Shape(Integer proportion) {
            super();
            this.proportion = proportion;
        }
    
        public Shape() {
            super();
        }
    
        //重写compareTo方法,接收传入参数,比较proportion大小,返回数值
        public int compareTo(Shape s) {
            if(this.proportion == null || s.proportion == null){
                throw new RuntimeException("空指针异常");
            }else if(this.proportion>s.proportion)
                return 1;
            else if(this.proportion == s.proportion)
                return 0;
            else if(this.proportion < s.proportion)
                return -1;
            return 0;
        }
    
    }

    再创建三个类,分别是Circle,Square和Rectangle,这三个类都继承了Shape,并且初始化proportion,调用Shape的有参构造方法,将proportion传入Shape中,再通过compareTo

    方法得到最后的结果。

    public class Square extends Shape{
    
        private Integer proportion;
    
        public Square(Integer c) {
            super(c*c);
            this.proportion = c*c;
        }
        
        
        @Override
        public String toString() {
            return "正方形 [proportion=" + proportion + "]";
        }
        
    }
    public class Circle extends Shape{
    
        private Integer proportion;
    
        public Circle(Integer r) {
            super(3 *r*r);
            this.proportion = 3 *r*r;
        }
    
        @Override
        public String toString() {
            return "圆形 [proportion=" + proportion + "]";
        }
        
    }
    public class Rectangle extends Shape{
    
        private Integer proportion;
    
        public Rectangle(Integer c,Integer w) {
            super(c*w);
            this.proportion = c*w;
        }
    
        @Override
        public String toString() {
            return "长方形 [proportion=" + proportion + "]";
        }
        
    }

    将所有图形类型放入Shape数组中比较它们的大小,这时需要一个寻找最大元素的方法FindMaxDemo并测试

    public class FindMaxDemo {
    
        public static Shape findMax(Shape[] s){
            int maxIndex = 0;
            if(s == null)
                return null;
            for(int index=1;index<s.length;index++){
                if(s[index].compareTo(s[maxIndex])>0)
                    maxIndex = index;    
            }
            return s[maxIndex];
        }
        
        public static void main(String[] args){
            Shape[] s = new Shape[]{new Circle(3),new Square(4),new Rectangle(3, 4)};
            System.out.println(findMax(s));
        }
        
    }

    以上我们已经实现了比较图形大小的功能,但实际上我们所用原理还是上回讲的通过继承实现泛型,只是这次继承的不是Object,而是拥有compareTo方法的Shape,而我们还可以

    将泛型写的更彻底,这时我们就要使用Comparable接口来接收传入的参数

    public class FindMaxDemo2 {
    
        @SuppressWarnings("unchecked")
        public static <T> Comparable<T> findMax(Comparable<T>[] c){
            int maxIndex = 0;
            if(c == null)
                return null;
            for(int index=1;index<c.length;index++){
                if(c[index].compareTo((T)c[maxIndex])>0)
                    maxIndex = index;    
            }
            return c[maxIndex];
        }
        
        public static void main(String[] args){
            Shape[] s = new Shape[]{new Circle(3),new Square(4),new Rectangle(3,5)};
            String[] ss = new String[]{"circle","square","rectangle"};
            System.out.println(findMax(s));
            System.out.println(findMax(ss));
        }
    }

    这时我们发现我们不仅可以找出图形面积的最大值,还可以作其他类型的比较,例如String类型,通过ASCII码来比较

  • 相关阅读:
    Java实现 LeetCode 242 有效的字母异位词
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 344 反转字符串
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
  • 原文地址:https://www.cnblogs.com/Shevo/p/8491056.html
Copyright © 2011-2022 走看看