Java类
2024-07-13 00:32:24

Java 类与对象


1 基础

创建对象需要使用new关键字,使用new关键字创建的对象属性值都为默认的,想要创建时自定义值,可以使用构造函数,构造函数与类名相同,并且不需要写返回值,构造方法会在调用new关键字时只运行一次,会覆盖掉默认无参构造方法
使用.运算符,来使用对象的属性和方法
想要在方法中改变对象的值,可以使用this关键字,this关键字相当于对象本身

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// person.java
public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}

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

public String getName() {
return name;
}

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

// Main.java
public static void main(String[] args) {
Person person = new Person("test", 12);

System.out.println(person.getName());
}

1.1 静态属性 & 静态方法

使用static关键字来声明静态属性和方法
可以使用类名.属性/方法的形式来使用静态方法和属性

静态属性或静态方法可以理解为是这个类所有的属性或方法,所有基于这个类创建的对象都可以访问到,要是其中有一个对象改变了静态属性的值,那么其他对象读到的就是修改的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Persion.java
public class Person {
public int testStatic = 123;
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}

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

public String getName() {
return name;
}

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

public String showInfo() {
return "name: " + this.getName() + "\tage: " + this.getAge() + "\n";
}
}

// Main.java
public static void main(String[] args) {
Person person = new Person("test", 12);
Person person1 = new Person("test1", 123);

System.out.println(person.showInfo());
System.out.println(person1.showInfo());
}

1.2 包

所处包和目录是一一对应的,需要使用其他包里面的类时,需要使用import才能使用
如果一个包里面有多个类,可以使用*表示导入这个包中的全部类

1
2
import com.test.testClass.test1;
import com.test.testClass.*;

2 封装,继承,多态

2.1 封装

经对象内部的属性和方法封装起来,对客户端隐藏具体实现细节
封装的思想保证了类内部数据的完整性,避免了外部操作对内部数据的影响

2.2 继承

  • 使用extends关键字,语法为class 子类名 extends 父类名
  • 只支持单继承
  • 如果添加了final关键字的类,不允许被继承
  • 父类中如果有构造方法,子类中必须在构造方法中调用,用super表示父类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    // Person.java
    public class Person {
    public int testStatic = 123;
    private String name;
    private int age;

    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }

    public int getAge() {
    return age;
    }

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

    public String getName() {
    return name;
    }

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

    // PersonSonclass.java
    public class PersonSonclass extends Person {

    public PersonSonclass(String name, int age) {
    super(name, age);
    }

    public String showInfo() {
    return "name: " + this.getName() + "\tage: " + this.getAge() + "\n";
    }
    }

    // Main.java
    public static void main(String[] args) {
    PersonSonclass person = new PersonSonclass("test", 12);
    PersonSonclass person1 = new PersonSonclass("test1", 123);

    System.out.println(person.showInfo());
    System.out.println(person1.showInfo());
    }

    instanceof判断变量所应用的对象是什么类,如果变量所引用的对象是对应类型或对应类型的子类,返回true

2.3 多态

针对某个类型的方法调用,其真正执行的方法取决于运行时期实际类型的方法。

2.3.1 重写

子类对父类方法进行重写,调用时会使用重写后的方法

2.3.2 转型

  • 向上转型:子类对象实例化父类对象,这种属于自动转换(当我们需要多个同父的对象调用某个方法时,通过向上转换后,则可以确定参数的统一.方便程序设计)
  • 向下转型:父类对象实例化子类对象,这种属于强制转换(通过将父类强制转换为子类来调用子类独有的方法)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    // Pweson
    public class Person {
    public void print () {
    System.out.println("father.print");
    }
    }

    // PersonSonclass
    public class PersonSonclass extends Person {
    @Override
    public void print () {
    System.out.println("son.print");
    }
    }

    // main
    public static void main(String[] args) {
    Person person = new PersonSonclass(); //通过子类实例化父类
    person.print(); // son.print
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Person
public class Person {
public void print () {
System.out.println("father.print");
}
}

// PersonSonclass
public class PersonSonclass extends Person {
@Override
public void print () {
System.out.println("son.print");
}

public void test () {
System.out.println("test");
}
}

// Main
public static void main(String[] args) {
func(new Person());
func(new PersonSonclass());
}

public static void func( Person a ) {
a.print();
if (a instanceof PersonSonclass) {
((PersonSonclass) a).test(); // 调用独有的方法
}
}

3 抽象类

使用abstract关键字创建一个抽象类
抽象类无法使用new关键字,子类必须实现抽象类中所有抽象方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Person
public abstract class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

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 void say() {};
}

// Student
public class Student extends Person {
public Student(String name, int age) {
super(name, age);
}

@Override
public void say () {
System.out.println("Student: " + this.getName() + ":" + this.getAge());
}
}

// Main
public static void main(String[] args) {
Student student = new Student("name", 12);

student.say();
}

4 接口

使用interface声明一个接口
实现一个接口需要使用implements关键字,可以一次实现多个接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// test
public interface test {
void print();
void printTest();
}

// testImp
public class testImp implements test{
@Override
public void print() {
System.out.println("print 函数");
}

@Override
public void printTest() {
System.out.println("printTest 函数");
}
}

5 内部类

定义在另一个类中的类

1
2
3
4
5
public class test {
public class test1 {
// 定义一个内部类
}
}

