Chinaunix首页 | 论坛 | 博客
  • 博客访问: 267234
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: Java

2015-11-02 18:30:42


点击(此处)折叠或打开

  1. /*基本数据类型的封装
  2. *Integer
  3. Double
  4. Byte
  5. Boolean
  6. */
  7. public class test{
  8.     public static void main(String args[]){
  9.         Integer i=new Integer(256);
  10.         Integer j=new Integer("256");
  11.         System.out.println(i.intValue()*2); //将当前Integer类的对象转化为整型数据
  12.         System.out.println(i.doubleValue());//转为double
  13.         System.out.println("i="+i.toString());//转为字符串
  14.         System.out.println(i==j);//判断是否相同
  15.         System.out.println(i.compareTo(j));//判断是否相等
  16.         
  17.         System.out.println(Integer.parseInt("100")*2);//字符串转为整型
  18.         System.out.println("100*2="+Integer.toString(100*2));//整型转字符串
  19.         int k;
  20.         k=Integer.valueOf("1010",2).intValue();//字符串转化为Integer类的对象
  21.         System.out.println(k);
  22.         
  23.         System.out.println(Integer.MAX_VALUE);
  24.         System.out.println(Integer.MIN_VALUE);
  25.         
  26.     }
  27. }

点击(此处)折叠或打开

  1. /*Math 类*/
  2. public class test{
  3.     public static void main(String args[]){
  4.         System.out.println(Math.E);
  5.         System.out.println(Math.PI);
  6.         System.out.println(Math.exp(5.7));//e的参数次幂
  7.         System.out.println(Math.random());//随机数
  8.         System.out.println(Math.sqrt(9.08));//开方
  9.         System.out.println(Math.pow(2,3));//乘方
  10.         System.out.println(Math.round(99.6));//四舍五入
  11.         System.out.println(Math.abs(-8.09));//绝对值
  12.     }
  13. }

点击(此处)折叠或打开

  1. /**String类*/
  2. public class test{
  3.     public static void main(String args[]){
  4.         String s;
  5.         s="String test";//赋值方式
  6.         s=new String("String test");
  7.         
  8.         /*获取字符串长度*/
  9.         int stringLength=s.length();
  10.         System.out.println("stringLength="+stringLength);
  11.         
  12.         /*查询特定字符位置*/
  13.         int blankIndex=s.indexOf('s');
  14.         System.out.println("blankIndex="+blankIndex);
  15.         /*查询特定字符串的位置*/
  16.         int subStringIndex=s.indexOf("est");
  17.         System.out.println("subStringIndex="+subStringIndex);
  18.         /*获取特定位置的字符串*/
  19.         String sub=s.substring(2,5);
  20.         System.out.println("sub="+sub);
  21.         /*得到特定位置的字符*/
  22.         char singleChar=s.charAt(3);
  23.         System.out.println("singleChar="+singleChar);
  24.     }
  25. }

点击(此处)折叠或打开

  1. /*
  2. *Date 类
  3. void setTime(long)//设置新的时间
  4. long getTime();//返回时间为毫秒
  5. */
  6. import java.util.Date;
  7. public class test{
  8.     public static void main(String args[]){
  9.         Date currentDate=new Date();
  10.         /**获取当前时间*/
  11.         System.out.println(currentDate);
  12.         /*从1970年的毫秒时间*/
  13.         Date newDate=new Date(100000);
  14.         System.out.println(newDate);
  15.     /*判断当前时间与1970年时间的前后*/
  16.         System.out.println(currentDate.after(newDate));
  17.         System.out.println(currentDate.before(newDate));
  18.         
  19.     }
  20.     }

点击(此处)折叠或打开

  1. //Calendar类
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.GregorianCalendar;
  6. public class test{
  7.     //获取当前时间之前的某个时间
  8.     private static void doAdd(){
  9.         Calendar now=Calendar.getInstance();
  10.         Calendar working;
  11.         SimpleDateFormat formatter=new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
  12.         working=(Calendar)now.clone();
  13.         working.add(Calendar.DAY_OF_YEAR,-(365*2));
  14.         System.out.println("Two years "+formatter.format(working.getTime()));
  15.     }
  16.     //获取指定时间与当前时间的差
  17.     private static void doDate(){
  18.         Date startDate1=new GregorianCalendar(1994,02,14,14,00).getTime();
  19.         Date endDate1=new Date();
  20.         long diff=endDate1.getTime()-startDate1.getTime();
  21.         long time=diff/(1000L*60L*60L*24L);
  22.         System.out.println(time);
  23.     }
  24.     public static void main(String args[]){
  25.         doAdd();
  26.         doDate();
  27.     }
  28. }







