类的成员:字段、方法、代码块、内部类和接口等

字段

1
2
3
4
class Person{
public int age;
public String name;
public String sex;

age,name,sex这些类中方法外的变量,是字段、属性、成员变量(3种称呼均可)

1
2
3
4
5
6
7
8
9
public class TestDemo {
public static void main(String[] args) {
//类的实例化
Person person=new Person();
System.out.println(person.name);//null
System.out.println(person.age);//0

}
}

对象的字段如果没有初始化,访问时会被设置成默认值。

默认值规则

  • 对于各种数字类型, 默认值为 0.

  • 对于 boolean 类型,默认值为 false.

  • 对于引用类型(String, Array, 以及自定制类), 默认值为 null

1
2
Exception in thread "main" java.lang.NullPointerException
at TestDemo.main(TestDemo.java:15)

对null进行访问,会出现空指针异常。

方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Person {
//字段、成员变量、属性
public int age;
public String name;
public String sex;
//方法
//构造方法
public Person(String name) {
this.name = name;
}

public void study() {
System.out.println(this.name+"正在学习");
}

}
public class TestDemo {
public static void main(String[] args) {
//类的实例化
Person person=new Person("panghutx");
person.study();

}
}

代码中的study()就是一个方法,和之前学到的方法(c语言为函数没什么区别)

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

上段代码是一个构造方法

构造方法是一种特殊方法, 使用关键字new实例化新对象时会被自动调用, 用于完成初始化操作。局部变量必须初始化,否则会编译失败,而成员变量无需初始化,原因在于构造方法为各成员进行了初始化。

语法规则:方法名与类名一致,没有返回值,支持重载

1
2
3
4
5
6
7
8
9
10
11
//不带参数的构造方法
public Person(){
System.out.println("I am the constructor method of zero Parameter ");
}
//带有3个参数的构造方法
public Person(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
System.out.println("I am the constructor method of three Parameters ");
}
1
2
3
//类的实例化
Person person1=new Person();
Person person2=new Person("panghutx",12,"male");

输出结果为

1
2
3
I am the constructor method of zero Parameter  

I am the constructor method of three Parameters

代码块

使用{}定义的一段代码.分为本地代码块、实例代码块和静态代码块,定义在方法中的代码块是本地代码块,定义在类中的代码块是实例代码块(也叫构造代码块),由static修饰的是静态代码块。

构造代码块

构造代码块(实例代码块):定义在类中的代码块(不加修饰符)。构造代码块一般用于初始化实例成员变量。

实例代码块优先于构造函数执行。

静态代码块

使用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
class Person{

private String name;//实例成员变量
private int age;
private String sex;
private static int count = 0;//静态成员变量 由类共享数据 方法区

public Person(){
System.out.println("I am Person init()!");
}

//实例代码块
{
this.name = "bit";
this.age = 12;
this.sex = "man";
System.out.println("I am instance init()!");
}

//静态代码块
static {
count = 10;//只能访问静态数据成员
System.out.println("I am static init()!");
}

public void show(){
System.out.println("name: "+name+" age: "+age+" sex: "+sex);
}

}
public class Main {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();//静态代码块是否还会被执行?
}
}

执行结果:

1
2
3
4
5
I am static init()!
I am instance init()!
I am Person init()!
I am instance init()!
I am Person init()!

static关键字

static修饰属性、方法、类、代码块

static修饰属性

static修饰的属性不与对象绑定,所有类共享,只有一份,存在于方法区。

访问方法:类名.static属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class TestDemo{
public int a;
public static int count;
}
public class Main{

public static void main(String[] args) {
TestDemo t1 = new TestDemo();
t1.a++;
TestDemo.count++;
System.out.println(t1.a);
System.out.println(TestDemo.count);
System.out.println("============");
TestDemo t2 = new TestDemo();
t2.a++;
TestDemo.count++;
System.out.println(t2.a);
System.out.println(TestDemo.count);
}
}

输出结果:

1
2
3
4
5
1
1
============
1
2

static修饰方法

static修饰的方法无需创建对象,可以由类直接调用,调用方法:类名.方法。静态方法只能访问静态变量,不能访问非静态变量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class TestDemo{
public int a;
public static int count;

public static void change() {
count = 100;
//a = 10; error 不可以访问非静态数据成员
}
}
public class Main{
public static void main(String[] args) {
TestDemo.change();//无需创建实例对象 就可以调用
System.out.println(TestDemo.count);
}
}

注意:thissuper两个关键字不能在静态上下文中使用(this 是当前实例的引用, super是当前实例父类实例的引用, 也是和当前实例相关).

this关键字

this关键字代表当前对象的引用,不是当前对象。

一个对象的产生分为两步:

①为对象分配内存②调用合适的构造方法

对象的产生需要调用合适的构造方法,在构造方法内部可以使用this,对象还没有产生我们就已经使用了this,恰恰说明this不代表当前对象。

this的用法:

this():调用构造方法,该语句必须是构造方法中的第一条语句,否则会编译失败。

this.data:调用当前对象的属性

this.func():调用当前对象的方法