Java对象的创建过程
构造方法的概述与基本格式
构造方法的作用是给对象中的成员进行初始化,它的构造格式特点是,方法名和类名相同,其中没有返回值甚至时void都没有,也没有具体的返回值;
构造方法的重载
构造方法的重载指的就是,如果我们在一个类中没有给出一个构造方法,系统将自动的为我们提供一个空参构造的方法,当然如果我们自己写入了构造方法,那么系统将不会有空参构造的方法,这个时候如过我们还想要使用空参构造,我们就需要在类中自己手动写入一个空参构造,例如我现在要写一个手机类:
package method;
public class phone {
static String brand;
static int price;
static String colour;
public phone(){ };
public phone(String brand, int price,String colour){
this.brand=brand;
this.price=price;
this.colour=colour;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public void show(){
System.out.println("我的品牌是"+brand)
}
}
在上类中,我们定义了三个成员变量,同时也写了一个空参构造方法public phone(),因为下面我们有写一个有参构造的方法public phone(String brand, int price,String colour),所以我们就需要在上面自己手动写入空参的构造方法,这样理解的话我们就知道了,一个完整且标准的类应该包含三个东西:成员变量、成员方法、构造方法,只有包含了三种的类才能够用来创建一个对象;我们来创建两个对象看一下:
public class huawei {
public static void main(String[] args) {
phone huawei = new phone();
huawei.setBrand("华为mate");
huawei.setPrice(12000);
huawei.setColour("red");
System.out.println(huawei.getBrand());
System.out.println(huawei.getPrice());
System.out.println(huawei.getColour());
huawei.show();;
phone huawei2 = new phone("荣耀", 2500, "red");
System.out.println(huawei2.getPrice());
System.out.println(huawei2.getBrand());
System.out.println(huawei2.getColour());
huawei2.show();
}
}
输出结果:
对象创建的具体过程
(1):加载.class文件进内存
(2):在栈内存为s(这里指得是对象起名字)开辟空间
(3):在堆内存为对象开辟空间
(4):对对象的成员变量进行默认初始化
(5):对对象的成员变量进行显示初始化
(6):通过构造方法对对象的成员变量赋值
(7):对象初始化完毕,把对象地址赋值给s变量
在这里我们还可以再来实现一个案例:
需求:
定义一个长方形(RectangleDemo)类,定义求周长(length)和面积(area)的方法,
然后定义一个测试类Test,进行测试。
代码:
类的定义
public class Rectangle {
//定义属性
private double width;
private double height;
//提供构造方法
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
//提供get set 方法
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//提供获取周长的方法
public double getPerimeter() {
return (width + height) * 2;
}
//提供获取面积的方法
public double getArea() {
return width * height;
}
}
测试test:
public class MyTest {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setHeight(100);
rectangle.setWidth(200);
double perimeter = rectangle.getPerimeter();
double area = rectangle.getArea();
System.out.println("面积是"+area+"==周长是"+perimeter);
System.out.println("-------------------");
Rectangle rectangle1 = new Rectangle(10.3, 190.6);
double perimeter1 = rectangle1.getPerimeter();
double area1 = rectangle1.getArea();
System.out.println("面积是" + area1 + "==周长是" + perimeter1);
}
}
输出结果:
static关键字的使用
static关键字的特点
- 随着类的加载而加载
- 优先于对象存在
- 被类的所有对象共享
举例:咱们班级的学生应该共用同一个班级编号。
其实这个特点也是在告诉我们什么时候使用静态?
如果某个成员变量是被所有对象共享的,那么它就应该定义为静态的。 - 可以通过类名调用
其实它本身也可以通过对象名调用。
推荐使用类名调用。
静态修饰的内容一般我们称其为:与类相关的,类成员
static关键字的注意事项
static的注意事项
- 在静态方法中是没有this关键字的
如何理解呢?
静态是随着类的加载而加载,this是随着对象的创建而存在。
静态比对象先存在。 - 静态方法只能访问静态的成员变量和静态的成员方法
简单记:静态只能访问静态,非静态可以访问静态的也可以访问非静态的
static修饰的静态变量和成员变量的区别
- 所属不同 静态变量属于类,所以也称为类变量 成员变量属于对象,所以也称为实例变量(对象变量)
- 内存中位置不同 静态变量存储于方法区的静态区 成员变量存储于堆内存
- 内存出现时间不同 静态变量随着类的加载而加载,随着类的消失而消失 成员变量随着对象的创建而存在,随着对象的消失而消失
- 调用不同 静态变量可以通过类名调用,也可以通过对象调用 成员变量只能通过对象名调用
案例随机数小游戏的实现
要求:由系统产生一个随机数在1—100之间,用户键盘输入一个值和系统随机数匹配
代码:
import java.util.Scanner;
class playgame {
public static void main(String[] args) {
int num = (int) ((Math.random()) * 100) + 1;
Scanner scan = new Scanner(System.in);
int guest = -1;
do {
System.out.println("请输入你想得到的数字(1--100之间的数):");
guest = scan.nextInt();
if (guest > num) {
System.out.println("你输入的数字太大了,在小点");
}
if (guest < num) {
System.out.println("你输入的数字太小了,在大点");
}
if (guest == num) {
System.out.println("恭喜你猜对了!");
}
} while (guest != num);
System.out.println("欢迎下次再来");
}
}
我们来看下输出结果:
ok,我们这样就实现了这个小游戏啦,再见!