2016/6/7学习记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(){} }
以上是关于2016/6/7学习记录的主要内容,如果未能解决你的问题,请参考以下文章