zoukankan      html  css  js  c++  java
  • Java自学课程笔记6

    2021年2月18日11点21分
    JAVA自学课程笔记6:
    
        Object是类层次结构的根类。每个类都使用Object作为超类,即每个类都继承了Object里的非私有方法。
    
        toString()方法:
            “System.out.println(类对象名);”实际输出的是该对象toString()方法所返回的字符串(类的名字和该对象哈希码组成的一个字符串)
            == 
            “System.out.println(aa.toString());”
    
        equals()方法:
            判断this和obj本身的值是否相等,即是否为同一对象,即是否内存同一块存储单元。(即便内容完全相等的两块不同的内存对象,也有可能返回false。同一块内存返回true,非同返回false)
            public boolean equals(Object obj){
                return this == obj;
            }
            
            若使用equals()方法判断成员的内容是否相同:
                class A{
                    public int i;
    
                    public A(int i){
                    this.i = i;
                    }
                }
    
                public class Test1{
                    public static void main(String[] args){
                    A aa1 = new A(2);
                    A aa2 = new A(2);
                    System.out.println(aa1.equals(aa2));
                    }
                }
            //输出结果:
                false
            
            重写equals()方法以判断成员的内容是否相同返回boolean值:
                class A{
                    public int i;
    
                    public A(int i){
                    this.i = i;
                    }
    
                    public boolean equals(Object obj){
                    A aa = (A)obj;
    
                    if(this.i == aa.i)
                        return true;
                    else
                        return false;
                    }
                }
    
                public class Test2{
                    public static void main(String[] args){
                    A aa1 = new A(2);
                    A aa2 = new A(2);
                    System.out.println(aa1.equals(aa2));
                    }
                }
            //输出结果:
                true
            
        String类:
            equal()方法被重写,通过检查Unicode字符是否相同返回对应的boolean值:
                public class test1{
                    public static void main(String[] args){
                    String a = "aaa";
                    String b = "aaa";
                    System.out.println(a.equals(b));
                    }
                }
            String类常用方法:
                public char charAt(int index)
                    返回字符串中第index个字符。
                public int length()
                    返回字符串的长度。
                public int indexOf(String str, int fromIndex)
                    返回字符串中从fromIndex开始出现str的第一个位置。
                public boolean equalsIgnoreCase(String another)
                    比较字符串与another是否一样(忽略大小写)。
                public String replace(char oldChar, char newChar)
                    在字符串中用newChar字符替换oldChar字符。
                public boolean startsWith (String pref ix)
                    判断字符串是否以prefix字符串开头。
                public boolean endsWith (String suffix)
                    判断字符串是否以prefix字符串结尾。
                public String toUpperCase ()
                    返向一个字符串为该字符串的大写形式。
                public String toLowerCase ()
                    返回一个字符串为该字符串的小写形式。
                public String substring (int beginIndex)
                    返回该字符串从beginIndex开始到结尾的子字符串。
                public String substring (int beginIndex, int endIndex)
                    返问该字符串从beginIndex开始到endIndex结尾的子字符串。
                public boolean startsWith (String pref ix)
                    判断字符串是否以prefix字符串开头。
                public boolean endsWith (String suffix)
                    判断字符串是否以prefix字符串结尾。
                public String toUpperCase () 
                    返回一个字符串为该字符串的大写形式。
                public String toLowerCase ()
                    返回一个字符串为该字符串的小写形式。
                public String substring (int beginIndex)
                    返回该字符串从beginIndex开始到结尾的子字符串。
                public String substring (int beginIndex , int endIndex)
                    返回该字符串从beginIndex开始到endIndex结尾的子字符串。
    
            public class Test1{
                public static void main(String[] args){
                    String s = "Mary,F,1976";
                    String[] sPlit = s.split(",");
                    for(int i = 0; i < sPlit.length;i++)
                        System.our.println(sPlit[i]);
                }
            }
            //运行结果:
            Mary
            F
            1976
    
            charAt(int index)方法取出,统计一个String对象中大写字母,小写字母,非字母各自出现的个数:
                class Test2{
                    public static void main(String[] args){
                        String str = "abAM1,!23";
                        int cntU = 0;    //大写字母的个数
                        int cntL = 0;    //小写字母的个数
                        int cntOther = 0;    //非字母的个数
                        int i;
                        String s1 = "abcdefghijklmnopqrstuvwxyz";
                        String s2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
                        for(i = 0; i < str.length(); ++i){
                            char ch = str.charAt(i);
                            
                            //if(ch>='a' && ch<='z')    //方法①
                            //if(-1 != s1.indexOf(ch))    //方法②
                            if(Character.isUpperCase(ch)){    //方法③
                                cntL++;
                            }
    
                            //else if(ch>='A' && ch<='Z')        //方法①
                            //else if(-1 != s2.indexOf(ch))        //方法②
                            else if(Characer.isLowerCase(ch)){    //方法③
                                cntU++;
                            }
    
                            else{
                                cntOther++;
                            }
                        }
                        System.out.printf("大写字母个数:%d
    ", cntU);
                        System.out.printf("小写字母个数:%d
    ", cntL);
                        System.out.printf("非字母个数:%d
    ", cntOther);
                    }
                }
    
                字符串本身就是个对象:
                    System.out.printf("%b
    ", "abc".equals("zhangsan"));
                    System.out.printf("%d
    ", "abc".length());
                    System.out.printf("%d
    ", "abcadssad".indexOf("ads"));
                    System.out.println(Integer.toHexString(47).toUpperCase());
                    //不能用toUpperCase(Integer.toHexString(47)),因为toHexString(47)的返回类型是public static String,而toUpperCase()的返回类型是public String。无法用非静态调用静态成员
                //结果:
                    false
                    3
                    3
                    2F
    
            StringBuffer类:
                由来:String类对象一旦创建就不可更改。如果经常对字符串内容进行修改而使用String的话,就会导致既耗空间又耗时间。
                
                常用方法:
                    public StringBuffer append(…)    //重载方法
                        包括(String str), (StringBuffer sbuf), (char[] str), (char[] str, int offset, int len), (double d), (Object obj)。用于在字符串结尾续写。
                    public StringBuffer insert(…)    //重载方法
                        包括(int offset, String str), (int offset, double d)。用于在指定位置插入字符序列。
                    public StringBuffer delete(int start, int end)
                        可以删除从start开始到end-1为止的一段字符序列。
                    public StringBuffer indexOf(String str)/(String str, int fromIndex)
                        返回从指定索引处开始第一次出现的指定子字符串在该字符串中的索引。
                    public StringBuffer reverse()
                        字符序列逆序。
    
                StringBuffer产生的对象可以直接打印:
                    StringBuffer aa = new StringBuffer("AAAA");
                    System.our.println(aa);        //结果:AAAA
    
                StringBuffer产生的对象不能和字符串互相转换:
                    StringBuffer aa;
                    aa = "AAAA";    //error
  • 相关阅读:
    Javascript獲取濟覽器高屏幕寬高
    引用CSS的問題
    轮胎尺寸周长一览表
    C# 配置文件
    C# 正则表达式替换分组内的内容
    按钮的背景图
    WPF 设置全屏
    窗体内嵌外部程序的显示,获取控件的图片
    将图像转换成一个图标
    resharper 6.0 注册码
  • 原文地址:https://www.cnblogs.com/yinjx/p/14647900.html
Copyright © 2011-2022 走看看