Java内部类、枚举、字符串的相关基础知识总结

(1)内部类

    类内部定义的类就叫做内部类,包含内部类的类也被叫做外部类或者宿主类
    内部类隐藏在外部类中,不允许同一个包中的其他类访问
    内部类成员可以直接访问外部类的私有数据,内部类被当成其外部类成员,同一个类的成员之间可以相互访问。但外部类不能访问内部类的实现细节,例如内部类的成员变量。

(2)非静态内部类

/**

外部类

@author Ray * */ public class Outer {

/**

内部类

@author Ray * */ public class Inner{

}

}

/**

        外部类

        @author Ray

*

*/

public class Outer {

        private int number;

        public Outer(int number) {

        this.number = number;

}

/**

        非静态内部类

        @author Ray

* */

public class Inner{

        private String name;

        private int age;

        public Inner(String name, int age) {

                super();

                this.name = name;

                this.age = age;

        } public void test() {

         //内部类中可访问外部类的私有成员

、     System.out.println("外部类的私有变量:"+age); } } }

/**

测试类

@author Ray * */ public class Test {

public static void main(String[] args) {

    Inner inner = new Outer(20).new Inner("张三",18);
    inner.test();

}

}

(3)静态内部类:

 当内部类被static修饰后,就成为了静态内部类,和类变量、类方法、静态代码块一样具有同等的地位。
 static 关键字的作用是把类的成员变成类相关,而不是实例相关,即 static 修饰的成员属于整个类,而不属于单个对象。

/**

外部类

@author Ray

* */

public class Outer2 {

        static int number = 100;

        int count = 200;

/**

*内部类

 @author Ray

* */

         static class Inner{

                public void print() {

                        //访问外部类类成员

                        System.out.println(number);

                        //编译出错,静态内部类不能访问外部类的实例变量

                        //System.out.println(count);

                }

        }

}

public class Test {

public static void main(String[] args) {
    
    Inner inner = new Outer2().Inner();
}

}

(4)局部内部类

如果把一个内部类放在方法里定义,则这个内部类就是一个局部内部类,局部内部类仅在该方法里有效。
由于局部内部类不能在外部类的方法以外的地方使用,因此局部内部类也不能使用访问控制符和 static 修饰符修饰。