内部类不能单独存在,必须依靠外部类的实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Main {
public static void main(String[] args) {
Student student = new Student("张三", 12);
Student.StudentInner studentInner = student.new StudentInner();

studentInner.say();
}

public static class Student {
private String name;
private int age;

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

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;
}

class StudentInner {
public void say () {
System.out.println("Hi " + getName());
}
}
}
}

5.1 成员内部类

写在成员的位置,属于外部类的成员之一

5.2 静态内部类

在静态内部类只能访问外部类的静态属性和方法,想要访问其他属性需要创建对象

5.3 局部内部类

将内部类定义在方法中,就是局部内部类
局部内部类的访问仅限于方法内或者该作用域内

5.4 匿名内部类

隐藏了名字的内部类

1
2
3
new 类名或者接口名() {
重写方法
}

6 核心类

6.1 String

String是一个引用类型,本身为class,最重要特点就是字符串不可变

6.1.1 比较

比较两个字符串是否相同时,应该使用equals()这个方法
忽略大小写可以使用equalsIgnoreCase()这个方法

1
2
3
4
5
6
7
8
public static void main(String[] args) {  
String string1 = "test";
String string2 = "test";
String string3 = "test1";

System.out.println(string1.equals(string2));
System.out.println(string1.equals(string3));
}

6.1.2 更多方法

检测某个字符串是否包含某个字符串用contains()这个方法

1
2
3
4
5
6
public static void main(String[] args) {  
String string = "test";

System.out.println(string.contains("t"));
System.out.println(string.contains("a"));
}

返回某个字符在字符串中的索引(从零开始)

1
2
3
4
5
public static void main(String[] args) {  
String string = "hello";
System.out.println(string.indexOf('e')); // 1
System.out.println(string.lastIndexOf('l')); // 3
}

检查字符串是否以xxx开头或者结尾

1
2
3
4
5
public static void main(String[] args) {  
String string = "hello";
System.out.println(string.startsWith("h")); // 检查开头
System.out.println(string.endsWith("lo")); // 检查结尾
}

使用substring()来截取字符串

1
2
3
4
5
public static void main(String[] args) {  
String string = "hello";
System.out.println(string.substring(1, 4)); // llo
System.out.println(string.substring(3)); // lo
}

trim()清除字符串首尾的空格字符串(该方法是返回立刻一个新的字符串)

1
2
3
4
5
public static void main(String[] args) {  
String string = " \n \t test \n\t";
System.out.println(string);
System.out.println(string.trim());
}

strip()也可以清除字符串首尾的空格字符串,但是可以清楚类似中文空格也可以清除
stripLeading()清除字符串后
stripTrailing()清除字符串前

isEmpty()isBlank()来判断字符串是否为空和空白字符串

1
2
3
4
5
6
public static void main(String[] args) {  
System.out.println("".isEmpty()); // t
System.out.println(" ".isEmpty()); // f
System.out.println("asdf".isBlank()); // f
System.out.println("\n".isBlank()); // t
}

替换字符串,根据字符替换

1
2
3
4
public static void main(String[] args) {  
System.out.println("hello".replace("ll", "o"));
System.out.println("hello".replace("o", "asdf"));
}

也可以使用正则表达式

1
2
3
public static void main(String[] args) {  
System.out.println("hello".replaceAll(正则表达式));
}

分割字符串

1
2
3
public static void main(String[] args) {  
System.out.println("Hello".split(正则表达式));
}

拼接字符串,join()

1
2
3
4
public static void main(String[] args) {  
String[] test = {"a", "b", "c"};
System.out.println(String.join(". ", test));
}

格式化字符串formatted()方法和format()方法

1
2
3
4
5
public static void main(String[] args) {  
String string = "Hi, My name is %s, I'm %d";
System.out.println(string.formatted("name", 12));
System.out.println(String.format("this is a number: %.2f", 43.212));
}

格式转换,可以使用valueOf()方法

1
2
3
public static void main(String[] args) {  
System.out.println(String.valueOf(123));
}

stringchar[]是可以相互转化的

1
2
3
4
5
6
7
8
9
public static void main(String[] args) {  
char[] test = "test".toCharArray();
String string = new String(test);

for (int i = 0; i < test.length; i++) {
System.out.println(test[i]);
}
System.out.println(string);
}

6.2. StringBuilder

可以看作容器,创建后内容可变
常用方法有

  1. length()返回字符串长度
  2. toString()将 StringBuilder 转换为 String
  3. append()添加数据,并返回
  4. reverse()反转字符串
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static void main(String[] args) {  
    StringBuilder stringBuilder = new StringBuilder("asdf");
    System.out.println(stringBuilder.length());
    System.out.println(stringBuilder.append("qwer"));
    System.out.println(stringBuilder);

    System.out.println(stringBuilder.reverse());

    System.out.println(stringBuilder.toString());
    }

6.3 StringJoiner

可以看作一个容器,创建后内容可变

  1. 直接在创建对象时填入参数(间隔符, 开始符号, 结束符号)
  2. add()添加数据,返回自身
    1
    2
    3
    4
    5
    6
    7
    8
    public static void main(String[] args) {  
    StringJoiner stringJoiner = new StringJoiner("**", "[", "]");
    stringJoiner.add("asdf");
    stringJoiner.add("qwer");
    stringJoiner.add("zxcv");

    System.out.println(stringJoiner);
    }
上一页
2024-07-13 00:32:24