JAVA-(接口实现数据回调 ,系统方法(String))

2019-08-13  本文已影响0人  宁晓鸯

心得体会


技术

1.接口实现数据回调

2.系统方法(String)


具体操作

1.接口实现数据回调

ReadChatMessage类都需要设置字体颜色和大小 Setting类就能完成这个功 能 所以大家都去找Setting类实现设置字体颜色和大小

Read类定义一个 Change接受数据

public class Read {
  private String text;
  private String  color;//默认的颜色
  private int  size;//默认的字体大小

  public Read(String text){//阅读什么文本
      this.text=text;
  }

//要进入到设置页面,就要先找到设置页面的一个对象
  public void goToSetting(){
      //1.创建页面设置的对象
      Setting setting=new Setting(this);
      setting.startSetting();
    }

    //提供给外部一个方法 可以通过这个方法给我传值
  @Override
    public void change(String color , int size){
      System.out.println("改变前的颜色:"+this.color+" 大小:"+this.size);
      this.color=color;
      this.size=size;
        System.out.println("改变后的颜色:"+this.color+" 大小:"+this.size);
    }

}

问题:

1.每添加一个对象使用Setting类 都必须添加一个这个类的一个对象 和 构造方法
2.每个类都有自己的接收⽅方法 导致方法不统一

Read r1;
Chat c1;
Message m1;
public Setting(Chat c1){
this.c1=c1;
}
public Setting(m1){
this.r1=r1;
}
public Setting(Message m1){
this.m1=m1;
}
Object obj;
if (obj instanceof Read) {

            Read r1 = (Read) obj;//将obj强制转换为Read类型
            if (r1 != null) {
                r1.change("黑色", 20);
            }
        }
            if (obj instanceof Chat) {
                Chat c1 = ( Chat) obj;//将obj强制转换为Chat类型
                if (c1 != null) {
                    c1.doChange("黑色", 20);
                }
            }
        }

1.定义一个内部接口

  public interface FontSettingInterface{
        //自己规定的方法
        public void change(String color,int size);
    }

2.创建对象

    //创建对象的时候 就需要告诉我 你是谁
 public Setting(FontSettingInterface obj){
     this.obj=obj;
 }

3.传递参数/数据

  //开始设置
    public void startSetting() {
        System.out.println("开始设置");
        System.out.println("..........");
        System.out.println("设置完毕 即将返回结果");

4.setting

public class Setting {
/**
 * 记录为谁设置颜色和大小
 * 记录我做完事情之后将数据返回给谁
 */
    FontSettingInterface obj;

    //创建对象的时候 就需要告诉我 你是谁
 public Setting(FontSettingInterface obj){
     this.obj=obj;
 }
    //使用接口定义一套方法 强制使用者来使用这个方法
    public interface FontSettingInterface{
        //自己规定的方法
        public void change(String color,int size);
    }
    //开始设置
    public void startSetting() {
        System.out.println("开始设置");
        System.out.println("..........");
        System.out.println("设置完毕 即将返回结果");
        //1.如果有可以访问的属性 直接通过属性给值
        //比较少用 对象没办法第一时间知道自己需要的值有了
        //r1.color="橘色";
        //r1.size=18;
        obj.change("黑色",20);
        //2.通过方法来回调

    }
}

运行结果


image.png

2.系统方法(String)

不可变字符串:一旦创建,内容不可改变

a.String str="abc";是定义一个不可变字符串,在此基础上若重新更改字符串的值是不可以的,但定义str="1234";是可以的,因为str="1234";只是定义了地址,并没有改变字符串的内容

String str="abc";
str="1234";//可以

b.== 比较两个对象是否相同,比较的是地址;equals比较内容是否相同

String str1="abc";
String str2="abc";  
System.out.println(str1==str2);
System.out.println(str1.equal(str2));

运行结果:

LL[NBWH}I%~RA2LC]3QO0JR.png
若将String str2="abc";改成String str2="abcd";,此时结果就不同了
String str1="abc";
String str2="abcd";  
System.out.println(str1==str2);
System.out.println(str1.equal(str2));

