luliyu-Java-day06

String

在这里插入图片描述

特点

  • 字符串不变:字符串的值在创建后不能被更改
package com.claire.day06;


public class Demo1 {
    public static void main(String[] args) {
        String s1 = "abc";
//        s1 += "d";
        s1 = s1 + "d";
        System.out.println(s1); // abcd

        String str = "abc";
        char data[] = {'a', 'b', 'c'};
        System.out.println("data[0] = " +data[0]);
        data[0] = 'A';
        System.out.println("data[0] = " +data[0]);
        String str2 =  new String(data);
        System.out.println(str2);
        System.out.println(str2.charAt(0));
        // 创建方法
        String ss1 = new String();
        // 字符数组
        char chars[] =  {'a', 'b', 'c'};
        String ss2 =  new String(chars);

        // 字节数组
        byte bytes[] = {97, 98, 99};
        String ss3 = new String(bytes);
        System.out.println(ss3);

        char c = 'A';
        System.out.println(c + 1);// 66
        
    }
}

常用方法

equals 将此字符串与指定对象进行比较。
equalsIgnoreCase 将此字符串与指定对象进行比较,忽略大小

package com.claire.day06;

public class Demo2 {
    public static void main(String[] args) {
        String s1 = "hello"; // 字符串常量池
        String s2 = "hello";
        String s3 = "Hello";
        System.out.println(s1==s2); //true
        System.out.println(s1.equals(s2)); //true
        System.out.println(s1.equals(s3)); //false
        System.out.println(s1.equalsIgnoreCase(s2)); //true
        System.out.println(s1.equalsIgnoreCase(s3)); //true

        String ns1 = new String("hello"); // 对象创建方式 堆去
        String ns2 = new String("hello");
        String ns3 = new String("Hello");
        System.out.println(ns1.equals(ns2)); //true
        System.out.println(ns1.equals(ns3)); //false
        System.out.println("+++++++++++++++++");
        System.out.println(ns1 == ns2); // false


    }
}

获取功能的方法

package com.claire.day06;

public class Demo3 {
    public static void main(String[] args) {
        String s = "helloword";
        System.out.println(s.length());
        String s2 = s.concat("luliyu");
        System.out.println(s);
        System.out.println(s2);
        System.out.println("+++++++++++++++");
        System.out.println(s.charAt(0));
        System.out.println("+++++++++++++++");
        System.out.println(s.indexOf('h'));
        System.out.println(s.indexOf("owo")); //4
        System.out.println(s.indexOf("l")); //2
        System.out.println("+++++++++++++++");
        System.out.println(s.substring(1));// elloword
        System.out.println(s.substring(1, 4));// ell 左闭右开
    }
}

转换功能

package com.claire.day06;

import java.util.Arrays;

public class Demo4 {
    public static void main(String[] args) {
        String s = "abcde";
        char[] chars = s.toCharArray();
        System.out.println(Arrays.toString(chars));

        byte[] bytes = s.getBytes();
        System.out.println(Arrays.toString(bytes));

        String replace = s.replace('a', 'A');
        System.out.println(s);
        System.out.println(replace);
        String s2 = "aa /bb /cc";
        String[] split = s2.split("/");
        String[] split2 = s2.split(" ");
        System.out.println(Arrays.toString(split));
        System.out.println(Arrays.toString(split2));



    }
}

定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:[word1#word2#word3]。

package com.claire.day06;

import java.util.Arrays;

public class Demo5 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};

        String s = arrayToString(arr);
        System.out.println(s);

    }
    // [word1#word2#word3]
    private static String arrayToString(int[] arr) {
        String s = new String("[");
        for (int i = 0; i < arr.length; i++) {
          if (i == arr.length-1)
              s = s.concat(arr[i] + "]");
          else
              s = s.concat(arr[i] + "#");
        }
        return s;
    }
}

键盘录入一个字符,统计字符串中大小写字母及数字字符个数

package com.claire.day06;

