4.7 构造方法
-
new关键字做的事情:
- 在堆中为当前对象开辟一块内存空间 并且同时类的成员变量跟随对象进入到堆内存并附默认值
- 调用构造器为对象初始化信息
- 将地址返回给引用
-
构造方法
构造方法是一种特殊的方法
-
作用: 为对象初始化信息 为对象的属性赋值 不是用来创建对象的 new才能够创建对象
-
调用: 只能跟随new一起使用
-
注意:
- 构造器可以实现重载
- 如果没有定义构造方法 系统将给出一个默认的无参数构造方法
- 如果定义了构造方法 系统将不再提供默认的构造方法
- 如果自定义了带参构造方法 还要使用无参数构造方法 就必须再写一个无参数构造方法
- 无论是否使用 都手工书写无参数构造方法
-
定义:
修饰符 类名(参数){ 初始化信息代码; }
-
范例:
package user;
public class Car {
public String brand;
public String color;
public Car(){ //无参构造方法
}
public Car(String carName,String carColor){ //有参构造方法
//new中提供的值直接赋值
brand=carName;
color=carColor;
}
public void show(){
System.out.println("一个品牌为"+brand+"颜色为"+color+"的车正在跑");
}
}
package user;
public class CarDemo {
public static void main(String[] args) {
Car sc=new Car("保时捷","金黄色"); //给参数
sc.show();
}
}
4.8 this关键字
- this指向当前new的对象
作用:
- 在构造器的首行通过this(实参)调用本类中其他构造器
**注:**构造器不能相互调用
2)区分同名成员与局部变量问题
- 默认会发生就近原则
- 可以通过this 调用成员变量 否则默认找局部变量
- 如果不存在局部与成员同名问题 指代成员的this.可以省略
注意:
- this在构造器中使用 默认指代当前new的对象
- 在成员方法中的this 默认指代调用成员方法的对象
- this不能使用在static方法中
public class Class001_This {
public static void main(String[] args) {
Person p = new Person("张三");
System.out.println("p--->地址--->"+p);
p.show();
}
}
class Person{
//属性
public String name;
public int age;
public boolean gender;
//构造器
public Person(){
System.out.println("-----空造器.....");
}
//带参构造
public Person(String name){
System.out.println("-----1个参数构造器.....");
this.name = name;
}
public Person(String name,int age){
System.out.println("-----2个参数构造器.....");
this.name = name;
this.age = age;
}
public Person(String name,int age,boolean gender){
this(name,age);
this.gender = gender;
System.out.println("-----3个参数构造器.....");
}
//功能
public void show(){
System.out.println("this------->"+this);
String name = "str";
System.out.println(this.name+"--->"+age+"--->"+this.gender);
}
}
4.9 static关键字
- 静态的使用注意 :
- 成员方法中的this默认指代当前调用成员方法的对象
- 成员方法中可以直接使用成员 也可以直接使用静态
- 静态方法中可以直接使用静态内容 但是需要通过对象使用成员变量
- 在静态方法中不能使用this 因为冲突
public class Class002_Static {
static int i = 20;
int j = 100;
public static void main(String[] args) {
testStatic();
Class002_Static cs = new Class002_Static();
cs.test();
}
//成员方法
public void test(){
System.out.println("成员方法");
//直接使用
System.out.println(i);
System.out.println(j);
}
//静态方法
public static void testStatic(){
//直接使用
System.out.println(i);
//通过对象使用
System.out.println(new Class002_Static().j);
}
}
4.10 block关键字
块 { }:
分类 位置 执行时机
分类 | 位置 | 执行时机 |
---|---|---|
局部代码块 | 普通语句块 | 定义在方法或者语句块中 | 跟随方法 |
构造块 | 成员代码块 | 定义在类中方法外 | 跟随new |
静态块 | 定义在类中方法外 | 类第一次加载 |
注意 :
- 构造块中的代码先于构造器代码之前执行 因为在编译期间构造块代码会被编译到要执行的构造器内部 构造器代码之前
- 如果存在多个构造块 从上到下依次执行
- 静态块中的内容在类第一次加载完成之后就执行 并且只执行一次 先与main方法执行
- 如果存在多个静态块 从上到下依次执行
执行顺序 :
静态块 --> main —> 构造块 --> 构造器
public class Class001_Block {
static int i = 10;
int j;
//静态块
static{
System.out.println("我是静态块1");
}
static{
System.out.println("我是静态块2");
}
//构造器
public Class001_Block(){
System.out.println("构造器...");
}
public Class001_Block(int j){
System.out.println("构造器...");
}
//构造块
{
j = 10;
System.out.println("构造块1");
}
{
System.out.println("构造块2");
}
public static void main(String[] args) {
i = 1;
//局部代码块|普通语句块
{
System.out.println("局部代码块");
int i = 5;
System.out.println(i); //5
}
System.out.println(i); //1
Class001_Block cb = new Class001_Block();
Class001_Block cb2 = new Class001_Block();
}
}