我的代码java添加构造函数是否有任何错误? [英] Are there any mistakes in my code java adding constructors?

查看:79
本文介绍了我的代码java添加构造函数是否有任何错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向Person类添加2个构造函数。一个不带参数并将数据初始化为全0和(空字符串)。一个构造函数接受所有4个参数,每个属性一个参数,然后将属性设置为传入的这些参数。最后更改main以使用这些新的构造函数。您不再需要调用设置函数,但不要删除类中的设置函数。



Add 2 constructors to the Person class. One that takes no arguments and initializes the data to all 0’s and "" (empty strings). And one constructor that takes all 4 arguments, one argument for each property and then sets the properties to these arguments that are passed in. Lastly change the main to use these new Constructors. You will not need to call the set functions any more, but do not remove the set functions from your class.

Main Code -->




Person p1;
p1 = new Person("Rodney","Duncan","70 Bowman St. South Windsor, CT 06074","rduncan@gmail.com");
p1.display();





如果我的代码中有任何错误,你可以给我一些如何解决它们的建议



这里是人类的原始代码:



If there any mistakes in my code can you give me suggestions of how to fix them

Here my original code for Person Class:

public class Person {
	
	//   ========================== Properties ===========================
	private String FirstName;
	private String LastName;
	private String Address;
	private String Email;
	
	//   ==========================  Behaviors  ==========================
	public void setFirstName(String fn) { FirstName = fn; }
	public String getFirstName() { return FirstName;}
	
	public void setLastName(String ln) { LastName = ln; }
	public String getLastName() { return LastName;}
	
	public void setAddress(String a) { Address = a; }
	public String getAddress() { return Address;}
	
	public void setEmail(String e) { Email = e; }
	public String getEmail() { return Email;}
	
 	//Returning String
	public String toString() {
		return FirstName + ":" + LastName + ":" + Address + ":" + Email;
	}
	
	public void display() {
		System.out.println("First Name            = " + getFirstName());
		System.out.println("Last Name     = " + getLastName());
		System.out.println("Address      = " + getAddress());
		System.out.println("Email          = " + getEmail());
		
	} //end display()
	
	
	
	
	
	public static void main(String args []) {
		
		Person p1;
		p1 = new Person();
		
		p1.setFirstName("Rodney");
		p1.setLastName("Duncan");
		p1.setAddress("70 Bowman St. South Windsor, CT 06074");
		p1.setEmail("rduncan@gmail.com");
		
		p1.display();
		
		
		//Test out toString() method
		System.out.println(p1);
	} //end main
	
} //end class





我尝试过:



这里是人类的修改版本:



What I have tried:

Here the modify version of Person Class:

public class Person {
	
	//   ========================== Properties ===========================
	private String FirstName;
	private String LastName;
	private String Address;
	private String Email;
	
	//constructor with parameters
	Person(String FirstName, String LastName, String Address, String Email) {
		this.FirstName = FirstName;
		this.LastName = LastName;
		this.Address = Address;
		this.Email = Email;
	}
	
	//constructor with no parameters
	Person() {
		this.FirstName = "";
		this.LastName = "";
		this.Address = "";
		this.Email = "";
	}
	
	//   ==========================  Behaviors  ==========================
	public void setFirstName(String FirstName) {
		this.FirstName = FirstName;
	}
	
	public void setLastName(String LastName) {
		this.LastName = LastName;
	}
	
	public void setAddress(String Address) {
		this.Address = Address;
	}
	
	public void setEmail(String Email) {
		this.Email = Email;
	}
	
	
	
	public String getFirstName() {
		return this.FirstName;
	}
	
	public String getLastName() {
		return this.LastName;
	}
	
	public String getAddress() {
		return this.Address;
	}
	
	public String getEmail() {
		return this.Email;
	}
	
	//method that displays person data
	void display() {
		System.out.println("First Name : "+this.FirstName);
		System.out.println("Last Name : "+this.LastName);
		System.out.println("Address : "+this.Address);
		System.out.println("Email : "+this.Email);
	}
	
	//overriding toString method
	public String toString() {
		return "FirstName: "+getFirstName() + "\nLastName: "+getLastName() + "\n" +this.Address.toString()
		+"\nEmail: "+getEmail();
	}
	
	//main method
	public static void main(String args []) {
		
		Person p1;
		p1 = new Person("Rodney","Duncan","70 Bowman St. South Windsor, CT 06074","rduncan@gmail.com");
		p1.display();
	}
	}

推荐答案

根据您的问题,我只知道您有2个构造函数正在使用。



1)默认构造函数&

2)4参数化



但是我无法得到实际问题是什么?







您输出的上述代码为:< br $>


名字:Rodney

姓:Duncan

地址:70 Bowman St. South Windsor,CT 06074

电子邮件:rduncan@gmail.com
As per your question I just understand that you have 2 constructors are using.

1) Default Constructor &
2) 4 Parameterized

But I can not get what actual your problem is what?



Your Output of above coding is :

First Name : Rodney
Last Name : Duncan
Address : 70 Bowman St. South Windsor, CT 06074
Email : rduncan@gmail.com


这篇关于我的代码java添加构造函数是否有任何错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