构造方法,this关键字,static关键字,封装,静态变量

时间:2023-03-09 07:28:52
构造方法,this关键字,static关键字,封装,静态变量

1.构造方法

构造方法是一种特殊的方法,是专门用于创建/实例化对象的方法。

构造方法根据是否有参数分为两类:1.无参构造方法  2.有参构造方法

1.1无参构造方法

无参构造方法就是构造方法中没有参数。构造方法在创建对象(new Dog())时使用.无参构造方法一般用于给属性赋默认值。如果开发中没有定义无参构造方法,jvm默认给类分配一个无参构造方法。格式如下

[修饰符] 类名(){

}

1.2有参构造方法

当构造/实例化一个对象的时候,可以向构造方法中传递参数,这样的构造方法叫做有参构造方法。有参构造方法格式如下

[修饰符] 类名(type arg1,type arg2,...){
//初始化代码
}

有参构造和无参构造是方法重载的关系

2.1局部变量和成员变量的优先级

在一个代码块(作用域)中,局部变量和成员变量同名,局部变量的优先级高于成员变量。

2.2有参构造常见问题

在一个类中,当定义了有参构造的时候,jvm不再默认分配无参构造。但是在一些情况下我们仍然需要用到无参构造方法,如反射,只能用无参构造,不能用有参构造。所以,一般情况下,我们在定义有参构造的时候,同时也会定义无参构造。

3this关键字(上)

this关键字一般指向对象本身。this关键字用于访问成员属性,可解决局部变量和成员变量同名时优先级的问题。如下

 public Dog2(String name,int health,int love,String strain){
System.out.println("this:"+this);
this.name = name;
this.health = health;
this.love = love;
this.strain = strain;
}
 public class Test04{
public static void main(String[] args){ Dog2 dog = new Dog2("二狗",100,0,"土狗");
System.out.println("dog:"+dog);
dog.showInfo();
}
}

通过打印this的地址,和dog类的地址。可以发现他们都指向同一个地址。由此可知,this指向对象本身。

4static关键字

4.1静态变量

static关键字,表示静态的,可以修饰变量,也可以修饰方法。

static修饰的变量称为静态变量,形式如下:

static type 变量名 [=初始值]

静态变量,也叫类变量,归类所有。存放在方法区(共享区)中的静态区中,可以被类的对象共享访问。

静态变量的访问方式:

类名.静态变量(推荐)

对象名.静态变量

通过以下需求加深理解:

需求:统计汽车工厂成产了多少台汽车?

 public class Car{
String brand;
String type;
float price; static int count = 0; public Car(){
Car.count++;
} public Car(String brand,String type,float price){
this.brand = brand;
this.type = type;
this.price = price;
Car.count++;
} public void showInfo(){
System.out.println("车辆信息:");
System.out.println("品牌:"+this.brand);
System.out.println("型号:"+this.type);
System.out.println("价格:"+this.price);
System.out.println("我是第"+Car.count+"辆车");
} }
 public class Test01{
public static void main(String[] args){
Car car1 = new Car("奔驰","漏油GL300",66);
car1.showInfo(); Car car2 = new Car("奔驰","漏油GL400",66);
car2.showInfo(); System.out.println(Car.count);
System.out.println(car1.count);
System.out.println(car2.count); }
}

类中包含静态成员(静态属性,静态方法)和实例成员(实例变量,实例方法)。

4.2静态方法

static修饰的方法称为静态方法,形式如下:

[修饰符] static 返回值类型 方法名(arg..){

}

静态方法归类所有,调用方式:

类名.方法名()(推荐)

对象名.方法名()

需注意的是:实例方法可以访问静态成员。

      静态方法不能访问非静态成员。

5封装

封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问。

封装的步骤:[1]属性私有化

      [2]提供公共的设置器和访问器

      [3]在设置器和访问器中添加业务校验逻辑

例子如下:

 public class Dog{

     // 【1】private 私有的,对外不可见
private String name;
private int health;
private int love;
private String strain; // 【2】提供公共的设置器(setter)和访问器(getter)
public void setName(String name){
// 【3】逻辑校验
if(name.equals("")){
System.out.println("姓名不能为空.");
}else{
this.name = name;
}
}
public String getName(){
return this.name;
} public void setHealth(int health){
if(health < 0){
System.out.println("健康值不合法.");
this.health = 0;
}else{
this.health = health;
}
}
public int getHealth(){
return this.health;
} public void setLove(int love){
if(love < 0){
System.out.println("亲密度不合法.");
this.love = 0;
}else{
this.love = love;
}
}
public int getLove(){
return this.love;
} public void setStrain(String strain){
if(strain.equals("")){
System.out.println("品种不能为空.");
}else{
this.strain = strain;
}
}
public String getStrain(){
return this.strain;
} public Dog(){ } public Dog(String name,int health,int love,String strain){
this.setName(name);
this.setHealth(health);
this.setLove(love);
this.setStrain(strain);
} public void showInfo(){
System.out.print("我的名字叫"+this.name);
System.out.print(",健康值"+this.health);
System.out.print(",亲密度"+this.love);
System.out.println(",我是一只"+this.strain);
}
}

6.this关键字(下)

this表示对象本身。

[1]this调用属性

[2]this调用方法

[3]this调用本类的构造方法。形式如下:

 this(arg1,arg2,...)

通过下面练习加深理解

 public Dog(){

     }

     public Dog(String name,int health,int love){
this.setName(name);
this.setHealth(health);
this.setLove(love);
} public Dog(String name,int health,int love,String strain){
//this.setName(name);
//this.setHealth(health);
//this.setLove(love); // this调用本类的其他构造方法
// System.out.println("test");
this(name,health,love);
this.setStrain(strain); // showInfo();
//this.showInfo();
}

构造方法,this关键字,static关键字,封装,静态变量

注意:this调用其它构造方法时,只能放在构造方法的第一行

7.静态常量

在程序运行过程中,如果有一个量的值不发生改变,可以把该量声明成一个静态常量,用static final修饰。