/**

外部类

@author Ray * */ public class Outer3 {

        public void test() { /** * 局部内部类 * @author Ray * */ class Inner{

   }

}

(5)匿名内部类 new 实现接口() | 父类构造器(参数列表){ //内部类实现 }

(6)枚举类: 在特定情况下,一个类的对象是有限且固定的,这种实例有限且固定的类,Java中称为枚举类。 枚举类和普通类的差异: 1、枚举类可以实现一个或多个接口,使用 enum 定义的枚举类默认继承了java.lang.Enum 类,而不是默认继承 Object 类,因此枚举类不能显式继承其他父类。其中 java.lang.Enum 类实现. java.lang.Serializable 和 java.lang. Comparable 两个接口。 2、使用 enum 定义、非抽象的枚举类默认会使用 final 修饰,因此枚举类不能派生子类。 3、枚举类的构造器只能使用 private 访问控制符, 如果省略了构造器的访问控制符。则默认使用 private 修饰; 如果强制指定访问控制符,则只能指定 private 修饰符。 4、枚举类的所有实例必须在枚举类的第一行显式列出,否则这个枚举类永远都不能产生实例。列出这些实例时,系统会自动添加 public static final 修饰,无须程序员显式添加。

/**

性别 枚举

@author Ray

* */

public enum Gender {

        //相当于Gender类的对象 MALE("男"),FEMALE("女");

        public String name;

        private Gender(String name) {

                this.name = name;

        }

}

public class SwitchDemo {

public static void main(String[] args) {
    
    switch(Gender.FEMALE) {
        case FEMALE:
            System.out.println("女性");
            break;
        case MALE:
            System.out.println("男性");
    }
​
}

}

输出:女性

    枚举类中重要的方法:

/**

  • 测试类

  • @author Ray * */ public class Test {

    public static void main(String[] args) {

    //Values()方法返回该枚举的所有实例,即当前的MALE和FEMALE枚举值
    for(Gender g:Gender.values()) {
        
        //返回此枚举实例的名称,这个名称就是定义枚举类时列出的所有枚举值之一。
        System.out.println(g);
        //返回枚举值在枚举类中的索引值
        System.out.println(g.ordinal());
        //返回枚举常量的名称
        System.out.println(g.toString());
        
    }

    }

}

输出:

MALE 0 MALE FEMALE 1 FEMALE

    枚举类的构造方法:

/**

性别 枚举

@author Ray

* */

public enum Gender {

        //相当于Gender类的对象 MALE("男"),FEMALE("女");

        public String name; //定义构造方法

        private Gender(String name) { this.name = name; }

}

枚举中的抽象方法:

public enum Operation {

PLUS
{
    public double eval(int a,int b) {
        return a+b;
    }
},
MINUS
{
    public double eval(int a,int b) {
        return a-b;
    }
},
TIMES
{
    public double eval(int a,int b) {
        return a*b;
    }
},
DIVDE{
    public double eval(int a,int b) {
        return a/b;
    }
};
​
//定义抽象方法,为不同的枚举值提供实现
public abstract double eval(int a,int b);

}

/**

  • 测试类

  • @author Ray * */ public class Test {

    public static void main(String[] args) {

    System.out.println(Operation.PLUS.eval(5,2));
    System.out.println(Operation.MINUS.eval(5,2));
    System.out.println(Operation.TIMES.eval(5,2));
    System.out.println(Operation.DIVDE.eval(5,2));

    }

}

(7)字符串

    在Java中字符串就是连续的字符序列,Java提供了String,StringBuilder、StringBuffer 3个类来封装字符串

(8)String构造方法

    string类提供了大量的构造方法来创建String对象

public class Test {

public static void main(String[] args) {
​
    //通过字面量的方式创建字符串对象
    String s1 = "hello world";
    //创建一个包含0个字符的String对象
    String s2 = new String();
    //使用指定的字符集将指定的byte[]解码成一个新的String对象
    String s3 = new String(s1.getBytes(),Charset.forName("utf-8"));
    //根据字符串字面量来创建String对象
    String s4 = new String("世界你好");
    //根据StringBuffer对象来创建String
    String s5 = new String(new StringBuffer("StringBuffer"));
    //根据StringBuilder来创建对应的String对象
    String s6 = new String(new StringBuilder("StringBuilder"));
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(s3);
    System.out.println(s4);
    System.out.println(s5);
    System.out.println(s6);
}

}

    String类是一个不可变类,不可变类即类中的成员变量都使用final修饰,这也就说明一个String对象被创建以后,包含在这里对象中的字符序列是不可改变的,直到整个对象被销毁。并且在Java中所有字符串相关的类都是Charsquence接口的子类。

(9)String类中的API:

equals(String string)   判断两个字符串是否相等
equalsIgnoreCase(String string) 忽略大小写判断两个字符串是否相等
length()    获取字符串长度
charAt(int index)   获取某个索引处的字符
    判断字符串是否回文:
        /**
         * 
         * @author Ray
         *
        */
        public class Palindrome {
            public static void main(String[] args) {
                String s1 = new String("levelq");
                System.out.println(invert(s1));
                System.out.println(doublePointer(s1));
            }
            public static boolean invert(String s) {
                String temp = "" ;
                for(int i = s.length()-1;i>=0;i--) {
                    temp+=s.charAt(i);
                }
                if(temp.equals(s)) {
                    return true;
                }
                return false;   
            }
            public static boolean doublePointer(String s) {
                int left = 0;
                for(int right = s.length()-1;left<right;--right){
                    if(s.charAt(left)!=s.charAt(right)) {
                        return false;
                    }
                    left++;
                    right--;        
                }
                return true;
           }
        }
indexOf(String string)  获取字符串第一次出现的位置
indexOf(String string,int startIndex)   从startIndex处查找第一次出现的位置
lastIndexOf(String string)  字符串最后一次出现的位置
startsWith(String string)   判断是否以string开始
endsWith(String string) 判断是否以string结尾
compareTo(String string)    比较字符串大小
toLowerCase()   字符串转小写
toUpperCase()   字符串转大写
subString(int index)    从index位置处截取到字符串末尾
subString(int startIndex,int endIndex)  从startIndex位置开始,到endIndex结束,前闭后开
trim()  去除字符串首尾空格
split(String string)    以string对字符串进行分割,此方法会省略末尾空字符
split(String string,int limit)  对字符串进行分割,此方法不会省略末尾空字符
join(String s,String...str) 以s为连接符,连接str内字符串
concat(String str)  连接字符串
valueOf()   基本类型转字符串
contains(String str)    判断是否包含str
toCharArray()   将字符串转换成字符数组
intern()    判断字符串在常量池中是否存在,如果不存在,则复制,1.6是将实例复制,1.7及以后是将引用复制。
isEmpty()   判断字符串是否为空
​
实例:

public class StringDemo {

public static void main(String[] args) {
    String s1 = new String("http://WWW.baidu.com"); 
    String s2="baidu";
    
    //boolean equals(String string) 判断两个字符串是否相等 地址 长度 每个字符 equals只能判断是否相等,而compareTo除了看出是否相等,还等看出大小
    System.out.println("判断两个字符串是否相等");
    String s = new String("xyz");
    System.out.println("xyz".equals(s));//true

//boolean equalsIgnoreCase(String string)忽略大小写后判断两个字符串是否相等 System.out.println("XyZ".equalsIgnoreCase(s));//true

//int length() 获取字符串的长度 System.out.println(s2.length());//5

//char charAt(int index) 获取字符串对应索引的字符 char c = s2.charAt(1); System.out.println(c);//a

//indexOf(String string) 判断某个子字符串在字符串上第一次出现处的索引 System.out.println(s1.indexOf('.')); //10

    //indexOf(String string,int startIndex)判断某个子字符串在字符串上从指定索引startindex开始第一次出现处的索引
    System.out.println(s1.indexOf('.', 11));//16

//lastindexOf(String string) 返回指定子字符串在此字符串中最后一次出现处的索引。 System.out.println(s1.lastIndexOf('.')); //16

    //lastindexOf(String str,int endsIndex) 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引endsIndex开始反向搜索。
    System.out.println(s1.lastIndexOf('.',15)); //10

//boolean contains(String string) 判断前面的字符串是否包含后面的字符串 System.out.println("helloworld".contains("world"));//true System.out.println(s.contains("https://"));//false

    //boolean startsWith(String string)   判断当前字符串是否以某个字符串开始
    System.out.println(s1.startsWith("https://"));//false
    
    //boolean endsWith(String string)   判断当前字符串是否以某个字符串结尾
    System.out.println("test.txt".endsWith(".java"));//false
    System.out.println("test.txt".endsWith(".txt"));//true
    
    //int comparTo(String string) 
    System.out.println("按照字典顺序比较两个字符串大小");
    int res1 = "abc".compareTo("abc");
    System.out.println(res1);//0  前后一致 10-10 = 0
    int res2 = "abcd".compareTo("abcde");
    System.out.println(res2);//-1 前小后大 9-10 = -1
    int res3 = "abce".compareTo("abcd");
    System.out.println(res3);//1 前大后小 10-9 = 1
    int res4 = "abc".compareTo("bac");
    System.out.println(res4);//-1 两个字符串对应位置的字符依此按照字典顺序比较,分出胜负就不比较了
    
    //toLower() 将字符串转换为小写
    System.out.println(s1.toLowerCase());//http://www.baidu.com
    
    //toUpper() 将字符串转换为大写
    System.out.println(s1.toUpperCase());//HTTP://WWW.BAIDU.COM
    
    //string subString(int index) 将字符串从索引index位置截取到结尾
    System.out.println(s1.substring(7));//WWW.baidu.com
    
    //string subString(int index) 将字符串从索引startsindex位置截取到索引endsindex位置,前闭后开区间
    System.out.println(s1.substring(7,10));//WWW
    
    //trim() 去除字符串前后的空格
    System.out.println("  xyz  ".trim());//xyz
    
    //replace(String string) 返回一个新的字符串,它是通过用 `newChar` 替换此字符串中出现的所有 `oldChar` 得到的
    System.out.println(s1.replace("http://","https://"));//https://WWW.baidu.com
    
    //split(String string) 分割字符
    String [] time = "2022-5-22".split("-");
    for(int i=0;i<time.length;i++) {
        System.out.println(time[i]);
    }
    /*
     *  2022
        5
        22
     * */
    
    //split(String string,int limit) 分割,保留末尾的空字符
    System.out.println("");
    String [] time1 = "2022-5-22   ".split("-",3);
    for(int i=0;i<time1.length;i++) {
        System.out.println(time1[i]);
    }
    /*
     *  2022
        5
        22   
     * */
    
    //join(String s,str1,str2....) 以s为连接符,连接字符串
    System.out.println(String.join(".", "www","baidu.com"));//www.baidu.com
    
    //concat(String string) 将指定字符串连接到此字符串的结尾。
    System.out.println(s2.concat(".com"));//baidu.com
    
    //char[] toCharArray 将字符串转换为字符数组
    char [] chars=s2.toCharArray();
    for(int i = 0;i<chars.length;i++) {
        System.out.println(chars[i]);
    }
    /*
     *  b
        a
        i
        d
        u
     * */

//intern() 返回字符串对象的规范化表示形式。 System.out.println(s1.intern());//百度一下,你就知道

    //isEmpty() 判断某个字符串是否为空字符串 数组长度是length属性,字符串长度是length方法
    System.out.println(s2.isEmpty());//false    
}

}

课后练习:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好教员好

您的鼓励是我前进动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值