java基础 第十一章(多态、抽象类、接口、包装类、String)

一、多态

    1.多态存在于继承和接口中。

    2.不确定行为放在父类中。

    3.子类必须重写父类中的不确定行为。

二、抽象类

    1.关键字 abstract。

    例:public abstract class Shap{

               public abstract void View();

           }

    2.抽象类中放不确定的行为方法。

    3.不能构建实例,因为有抽象方法。

    4.抽象类中有构造函数,方法和属性。

       子类默认调用父类的无参构造函数。

       如果父类中是有参构造函数,子类也要有构造函数来调用,关键字  super。

三、接口

    1.放多态方法。

    2.创建——> Interface。

    3.子类的关键字  implements 类名。

四、包装类

    1.包装类就是对象类型。

    2.Integer

      (1)可接收null值,但是涉及计算时,不要赋null值,会报空指针异常。

      (2)Integer b = new Integer(100);

               Integer b = 100; // 有自动装箱功能。

               System.out.println( b++ ); //自动拆箱功能

               例:Integer a = 200;

                      Integer b = 200;

                      System.out.println(a == b); //false

                      == 同一对象比较

                      System.out.println( a.equals(b) ) // true

                      equals 对象的值比较

      (3)方法

               intValue(), doubleValue(), byteValue(), floatValue(), longValue(), shortValue(), toString(),parseInt()//字符串转为整型数字

               例:int  V1 = b.intValue();

    3.Byte

      (1)Byte a = 100;

      (2)方法和Integer 大同小异。

              Byte b  =  a.parseByte("100"); //要是标准的数字字符串转为字节。

    4.Boolean

      (1)Boolean  a = true;

      (2)方法

               a.booleanValue();

               a.toString();

               a.parseBoolean() ;

              例:String s = "true";

                     boolean a = Boolean.parseBoolean(s);

                     System.out.println(a); // true

                     当s为其他值时 显示 false

    5.Character

      (1)Character c = 'a';

      (2)方法

               charValue()

               isLetter() //是否是一个字母

               isDigit() //是否是一个数字字符

               isWhiteSpace() //是否是一个空格

               isUpperCase() //是否大写字母

               toUpperCase() //指定大写形式

五、字符串 String

    1.String message = new String("hello world");

       String hello = "hello world";

    2.字符串池

       例:(1)String message = ”hello world”;

                 String hello = "hello world";

                 System.out.println(message == hello); //true

                 当字符串池已经有hello world了  两个对象就会用字符串中的同一个hello world。

               (2)String message = new String( ”hello world”);// 如果加上 .intern(),就会利用字符串池,否则new的会不利用字符串池

                   String hello = "hello world";

                   System.out.println(message == hello); //false 

      3.字符串的不可变性

          String a ="hello";

          String b = "hello';

          b = b + "world";

          System.out.println(a); //hello

          此时字符串池中有 hello 、world、hello world  

      4.减少字符串池中的垃圾

         StringBuffer  bf =  new StringBuffer();

         bf.append("hello");

         bf.append("world"); 

         bf.toString();

         只是字符串池中只有  hello world

      5.String 的方法

         (1)length(); //字符串长度

         (2)equals(); //对象的值比较

         (3)indexOf(); //字符串首次在查找的字符串的位置,下标从0开始,没有找到返回-1

         (4)lastIndexOf(); //从末尾查找字符串首次出现的位置,下标值也是从开始数

         (5)replace(换,替); //t替换指定的字符串

         (6)split(); //返回一个数组,拆分字符串

                  例:String stu1 = "李明-20-男";

                          String stu[] = stu1.split("-");

                          System.out.println(stu[1]); // 20

         (7)substring(开始,结束); // 截取指定长度字符串,没有结束值,截取到末尾

         (8)toUppercase(); //小写转大写

         (9)toLowerCase(); //大写转小写

         (10)charAt(); //取字符串中的第n个字符

         (11)concat(); //连结字符串

         (12)contains(); //该字符串是否存在于查找的字符串中

         (13)endsWith(); //判断末尾包含该字符串吗,可判断文件类型

                    例:a.endsWith(".mp4");

         (14)startsWith(); //判断开头包含该字符串吗,可判断URL协议

                    例:url.startsWith("http://');

         (15)equalsIgnore(); //忽略大小写,判断值比较

         (16)getByte(); //得到字节

原文地址:https://www.cnblogs.com/catcoffer/p/9007145.html