运行结果:

image.png
c.字符串的创建
String str1="abc";
 String str3=new String();
创建的该字符串没有内容,有对象,并且不可更改,所以没有意义
        byte[] name={'a','b','c'};
        String str4=new String(name);
        System.out.println(str4);

        byte[] name2={97,98,99};
        String str5=new String(name2);
        System.out.println(str5);

运行结果


image.png

以上运行结果是一样的,因为因为97,98,99会以字符串的形式打印出来

        String str6=new String(name,0,2);
        System.out.println(str6);

        char[] hello={'你','好','啊'};
        String h =new String(hello);
        System.out.println(h);

运行结果

image.png
d.字符串有哪些方法
        char[] hello={'你','好','啊'};
        String h =new String(hello);
        System.out.println(h);
        char c=h.charAt(2);
        System.out.println(c);
image.png
其中 char c=h.charAt(2);表示的是创建一个对象c,并且从字符串h里面取出第个字符

A.comepareTo(B):
输出结果为0时,A=B;
输出结果>0时,A>B;
输出结果<0时,A<B.

compareToIgnoreCase();方法:同样比较两个字符串的大小,但是它会忽略字符串的大小写

       byte[] name={'a','b','c'};
        String str4=new String(name);
        System.out.println(str4);

        byte[] name2={96,98,99};
        String str5=new String(name2);
        System.out.println(str5);

        int result=str4.compareTo(str5);
        System.out.println(result);

        int result1=str4.compareToIgnoreCase(str5);//忽略大小写
       System.out.println(result1);

运行结果

image.png
修改数据,将‘a'改为’A‘,有:
       byte[] name={'A','b','c'};
        String str4=new String(name);
        System.out.println(str4);

        byte[] name2={96,98,99};
        String str5=new String(name2);
        System.out.println(str5);

        int result=str4.compareTo(str5);
        System.out.println(result);

        int result1=str4.compareToIgnoreCase(str5);//忽略大小写
       System.out.println(result1);

运行结果


image.png

运行结果


image.png
       boolean r="hello".contains("lle");
        System.out.println(r);

运行结果


image.png
     String url="http://www.baidu.com";
        if(url.endsWith(".com")){
            System.out.println("网址");
        }
        if(url.endsWith("http")){
            System.out.println("http协议");
        }
        if(url.startsWith("www",7)){
            System.out.println("万维网");
        }
image.png

“http协议”没有打印出来,因为字符串url的结尾并不是http

        if("abc".equals("ABC")){
            System.out.println("相同");
        }else{
            System.out.println("不相同");
        }
        System.out.println(str4.hashCode());

运行结果


image.png
     //indexOf 不存在返回值为-1
        String i1 = "hello Java";
        int index = i1.indexOf("Java");
        System.out.println(index);

    //获取子字符串
        String sStr = i1.substring(7);//从第七个字符串到结尾
        System.out.println(sStr);

     //从0开始到5
        String sStr2 = i1.substring(0, 5);//获取第一个到第六个字符串
        System.out.println(sStr2);
    }
}

运行结果


image.png

StringBuffer()方法:线程安全,可变的字符序列
StringBuilder()方法: 线程不安全的 效率更高

  StringBuilder sb=new StringBuilder(6);//创建的同时先准备好6个字符的空间
        
        //append 在末尾追加
        sb.append("I ");
        sb.append(" Love ");
        sb.append("Android ");
        System.out.println(sb);

        //insert 插入数据
        sb.insert(2,"also");
        System.out.println(sb);

        //replace 替换
        //start end String
        int start=sb.indexOf("Android");
        int end=start+"Android".length();
        sb.replace(start,end,"you!!!");
        System.out.println(sb);

        //reverse 反转
        sb.reverse();
        System.out.println(sb);
    }
}

运行结果


image.png
上一篇 下一篇

猜你喜欢

热点阅读