代码总结1

1.抽象类和接口

 

 

abstractclass Sorter {

    privateintmax; // 该数据成员在这个类中没有起到什么作用。

    privateintmin;

 

    public Sorter() {

    }

 

    public String sort(int x, int y) {

       return (x > y ? (Integer.toString(x) + "    " + Integer.toString(y))

              : (Integer.toString(y) + "     " + Integer.toString(x)));

    }

 

    abstractpublicint[] sort(int[] arr);

}

 

class ArraySort extends Sorter {

    ArraySort() {

    }

 

    publicint[] sort(int[] arr) {

       int temp = 0;

       for (int i = 0; i < arr.length; i++)

           for (int j = i + 1; j < arr.length; j++)

 

              if (arr[i] > arr[j]) {

                  temp = arr[i];

                  arr[i] = arr[j];

                  arr[j] = temp;

              }

 

       return arr;

 

    }

}

 

publicclass test2 {

 

    publicstaticvoid main(String[] args) {

       int[] a = { 7, 2, 3, 6 };

       int[] b;

       ArraySort arr1 = new ArraySort();

       b = arr1.sort(a);

       // foreach(int element:a)为什么foreach不能正常的使用呢?

       // System.out.println(element);

       for (int i = 0; i < b.length; i++)

           System.out.print(b[i]);

 

    }

}

 

 

接口

 

interface PrintAble {

    publicabstractvoid print();

}

 

class Circle implements PrintAble {

    privatedoubleradius;

    privatedoublezhouchang;

    privatedoublearea;

 

    Circle() {

       radius = 0;

       zhouchang = 0;

       area = 0;

 

    }

 

    Circle(double x) {

       radius = x;

       zhouchang = 2 * x * 3.1415926;

       area = 3.1415926 * x * x;

    }

 

    publicvoid print() {

       System.out.print("半径:" + radius + 周长:" + zhouchang + 面积:" + area);

    }

 

}

 

class MakeUp {

 

    private String content;

 

    MakeUp() {

       content = null;

 

    }

 

    MakeUp(String s) {

       content = s;

    }

 

    publicvoid print() {

       String t = new String();

       for (int i = 0; i < content.length(); i++)

           t = t + "=";

       System.out.print(t + content + t);

    }

}

 

publicclass test31 {

 

    publicstaticvoid main(String[] args) {

 

       Circle c = new Circle(8.0);

       c.print();

       System.out.println();

       MakeUp content1 = new MakeUp("现在你好吗");

       content1.print();

    }

 

}

 

接口2

interface Shape {

    abstractpublicdouble Area();

    abstractpublicvoid printArea();

}

 

class Circle1 implements Shape {

    privatedoubler;

    publicstaticfinaldoublePI = 3.1415;

 

    Circle1(double i) {

       r = i;

    }

 

    publicdouble Area() {

       return (r * PI * r);

    }

 

    publicvoid printArea() {

       System.out.println("Circle:  " + "r=" + r + area=" + Area());

    }

}

 

class Rectangle implements Shape {

    privatedoubleheight;

    privatedoublewidth;

 

    Rectangle(double x, double y) {

       height = x;

       width = y;

 

    }

 

    publicdouble Area() {

       return (height * width);

    }

 

    publicvoid printArea() {

       System.out.println("Rectangle:  " + "height=" + height + "   width="

              + width + area=" + Area());

    }

}

 

publicclass test5 {

 

    publicstaticvoid main(String[] args) {

//你发现与上一个程序有所不同的就是其实接口实现向上转型

       Shape s = new Circle1(8.9);

       s.printArea();

       System.out.println();

       Shape b = new Rectangle(3, 7);

       b.printArea();

 

    }

}

 

 

 

 

/*6、设计一个Time类,描述如下:

 属性:年、月、日、时、分、秒。

 方法:设置时间参数(可单独设置年、月、日、时、分、秒,也可进行整体设置)

 获取时间参数(按yyyy-MM-dd字符串格式返回代表时间字符串)

 时间比较功能(针对另一个Time对象,能够比较当前时间在某个时间单位上的差值)。

 编程实现这个时间类,并创建对象,调用相应的方法,计算20091225230000秒与现在的时间相差多少天。*/

 

import java.text.*;

import java.util.*;

import java.io.*;

 

class Time {

