JAVA 中构造方法的使用

一般构造方法是完成对对象的初始化,每当类实例化一个对象时候,都会自动调用构造方法

我们要怎么使用构造方法呢

语法的形式是

public   类名([参数列表])

//构造方法体

开始来使用构造函数了

一、如果在类中定义的构造方法都不是无参的构造方法,则编译器不会为类设置一个默认的无参构造方法,当试图调用无参构造方法实例化一个对象时候,编译器就会报错,什么意思呢

我们先来试验一下

定义一个student类,代码如下

public class Student
{
	public String name;
	public String sex;
	public int age;
	public void PrintView()
	{
		System.out.println("我是:"+name);
		System.out.println("我是:"+sex+"生");
		System.out.println("我"+age+"岁了");
	}
	public static void main(String[] args)
	{
		Student student= new Student();
		student. PrintView();
	}
}	
运行后得到

这时候系统默认的就是构造了一个无参的构造方法,里面的neme=null ,sex=null ,age=0;

但是我们如果构造了一个方法

代码如下:

public class Student
{
	public String name;
	public String sex;
	public int age;
	public Student(String name)
	{
		this.name=name;
	}
	public void PrintView()
	{
		System.out.println("我是:"+name);
		System.out.println("我是:"+sex+"生");
		System.out.println("我"+age+"岁了");
	}
	public static void main(String[] args)
	{
		Student student= new Student();
		student. PrintView();
	}
}
则运行的结果是


所以理解就是,如果构造了方法,调用无参构造方法实例化一个对象时,就会报错,我们怎么解决这个问题呢

一般的方法就是自己构造一个无参的方法添加进去

public Student()
	{
		name="未登记";
		sex="未登记";
		age=0;
	}


这个一个无参的构造函数,系统就不会报错了

二、理解上面的意思后,我们就可以来写有参的方法了

假设我们登记了一个学生信息,而且暂且就知道他的名字叫张三,这个时候我们只知道他叫张三

public static void main(String[] args)
	{
		Student student1= new Student();
Student student2= new Student("张三");
		student1. PrintView();
student2. PrintView();
}


 

这个时候我们也要构造一个有一个参数的方法,整体代码如下

public class Student
{
	public String name;
	public String sex;
	public int age;
	public Student()
	{
		name="未登记";
		sex="未登记";
		age=0;
	}
	public Student(String name)
	{
		this.name=name;
	}
	public void PrintView()
	{
		System.out.println("我是:"+name);
		System.out.println("我是:"+sex+"生");
		System.out.println("我"+age+"岁了");
		System.out.println("...............");
	}
	public static void main(String[] args)
	{
		Student student1= new Student();
		Student student2= new Student("张三");
		student1. PrintView();
		student2. PrintView();
	}
}


这个时候

student2. PrintView();调用的就是public Student(String name)这个构造函数,里面是一个参数的!

同理当有两个参数(传进来两个信息),我们要构造一个含有两个参数的方法

当有三个参数的时候,我们要构造一个含有三个参数的的方法

代码如下:

public class Student
{
	public String name;
	public String sex;
	public int age;
	public Student()
	{
		name="未登记";
		sex="未登记";
		age=0;
	}
	public Student(String name)
	{
		this.name=name;
	}
	public Student(String name,String sex,int age)
	{
		this.name=name;
		this.sex=sex;
		this.age=age;
	}
	public void PrintView()
	{
		System.out.println("我是:"+name);
		System.out.println("我是:"+sex+"生");
		System.out.println("我"+age+"岁了");
		System.out.println("...............");
	}
	public static void main(String[] args)
	{
		Student student1= new Student();
		Student student2= new Student("张三");
		Student student3= new Student("甲同学","男",16);
		student1. PrintView();
		student2. PrintView();
		student3. PrintView();
	}
}	

结果如下




以上大概就是构造方法使用的方法了,具体是为对象的初始化所准备的,相信聪明的你一定学会了


“限本人知识水平有限,有讲的不对的地方指出,勿喷!”




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值