接口:

点击(此处)折叠或打开

  1. /* interface
  2. 接口的定义格式.
  3. 接口的图形表示
  4. 接口的实现格式
  5. 有了接口,就可以得到多重继承的效果。
  6. 接口中的所有成员变量都默认是由public static final修饰的。
  7. 接口中的所有方法都默认是由public abstract修饰的。接口没有构造方法。

  8. 与抽象类一样,接口要使用也必须通过子类,子类通过implements关键字实现接口。
  9. */
  10. interface A{
  11.     String AUTHOR="李华";
  12.     void print();
  13.     String getInfo();
  14. }
  15. interface B{
  16.     public void say();
  17. }
  18. class X implements A,B{
  19.     public void say(){
  20.         System.out.println("hello world!");
  21.     }
  22.     public String getInfo(){
  23.         return "HELLO";
  24.     }
  25.     public void print(){
  26.         System.out.println("作者"+AUTHOR);
  27.     }
  28. }
  29. public class test1{
  30.     public static void main(String args[]){
  31.         X x=new X();
  32.         x.say();
  33.         x.print();
  34.     }
  35. }

点击(此处)折叠或打开

  1. /* interface
  2. 接口的定义格式.
  3. 接口的图形表示
  4. 接口的实现格式
  5. 有了接口,就可以得到多重继承的效果。
  6. 接口中的所有成员变量都默认是由public static final修饰的。
  7. 接口中的所有方法都默认是由public abstract修饰的。接口没有构造方法。

  8. 与抽象类一样,接口要使用也必须通过子类,子类通过implements关键字实现接口。

  9. */
  10. interface A{
  11.     public static final String AA="李华";
  12.     public void say();
  13.     
  14.     public void info();
  15. }
  16. abstract class B{
  17.     public abstract void print();
  18. }
  19. /* 同时接口和继承 */
  20. class C extends B implements A{
  21.     public void say(){
  22.         System.out.println("Hello");
  23.     }
  24.     public void print(){
  25.         System.out.println("print()");
  26.     }
  27.     public void info(){
  28.         System.out.println("info");
  29.     }
  30.     
  31. }
  32. public class test1{
  33.     public static void main(String args[]){
  34.         C c=new C();
  35.         c.say();
  36.         c.print();
  37.     }
  38. }

点击(此处)折叠或打开

  1. /* toString()方法来返回当前对象的一些重要属性。
  2. equals(obj,obj) */
  3. class Person {            // 定义Person类
  4.     private String name ;    // 定义name属性
  5.     private int age ;        // 定义age属性
  6.     private int num;
  7.     public Person(String name,int age,int num){//通过构造设置属性内容
  8.         this.name = name ;
  9.         this.age = age ;
  10.         this.num=num;
  11.     }
  12.     public String toString(){// 此处覆写toString()方法
  13.         return "姓名:" + this.name + ";年龄:" + this.age+"num"+this.num ;
  14.     }
  15. }
  16. public class test {
  17.     public static void main(String args[]) {
  18.         Person per = new Person("李华",30,2) ;//实例化Person对象
  19.         System.out.println("对象信息:" + per) ;// 打印对象调用toString()方法
  20.     }
  21. }




点击(此处)折叠或打开

  1. /*1.abstract抽象方法
  2. 2.抽象类的定义格式及使用规则
  3. 只有方法的声明,没有方法的实现的方法称为抽象方法
  4. 使用abstract定义的类被称为抽象类
  5. 1.抽象类不能被实例化(不能以抽象类为模板生成对象)
  6. 2.如果一个类当中包含抽象方法,那么这个类必须被声明为抽象类。
  7. 3.如果一个类当中没有抽象方法也可以被声明为抽象类。
  8. 4.抽象类必须被子类继承,子类(如果不是抽象类)必须覆盖抽象类中的全部抽象方法。

  9. */
  10. abstract class A{
  11.     public static final String FLAG="CHINA";
  12.     private String name="梨花";
  13.     public String getName(){
  14.         return name;
  15.     }
  16.     public void setName(String name){
  17.         this.name=name;
  18.     }
  19.     public abstract void print();
  20. }
  21. class B extends A{
  22.     public void print(){
  23.         System.out.println(FLAG);
  24.         System.out.println(super.getName());
  25.     }
  26. }
  27. public class test1{
  28.     public static void main(String args[]){
  29.         B b=new B();
  30.         b.print();
  31.     }
  32. }
阅读(1630) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~