    privateintyear;

    privateintmonth;

    privateintday;

    privateinthr; //

    privateintmin; //

    privateintsec; //

 

    public Time(int yea, int mont, int da, int h, int mi, int se) {

       this.setTime(yea, mont, da, h, mi, se);

    }

 

    /**

    注意允许外界访问的的函数一定为public的访问权限,但是要注意类中的所有的函数都是public的、

    ,有一些函数用来辅助另一些函数的,而这些辅助函数没有必要向外界公开,则最好将他们的访问权限设置为private

     */

    privatevoid setTime(int yea, int mont, int da, int h, int mi, int se) {

       this.year = yea;

       this.month = mont;

       this.day = da;

       this.hr = h;

       this.min = mi;

       this.sec = se;

    }

    privatevoid set_time1() {

       int year, month, day, h, m, s;

       System.out.println("设置日期:");

       System.out.println("请输入日期信息:格式为;yyyy-MM-dd HH:mm:s" +

              "s");

        String news = null;

        Scanner reader1 = new Scanner(System.in);

       //news = reader1.next();next这个函数的输入的时候不能用空格间断,否则,它只接受空格之间的内容。

        news = reader1.nextLine();

      /*  System.out.println(news);*/

       year = Integer.parseInt(news.substring(0, 4));

       month = Integer.parseInt(news.substring(5, 7));

       day = Integer.parseInt(news.substring(8, 10));

       h = Integer.parseInt(news.substring(11, 13));

       m = Integer.parseInt(news.substring(14, 16));

       s = Integer.parseInt(news.substring(17, 19));

    /*  System.out.println(year);

       System.out.println(month);

       System.out.println(day);

       System.out.println(h);

       System.out.println(m);

       System.out.println(s);用于验证自己是否正确将用户输入的信息分割。*/

       this.setTime(year, month, day, h, m, s);

    }

 

    privatevoid set_time2() {

       int x;

       int news;

       System.out.println("修改信息:");

       System.out.println("修改-----年:   请你按1键!");

       System.out.println("修改-----月:   请你按2键!");

       System.out.println("修改-----日:   请你按3键!");

       System.out.println("修改-----时:   请你按4键!");

       System.out.println("修改-----分:   请你按5键!");

       System.out.println("修改-----秒:   请你按6键!");

       Scanner reader1 = new Scanner(System.in);

       x = reader1.nextInt();

 

       while (true) {

           if (x == 1 || x == 2 || x == 3 | x == 4 | x == 5 | x == 6)

              break;

           else

              x = reader1.nextInt();

       }

       switch (x) {

       case 1: {

           System.out.println("请你输入新的年的信息:");

           news = reader1.nextInt();

           this.setTime(news, this.month, this.day, this.hr, this.min,

                  this.sec);

           break;

       }

       case 2: {

           System.out.println("请你输入月的年的信息:");

           news = reader1.nextInt();

           this.setTime(this.year, news, this.day, this.hr, this.min, this.sec);

           break;

       }

       case 3: {

           System.out.println("请你输入日的年的信息:");

           news = reader1.nextInt();

           this.setTime(this.year, this.month, news, this.hr, this.min,

                  this.sec);

           break;

       }

       case 4: {

           System.out.println("请你输入时的年的信息:");

           news = reader1.nextInt();

           this.setTime(this.year, this.month, this.day, news, this.min,

                  this.sec);

           break;

       }

       case 5: {

           System.out.println("请你输入分的年的信息:");

           news = reader1.nextInt();

           this.setTime(this.year, this.month, this.day, this.hr, news,

                  this.sec);

           break;

       }

       case 6: {

           System.out.println("请你输入秒的年的信息:");

           news = reader1.nextInt();

           this.setTime(this.year, this.month, this.day, this.hr, this.min,

                  news);

           break;

       }

 

       }

    }

