• 2016/6/7学习记录


    1.接口和抽象类的区别,详情见转载的几篇文章,分析的非常透彻

    http://blog.csdn.net/xw13106209/article/details/6923556

    几个小例子,有助于理解。

    package Demo;
    
    import java.util.Arrays;
    
    public class Test3 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            C[] s = { new C(1, 2), new C(2, 5), new C(3, 3), new C(1, 6) };
            Arrays.sort(s);
            for (C i : s)
                System.out.println(i.toString());
        }
    
    }
    
    class rectangle {
        private double width;
        private double height;
    
        public rectangle(double width, double height) {
            // TODO Auto-generated constructor stub
            this.width = width;
            this.height = height;
        }
    
        public double getArea() {
            return width * height;
        }
    
        public String toString() {
            return "width is " + width + "height is " + height;
        }
    }
    
    class C extends rectangle implements Comparable<rectangle> {
        public C(double width, double height) {
            super(width, height);
        }
    
        @Override
        public int compareTo(rectangle c) {
            if (getArea() > c.getArea())
                return 1;
            else if (getArea() == c.getArea())
                return 0;
            else
                return -1;
        }
    
        @Override
        public String toString() {
            return super.toString() + "the area is " + getArea();
        }
    }

    上面的例子是关于comparable接口的,对类C实现继承rectangle和Comparable接口,使得C类的对象可以进行比较,可以排序输出。

    package Demo;
    
    public class SSSS {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
        Student i = new Student("fan");
        i.sing();
        i.sleep();
        Painter j =(Painter)i;
        j.dance();
        j.paint();
        }
    
    }
    
    interface Singer{
        public void sing();
        public void sleep();
    }
    interface Painter{
        public void paint();
        public void dance();
    }
    
    class Student implements Singer,Painter{
        private String name ;
        public Student(String name) {
            this.name = name;
            // TODO Auto-generated constructor stub
        }
        public void study(){
        }
        public String getName(){
            return name;
        }
        public void sing(){
            System.out.println("I am sing!!!");
        }
        public void sleep(){
            System.out.println("I am sleeping");
        }
        public void paint(){
            
        }
        public void dance(){}
    
    }
  • 相关阅读:
    165. Compare Version Numbers
    163. Missing Ranges
    162. Find Peak Element
    161. One Edit Distance
    156. Binary Tree Upside Down
    工欲善其事-Eclipse设置
    2016年1月15日面试某互联网公司总结(一)
    以前用SQL实现的机构职能树,再看看
    Sublime3学习笔记
    Android之EACCES (Permission denied)与Permission denied异常探密
  • 原文地址:https://www.cnblogs.com/laigaoxiaode/p/5568742.html
Copyright © 2020-2023  润新知