java -- 接口

来源:互联网 发布:修改apache配置文件 编辑:程序博客网 时间:2024/06/11 19:42

一、接口
jdk1.8进行了优化
1.优化后可以可以写成员方方法 – > 可以写1.static静态方法 2.默认方法
a.默认接口不能直接调用方法,因为接口不可以实例化对象,如果要调用需要用,必须用该接口的实现类的对象去调用
b.默认方法在实现类中,不强制必须重写
c.如果想调用接口类中的默认方法,可以使用接口名.super.默认方法名();

抽象类 和 接口的区别
1.成员变量
抽象类:可以是常量和变量
接口:常量
2.成员方法
抽象类:有成员方法,抽象方法
接口:只能有 静态成员方法,抽象方法,默认方法
3.构造方法
抽象类:有
接口:没有

interface InterA{    int a = 10;//常量    public abstract void fun();    public static void staticfun1(){        system.out.print("我是接口中  静态成员方法")    }    public default void defaultfun2() {        System.out.println("我是接口中 默认的成员方法");    }}class InterAImpl implements InterA{    @Override    public void fun() {        System.out.println("我是实现类的  抽象方法的实现");    }    @Override    public void defaultfun2() {        //重写的时候 希望可以调到接口类的  默认方法        InterA.super.defaultfun2();        System.out.println("我是实现类的default");    }    //  实现类中定义一个成员方法    public void fun1() {        InterA.super.defaultfun2();        System.out.println("我是实现类中的成员方法 fun1");    }    public static void staticfun1() {        System.out.println("我是实现类的 静态方法");    }}public static void main(String[] args) {    //  调用接口中的静态成员方法    InterA.staticfun1();    InterA iA = new InterAImpl();    iA.defaultfun2();    //  必须是  先有向上转型 才能向下转型    InterAImpl a = (InterAImpl)iA;    a.fun1();    InterAImpl.staticfun1();}

最后的输出结果是

这里写图片描述

二、String 字符串系统方法用法

    public static void fun1() {    String string = "abc";    String string2 = new String("abc");    String string3 = "xyz";    System.out.println(string);    System.out.println(string2);    //比较的是地址(存在的位置)    System.out.println(string == string3);    System.out.println(string == string2);    //比较字符串是否相同    System.out.println(string.equals(string2));    /*     * 字符串的特点:=字符串是常量,不能改变     * string、string3相当于存放在 常量池     */}public static void fun2() {    //s1.indexOf("z")返回的是该字符在字符串第一次出现的下标    String s1 = "gaozhichao";    int i = s1.indexOf("z");    System.out.println(i);    //int j = s1.indexOf("o", 3)指定从什么位置开始,返回该字符出现的下标    int j = s1.indexOf("o", 3);    System.out.println(j);    //int k = s1.indexOf("chao")返回该字符串首次出现的下标    int k = s1.indexOf("chao");    System.out.println(k);    //int l = s1.indexOf("zhi", 6)指定位置开始,返回该字符串首次出现的下标    int l = s1.indexOf("zhi", 1);    System.out.println(l);}public static void fun3() {    String s1 = "www.baidu.com";    //该字符串中包含 给定字符串 返回turn 反之     boolean rel1 = s1.contains("baidu");    System.out.println(rel1);    //该字符串是否是 给定字符串开始的 是返回true  反之    boolean rel2 = s1.startsWith("w");    System.out.println(rel2);    //该字符串是否是 给定字符串结尾的 是返回true  反之    boolean rel3 = s1.endsWith("m");    System.out.println(rel3);}public static void fun4() {    //返回 给定的字符串 替换 之前的字符串    String s1 = "gaozhichao";    String s2 = s1.replace("chao", "qiang");    System.out.println(s2);}public static void fun5() {    //将给定的字符串 切割掉 返回的是字符串数组    String s1 = "gao,zhi,chao";    String[] array = s1.split(",");    for (String string : array) {        System.out.println(string);    }}public static void fun6() {    //从给定的索引开始获取新的子字符串 新字符串包括给定索引下的字符    String s1 = "gaozhichaoZNB";    String s2 = s1.substring(5);    System.out.println(s2);    //给定下表范围 获取新字符串 新字符串不包括结束索引的字符    String s3 = s1.substring(3, 10);    System.out.println(s3);    //  留头不留尾}public static void fun7() {    //将字符串小写都转换成大写    String s1 = "gaozhichao";    String s2 = s1.toUpperCase();    System.out.println(s2);    //将字符串大写都转换成小写    String s3 = s2.toLowerCase();    System.out.println(s3);}public static void fun8() {    //判断两个字符串是否相等(忽略大小写)返回true 或 false    String s1 = "gaozhichao";    String s2 = "GaoZhiChao";    System.out.println(s1.equalsIgnoreCase(s2));}public static void fun9() {    //去掉字符串前面和后面的空格    String s1 = "   gao  zhi  chao   ";    String s2 = s1.trim();    System.out.println(s2);}public static void fun10() {    //比较字符的ASCII码相同返回0,不相同返回两个字符串的差值,后面的不会再进行比较    String s1 = "abc";    String s2 = "bce";    System.out.println(s1.compareTo(s2));}public static void fun11() {    //将字符串转换为字符数组    String s1 = "gaozhichao";    char[] array = s1.toCharArray();    for (char c : array) {        System.out.println(c);    }    System.out.println(Arrays.toString(array));    //将字符数组 转换为 字符串    char[] array1 = new char[] {'g','a','o','z','h','i','c','h','a','o'};    String s2 = new String(array1);    System.out.println(s2);}public static void main(String[] args) {    //fun1();    //fun2();    //fun3();    //fun4();    //fun5();    //fun6();    //fun7();    //fun8();    //fun9();    //fun10();    fun11();}

例题

1.不用系统提供的.trim()方法 将字符串“   abc  def   ”前后的空格去掉public static void main(String[] args){    String s1 = "   abc  def  ";    //一位一位的查看,是否是空格,是就++    int start = 0;    int end = s1.lenth() - 1;    while(s1.charAt(start) == " " && start < end){        start++;    }    while(s1.charAt(end) == " " && start < end){        end--;    }    String s2 = s1.substring(start, end)    system.out.println(s2);}2.将“   abc  def   ”字符串反转public static void main(String[] args){    String s1 = "   abc  def  ";    char[] array = s1.toCharArray()    for(int i = 0; i < array.lenth/2; i++){        int temp = array[i];        array[i] = array[array.lenth - 1 - i];        array[array.lenth - 1 - i] = temp;    }    String s2 = new String(array);    system.out.println(s2);}3.计算字符串中  “wwwdosdwwwaswwwghwwwkl”  www出现的次数public static void main(String[] args){    String s1 = "wwwdosdwwwaswwwghwwwkl";    String key = "www";    int index = 0;    int number = 0;    while(s1.indexOf(key) != -1){        //记录第一次的位置        index = s1.indexOf(key);        //去掉查找到得key 获取新的子字符串        s1 = s1.substring(index + key.lenth());        number++;    }    system.out.println(number);}
原创粉丝点击