    publicvoid set_Time() {

       int y;

       System.out.println("单独设置年、月、日、时、分、秒:   请你按1键!");

       System.out.println("整体设置:   请你按2键!");

       System.out.println("请你输入你修改时间的方式:");

       Scanner reader1 = new Scanner(System.in);

       y = reader1.nextInt();

       while (true) {

           if (y == 1 || y == 2)

              break;

           else {

              System.out.println("请你输入正确的选项!");

              y = reader1.nextInt();

           }

       }

       switch (y) {

       case 1:

           this.set_time2();

           break;

       case 2:

           this.set_time1();

           break;

 

       }

 

    }

 

    public String get_time()// 输出的格式为;"yyyy-MM-dd HH:mm:ss"

    {

       String datatime1;

       datatime1 = Integer.toString(this.year) + "-"

              + Integer.toString(this.month) + "-"

              + Integer.toString(this.day) + " " + Integer.toString(this.hr)

              + ":" + Integer.toString(this.min) + ":"

              + Integer.toString(this.sec);

       return datatime1;

    }

 

    publicvoid compare(Time time2) throws ParseException {

       Calendar calendar1, calendar2;

       long daysOfYear = 0;

       // Scanner scanner = new Scanner(System.in);

       calendar1 = Calendar.getInstance();

       calendar1.set(time2.year, time2.month, time2.day, time2.hr, time2.min,

              time2.sec);

       calendar2 = Calendar.getInstance();

       calendar2.set(this.year, this.month, this.day, this.hr, this.min,

              this.sec);

       daysOfYear = calendar2.getTimeInMillis() / 86400000

              - calendar1.getTimeInMillis() / 86400000;

       System.out.println("两个Time对象之间相距" + daysOfYear + "天。");

    }

}

 

publicclass test6 {

    publicstaticvoid main(String[] args) {

 

       Time t1 = new Time(2011, 01, 01, 12, 12, 03);

       Time t2 = new Time(2010, 00, 01, 12, 12, 00);

       System.out.println("获得Time对象的格式yyyy-MM-dd HH:mm:ss时间字符串  "

              + t1.get_time());

       System.out.println("获得Time对象的格式yyyy-MM-dd HH:mm:ss时间字符串  "

              + t2.get_time());

       System.out.println("设置时间  ");

       t1.set_Time();

       // Integer.

       System.out.println("时间比较!  ");

       try {

           t1.compare(t2);

       } catch (ParseException e) {

           // TODO自动生成 catch

           e.printStackTrace();

       }

 

    }

 

}

 

 

 

 

 

异常类的编写

 

 

class NullException extends Exception {//编写异常类,则需要继承exception

    //序列化

    privatestaticfinallongserialVersionUID = -7509566421893986291L;

    

    public NullException(String name) {

       super(name + "不允许为空!");

    }

 

}

 

class TestException2 {

    //抛出异常,用throws关键字,抛出的异常需要调用者来处理。

    publicvoid test() throws NullException {

       thrownew NullException("test.txt");

    }

}

publicclass test2 {

 

    publicstaticvoid main(String[] args) {

 

       TestException2 exception= new TestException2();

       try {

           exception.test();

       } catch (NullException e) {

 

           System.out.println(e.getMessage());

           // e.printStackTrace();

       }

    }

 

}

//一般编写异常类的方法,是利用父类的方法的基础上进行处理。

 

 

 

 

 

 

 

public interface Comparator<T>

比较函数强行对某些对象 collection 进行整体排序。可以将 Comparator 传递给 sort 方法(如 Collections.sort),从而允许在排序顺序上实现精确控制。还可以使用 Comparator 来控制某些数据结构(如 TreeSet 或 TreeMap)的顺序。

当且仅当对于一组元素 S 中的每个 e1 和 e2 而言,(compare((Object)e1, (Object)e2)==0) 与 e1.equals((Object)e2) 具有相等的布尔值时,Comparator c 强行对 S 进行的排序才叫做与等号一致 的排序

Set是一个不包含重复元素的 collection。更正式地说,set 不包含满足e1.equals(e2)的元素对e1e2,并且最多包含一个 null 元素。正如其名称所暗示的,此接口模仿了数学上的 set 抽象。

当使用具有与等号一致的强行排序能力的 comparator 对有序 set(或有序映射)进行排序时,应该小心谨慎。假定一个带有显式 Comparator c 的有序 set(或有序映射)与从 set S 中抽取出来的元素(或键)一起使用。如果 c 强行对 S 进行的排序与等号一致,那么有序 set(或有序映射)将是行为“怪异的”。尤其是那些将违背根据 equals 所定义 set(或映射)的常规协定的有序 set(或有序映射)。例如,如果使用 comparator c 将满足 (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) 的两个键 a 和 b 添加到有序 set 中,则第二个 add 操作将返回 false(有序 set 的大小没有增加),因为从有序 set 的角度来看,a 和 b 是相等的。

注:通常用 comparator 来实现 java.io.Serializable 是一个好主意,因为它们在可序列化的数据结构(像 TreeSet、TreeMap)中可用作排序方法。为了成功地序列化数据结构,comparator(如果已提供)必须实现 Serializable。

在算术上,定义给定 comparator c 对给定对象 set S 强行实施整体排序关系式 为:

