栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java创建对象的步骤(java中对象的创建)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java创建对象的步骤(java中对象的创建)

9.对象的创建和使用 对象的创建
new 类名()
public class StudentTest
{
	public static void main(String[] args)
	{
		Student s1 = new Student(); // Student是引用数据类型,s1是变量名
		Student s2 = new Student();
		int i = 10;
	}
}

new Student() 执行之后是一个Student类型的对象

Java语言中凡是使用class关键字定义的类都属于引用数据类型,类名本身就是这种引用数据类型的类型名

Java语言中,实例变量如果没有手动赋值,在创建对象的时候,系统会对实例变量默认赋值

数据类型默认值
byte0
short0
int0
long0L
float0.0f
double0.0
booleanfalse
charu0000
引用类型null
对象创建和使用的深层次解密 Java虚拟机的内存管理

Java虚拟机有三块主要的内存空间,分别是虚拟机栈、方法区、堆区

方法区存储类的信息

栈中存储方法执行时的栈帧以及局部变量

堆区中主要存储new出来的对象,以及对象内部的实例变量

public class StudentTest
{
	public static void main(String[] args)
	{
		int i = 10;
		Student s1 = new Student();
	}
}

Student s1 = new Student()

s1不是对象,是一个引用,存的是对象的内存地址

Java只能通过引用访问堆内存中的对象,例如s1.no, s1.name

构造方法

构造方法是类中特殊的方法,通过调用构造方法来完成对象的创建,以及对象属性的初始化造作

[修饰符列表] 构造方法名(形式参数列表)
{
    构造方法体;
}

构造方法名和类名一致构造方法用来创建对象,以及完成属性初始化操作构造方法不需要写返回值类型,写了会报错构造方法的返回值类型实际上是当前类的类型一个类中可以定义多个构造方法,这些构造方法构成方法重载 构造方法的调用

new 构造方法名(实际参数列表);

Note:

当一个类没有显式地定义任何构造方法的时候,系统默认提供无参数构造方法,当显式地定义构造方法之后,系统则不再提供无参数构造方法。无参数构造方法又叫缺省构造器,或者默认构造方法。建议手动写上无参数构造方法。构造方法虽然在返回值类型方面不写任何类型,但它执行结束之后实际上会返回该对象在堆内存当中的内存地址,这时候可以定义变量接收内存地址,即引用通过引用就可以访问这个对象的内存了,例如time1.year, time1.month, time1.day

public class Date
{
	int year;
	int month;
	int day;
	
	public Date()
	{
		System.out.println("Date类无参数构造方法执行");
	}
	public Date(int year1)
	{
		System.out.println("带有参数year的构造方法");
	}
	public Date(int year1, int month1)
	{
		System.out.println("带有参数year, month的构造方法");
	}
	public Date(int year1, int month1, int day1)
	{
		System.out.println("带有参数year, month, day的构造方法");
	}
}
public class DateTest
{
	public static void main(String[] args)
	{
		System.out.println("main begin");
		Date time1 = new Date();
		System.out.println(time1);
		Date time2 = new Date(2008);
		System.out.println(time2);
		Date time3 = new Date(2008, 8);
		System.out.println(time3);
		Date time4 = new Date(2008, 8, 8);
		System.out.println(time4);
		System.out.println("main over");
	}
}

public class DateTest
{
	public static void main(String[] args)
	{
		System.out.println("main begin");
		Date time1 = new Date();
		System.out.println(time1.year + "年" + time1.month + "月" + time1.day + "日");
		Date time2 = new Date(2008);
		System.out.println(time2.year + "年" + time2.month + "月" + time2.day + "日");
		Date time3 = new Date(2008, 8);
		System.out.println(time3.year + "年" + time3.month + "月" + time3.day + "日");
		Date time4 = new Date(2008, 8, 8);
		System.out.println(time4.year + "年" + time4.month + "月" + time4.day + "日");
		System.out.println("main over");
	}
}

无参数构造方法中没有给属性赋值,所以使用系统默认值0

在编写构造方法的时候要注意变量名的问题

public Date(int year1, int month1, int day1)
{
	year = year1;
	month = month1;
	day = day1; // 这样写可以
}

public Date(int a, int b, int c)
{
	year = a;
	month = b;
	day = c; // 这样写可以
}

public Date(int year, int month, int day)
{
	year = year; // 这里的两个year都是局部变量,和属性year没有关系
	month = month; // 同上
	day = day; 
}
空指针异常
public class Ballon
{
	String color;
	String gas;
	public Ballon()
	{
		
	}
	
	public Ballon(String _color, String _gas)
	{
		color = _color;
		gas = _gas;
	}
}
public class BallonTest
{
	public static void main(String[] args)
	{
		Ballon ball = new Ballon("红色", "氢气");
		System.out.println("气球颜色是:" + ball.color);
		System.out.println("气球中的气体是:" + ball.gas);
		ball = null;
		
		System.out.println("气球颜色是:" + ball.color);
	}
}

当实例变量是一个引用
public class Date
{
	int year;
	int month;
	int day;
	
	public Date()
	{
		
	}
	
	public Date(int year1, int month1, int day1)
	{
		year = year1;
		month = month1;
		day = day1;
	}
}
public class Vip
{
	int id;
	String name;
	Date birth;
	public Vip()
	{
		
	}
	public Vip(int _id, String _name, Date _birth)
	{
		id = _id;
		name = _name;
		birth = _birth;
	}
}
public class VipTest
{
	public static void main(String[] args)
	{
		Date d = new Date(1983, 5, 6);
		Vip v = new Vip(123, "Jack", d);
		System.out.println("编号=" + v.id);
		System.out.println("姓名=" + v.name);
		System.out.println("生日=" + v.birth.year + "年" + v.birth.month + "月" + v.birth.day + "日");
	}
}

方法调用时参数的传递问题
int a = 10;
int b = a'

Bird bird1 = new Bird("polly");
Bird bird2 = bird1;
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773605.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号