问题
I want to change the default constructor (person1) from public to private and then ask the user to input the information instead of the given values.
I have two different classes - Person; which is encapsulated and then PersonTest which tests the information.
class Person {
// Data Members
private String name; // The name of this person
private int age; // The age of this person
private char gender; // The gender of this person
// Default constructor
public Person() {
name = "Not Given";
age = 0;
gender = 'U';
}
// Constructs a new Person with passed name, age, and gender parameters.
public Person(String personName, int personAge, char personGender) {
name = personName;
age = personAge;
gender = personGender;
}
// Returns the age of this person.
public int getAge( ) {
return age;
}
// Returns the gender of this person.
public char getGender( ) {
return gender;
}
// Returns the name of this person.
public String getName( ) {
return name;
}
// Sets the age of this person.
public void setAge( int personAge ) {
age = personAge;
}
// Sets the gender of this person.
public void setGender( char personGender ) {
gender = personGender;
}
// Sets the name of this person.
public void setName( String personName ) {
name = personName;
}
}
import java.util.Scanner;
public class PersonTest {
// Main method
public static void main(String[] args){
// Create first instance of Person class to test the default constructor.
Person person1 = new Person();
// Create a new instance of the Person class.
Person person2 = new Person();
Scanner input = new Scanner(System.in);
// Get the values from user for first instance of Person class.
System.out.println("Person 2 Name: ");
person2.setName(input.nextLine());
System.out.println("Person 2 Age: ");
person2.setAge(input.nextInt());
System.out.println("Person 2 Gender: ");
person2.setGender(input.next().charAt(0));
// Print out the information.
System.out.println("Person 1 Name = " + person1.getName());
System.out.println("Person 1 Age = " + person1.getAge());
System.out.println("Person 1 Gender = " + person1.getGender());
System.out.println("");
System.out.println("Person 2 Name = " + person2.getName());
System.out.println("Person 2 Age = " + person2.getAge());
System.out.println("Person 2 Gender = " + person2.getGender());
System.out.println("");
}
}
回答1:
One of the main purposes of constructor to be accessible from "outside", so that external classes can instantiate and use your class. You should not make constructor private. It will not be accessible, so main purposes of it will be violated!
回答2:
If you do not want a default-constructor, simply do not specify one. As soon as you write at least one constructor with paramters, Java does not provide a default-constructor unless you wirte one.
Example:
public class AClass
{
int value = 0;
}
public class BClass
{
int value;
public BClass()
{
value = 0;
}
public BClass(int value)
{
this.value = value;
}
}
This classes have default-constructors and you can write
AClass a = new AClass();
BClass b = new BClass();
to get new instances of this classes.
public class CClass
{
int value;
public CClass(int value)
{
this.value = value;
}
}
This class does not have a default-constructor, since at least one other constructor is specified and no default-constructor is written. Thus
CClass c = new CClass();
will result in a syntax-error.
来源:https://stackoverflow.com/questions/26762641/changing-public-data-to-private-data-in-java