String
字符串类
特点:
1、一旦创建不会改变。
2、String对象不可变,但可共享。
3、"abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。
package ch_test;
public class Demo {
public static void main(String[] args) {
String s1 = "abc";
s1+="d";
System.out.println(s1);//s1指向新的字符串“abcd”,(特点1)
String s2="123";
String s3 = "123";//特点2
char[] chars=new char[]{'1','2','3'};//特点3
}
}
构造方法
// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {'a', 'b', 'c'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = { 97, 98, 99 };
String str3 = new String(bytes);
String各类方法例子
package ch_test;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
String s1 = "helloworld";
Scanner sc = new Scanner(System.in);
String s2 = sc.next();
// 比较字符串
System.out.println("比较字符串测试");
System.out.println(s1.equals(s2));
String s3 = "HeLLOWORLD";
System.out.println(s1.equalsIgnoreCase(s3));//忽略大小写的比较
// 获取字符串信息
System.out.println("获取信息测试");
System.out.println(s1.length());//字符串长度
s1 = s1.concat("!!!");//字符串拼接到末尾
System.out.println(s1);
System.out.println(s1.charAt(3));//获取指定处的char值
System.out.println(s1.indexOf('w'));//返回指定字符的索引值
System.out.println(s1.substring(3));//返回指定处到末尾的子串
System.out.println(s1.substring(3,8));//返回指定开始结束的子串
// 转换方法
System.out.println("转换方法测试");
char[] ch = s1.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
System.out.println();
System.out.println("---------------");
byte[] b = s1.getBytes();
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]);
}
System.out.println();
System.out.println("---------------");
String s4 = s1.replace("world","****");
System.out.println(s3);
}
}
static
可以修饰变量与方法,修饰的变量叫类变量,修饰的方法叫静态方法。
类变量:可以被所有对象共享,属于类。
静态方法:不用创建对象即可访问,如Arrays.tostring(),类名.方法名,不能有this(对象的属性)
package ch_test;
public class Student {
//成员变量 属于对象
String name;
int id;
//类变量 属于类
static String home;
// 静态方法 静态方法无法访问普通成员变量,同时也无法访问普通成员方法
public static void sleep(){
System.out.println("睡觉");
}
}
静态代码块:给类变量进行初始化
package ch_test;
public class Demo3 {
static String name;
// 静态代码块,类中方法外,随着类的加载而执行,优先于main方法,初始化的作用
static {
name = "123";
System.out.println("静态代码快");
}
public static void main(String[] args) {
System.out.println("main方法");
System.out.println(name);
}
}
Arrays
操作数组的工具类,提供全部静态方法。
package ch_test;
import java.util.Arrays;
public class Demo4 {
public static void main(String[] args) {
int[] a = new int[]{4,1,5,1,6,8,2,9};
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
继承
就是子类继承父类的
属性
和
行为
,使得子类对象具有与父类相同的属性、相同的行为。子类可以直接 访问父类中的非私有
的属性和行为。
复用!!!
成员变量和成员方法重名与不重名的实例与注意事项
成员变量的重名与方法的不重名:
package ch_ex_test;
public class Fu {
String name;
int id;
public Fu() {
}
public Fu(String name, int id) {
this.name = name;
this.id = id;
}
public void hello(){
System.out.println("hello");
}
}
package ch_ex_test;
public class Zi extends Fu{
int id;
public Zi(int id) {
this.id = id;
}
public Zi(String name, int id, int id1) {
//当成员变量重名时,构造函数的变化,
/*
* 建议不管父类子类都需要写构造函数
* */
super(name, id);
this.id = id1;
}
public void show(){
System.out.println("子类的show");
super.hello();
}
public static void main(String[] args) {
Zi zi = new Zi(12);
zi.show();
}
}
成员方法的重名——重写
构造方法无法被继承
package ch_ex_test;
public class Fu {
private String name;
private int id;
public Fu() {
}
public Fu(String name, int id) {
this.name = name;
this.id = id;
}
public void show(){
System.out.println("父类的show");
}
}
package ch_ex_test;
public class Zi extends Fu{
int id;
public Zi(int id) {
this.id = id;
}
public Zi(String name, int id, int id1) {
//当成员变量重名时,构造函数的变化,
/*
* 建议不管父类子类都需要写构造函数
* */
super(name, id);
this.id = id1;
}
//方法的重写
// 1. 子类方法覆盖父类方法,必须要保证权限大于等于父类权限。 2. 子类方法覆盖父类方法,返回值类型、函数名和参数列表都要一模一样
@Override
public void show() {
System.out.println("子类的show");
super.show();
}
public static void main(String[] args) {
Zi zi = new Zi(12);
zi.show();
}
}
抽象类
没有方法主体的方法称为
抽象方法
。
Java
语法规定,包含抽象方法 的类就是抽象类
1. 抽象类不能创建对象
,如果创建,编译无法通过而报错。只能创建其非抽象子类的对象。
理解:假设创建了抽象类的对象,调用抽象的方法,而抽象方法没有具体的方法体,没有意义。
2. 抽象类中,可以有构造方法,是供子类创建对象时,初始化父类成员使用的。
理解:子类的构造方法中,有默认的
super()
,需要访问父类构造方法。
3. 抽象类中,不一定包含抽象方法,但是有抽象方法的类必定是抽象类。
理解:未包含抽象方法的抽象类,目的就是不想让调用者创建该类对象,通常用于某些特殊的类结构设 计。
4. 抽象类的子类,必须重写抽象父类中所有的
抽象方法,否则,编译无法通过而报错。除非该子类也是抽象 类。
理解:假设不重写所有抽象方法,则类中可能包含抽象方法。那么创建对象后,调用抽象的方法,没有意义
package ch_ex_test;
public abstract class Fu {
private String name;
private int id;
public Fu() {
}
public Fu(String name, int id) {
this.name = name;
this.id = id;
}
public abstract void show();
}
package ch_ex_test;
public class Zi extends Fu{
int id;
public Zi(int id) {
this.id = id;
}
public Zi(String name, int id, int id1) {
//当成员变量重名时,构造函数的变化,
/*
* 建议不管父类子类都需要写构造函数
* */
super(name, id);
this.id = id1;
}
@Override
public void show() {
System.out.println("子类的show");
}
public static void main(String[] args) {
Zi zi = new Zi(12);
zi.show();
}
}