       {(x, y) such that c.compare((Object)x, (Object)y) <= 0}.

整体排序的 是:

       {(x, y) such that c.compare((Object)x, (Object)y) == 0}.

 

它直接遵循 compare 的协定,商是 S 上的等价关系,自然顺序是 S 上的整体排序。当我们说 c 强行对 S 的排序是与等号一致 时,意思是说自然排序的商是对象的 equals(Object) 方法所定义的等价关系:

       {(x, y) such that x.equals((Object)y)}.

此接口是 Java Collections Framework 的成员。

、、

 

 

Equals的重写

class Circle {

    privatedoubler;

    publicstaticfinaldoublePI = 3.1415;

 

    Circle(double i) {

       r = i;

    }

 

    publicboolean equals(Object s) {

       Circle t;

       if (this == s)

           returntrue;

       if (s == null)

           returnfalse;

       if (s instanceof Circle) {

           t = (Circle) s;

           if ((this.r * this.r * this.PI) == (t.r * t.r * t.PI))

              returntrue;

 

       } else

           returnfalse;

 

       returnfalse;

 

    }

}

 

class Rectangle {

    privatedoubleheight;

    privatedoublewidth;

 

    Rectangle(double x, double y) {

       height = x;

       width = y;

 

    }

 

    publicboolean equals(Object s) {

       Rectangle t;

       if (this == s)

           returntrue;

       if (s == null)

           returnfalse;

       if (s instanceof Rectangle) {

           t = (Rectangle) s;

           if ((this.height * this.width) == (t.height * t.width))

              returntrue;

 

       } else

           returnfalse;

 

       returnfalse;

 

    }

}

 

publicclass test2 {

 

    publicstaticvoid main(String[] args) {

       Circle s = new Circle(8.9);

        Circle s1 = new Circle(8.4);

       System.out.println("两个圆进行面积比较:相等返回true,不相等为false,结果为:" + s.equals(s1));

       System.out.println();

       Rectangle b = new Rectangle(6, 4);

       Rectangle b1 = new Rectangle(4, 6);

       System.out.println("两个矩形进行面积比较:相等返回true,不相等为false,结果为:" + b.equals(b1));

 

    }

}

 

日期的处理

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.*;

 

publicclass test2 {

    publicstaticvoid main(String[] args) throws ParseException {

       String datanews;

       Date date;

       Calendar calendar;

       Scanner scanner = new Scanner(System.in);

       System.out.println("请输入你的日期:yyyy-MM-dd");

       datanews = scanner.next();

       // 创建一个日期格式对象

       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

       // 获得一个包含当前日期和时间的Calendar子类的实例

       calendar = Calendar.getInstance();

       // 按输入的产生一个Date对象

       date = dateFormat.parse(datanews);

       // date对象的值传递给calendar对象

       // calendar.setTime(date);

       long daysOfYear = Calendar.getInstance().getTimeInMillis() / 86400000

              - date.getTime() / 86400000 - 1;

       if (daysOfYear < 0)

           System.out.println("" + date.toLocaleString() + "还有"

                  + Math.abs(daysOfYear) + "天。");

       else

           System.out.println(date.toLocaleString() + "已经过去"

                  + Math.abs(daysOfYear) + "天。");

 

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值