import java.util.Scanner;

public class Demo6 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String s = scanner.nextLine();
        int bigCount = 0;
        int smallCount = 0;
        int numCount = 0;

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch >='A'&& ch <='Z')
                bigCount++;
            else if (ch >='a'&& ch <='z')
                smallCount++;
            else if (ch >='0'&& ch <='9')
                numCount++;
            else
                System.out.println("该字符" +  ch + "非法");
        }
        System.out.println("bigCount = "+bigCount);
        System.out.println("smallCount = "+smallCount);
        System.out.println("numCount = "+numCount);
    }

}

static关键字关于

static关键字的使用,它可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单是属于某个对象的。也就是说,既然属于类,就可以不靠创建对象来调用了

  • 类变量 (具有记忆功能)
    当static修饰成员变量时,该变量称为类变量。该类的每个对象都共享同一个类变量的值。任何对象都可以更改该类变量的值,但也可以在不创建该类的对象的情况下对类变量进行操作。
package com.claire.day06;

public class Student {
    //成员变量
    private  String name;
    private  int age;
    private int sid =0;// 学号
    public  static int numberOfStudent = 0;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        this.sid = ++numberOfStudent;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sid=" + sid +
                '}';
    }
}

测试

  public static void main(String[] args) {
        Student s1 = new Student("张三", 12);
        Student s2 = new Student("李四", 43);
        Student s3 = new Student("王五", 32);
        Student s4 = new Student("赵六", 3);

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

在这里插入图片描述

  • 静态方法
  • 当static修饰成员方法时,该方法称为类方法创建类的对象。调用方式非常简单。使用static关键字修饰的成员方法,习惯称为静态方法。
public static void showNum(){System.out.println("num:" + numberOfStudent}

静态方法调用的注意事项:

  • 静态方法可以直接访问类变量和静态方法。
  • 静态方法不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。
  • 静态方法中,不能使用this关键字
静态方法只能访问静态成员。

静态代码块

定义在成员位置,使用static修饰的代码块{}。
位置:类中方法外。
执行:随着类的加载而执行且执行一次,优先于main方法和构造方法的执行。

package com.claire.day06;

import java.util.ArrayList;

public class Game {
    public static int number;
    public static ArrayList<String> list;

    static {
        number = 2;
        list = new  ArrayList<String>();
        list.add("zhangsan");
    }

    public static void main(String[] args) {
        System.out.println(number);
        System.out.println(list);
    }
}

Arrays类

package com.claire.day06;

import java.util.ArrayList;
import java.util.Arrays;

public class Demo8 {
    public static void main(String[] args) {
        int[]  arr =  {2,34,35,4,657,8,69,9};
        String s = Arrays.toString(arr);
        System.out.println(s);
        Arrays.sort(arr);

        System.out.println(Arrays.toString(arr));
    }
}

将一个随机字符串中的所有字符升序排列,并倒序打印。

package com.claire.day06;

import java.util.Arrays;

public class Demo9 {
    public static void main(String[] args) {
        String s = "ysKUreaytWTRHsgFdSAoidq";
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        for (int i = chars.length-1; i >=0 ; i--) {
            System.out.print(chars[i] +" ");

        }
    }
}

在这里插入图片描述
多个类可以称为子类,单独那一个类称为父类、超类(superclass)或者基类

继承:就是子类继承父类的属性和行为,使得子类对象具有与父类相同的属性、相同的行为。子类可以直接访问父类中的非私有的属性和行为。
好处
-提高代码的复用性。

  • 类与类之间产生了关系,是多态的前提。
    使用 extends关键字进行继承
    父类
public class Employee {
    String name;
    public void work(){
        System.out.println("快乐的工作着");
    }
}

子类

public class Teacher extends Employee{
    public void printName(){
        System.out.println("name = " + name);
    }
}

测试

public class Test {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.name = "小明";
        teacher.printName();
        teacher.work();
    }
}
```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值