在Java中将公共数据更改为私有数据 [英] Changing public data to private data in Java
问题描述
我想将默认构造函数(person1)从public更改为private,然后要求用户输入信息而不是给定值。
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("");
}
}
推荐答案
如果您不想要默认构造函数,只需不指定一个即可。一旦您至少编写了一个带有参数的构造函数,除非您愿意使用Java,否则Java不会提供默认构造函数。
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.
示例:
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();
获取此类的新实例。
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();
将导致语法错误。
这篇关于在Java中将公共数据更改为私有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!