有两个班级:
人
/**
* This class models a person.
*
* @author author name
* @version 1.0.0
*/
public class Person {
/* Name of the person */
private String name;
/* Address of the person */
private String address;
/**
* Constructs a <code>Person</code> object.
*
* @param initialName the name of the person.
* @param initialAddress the address of the person.
*/
public Person (String initialName, String initialAddress) {
name = initialName;
address = initialAddress;
}
/**
* Returns the name of this person.
*
* @return the name of this person.
*/
public String getName() {
return this.name;
}
/**
* Returns the address of this person.
*
* @return the address of this person.
*/
public String getAddress() {
return this.address;
}
}
和员工
/**
* This class models an Employee.
*
* @author author name
* @version 1.0.0
*/
public class Employee extends Person {
/* Salary of the employee */
private double salary;
/**
* Constructs an <code>Employee</code> object.
*
* @param initialName the name of the employee.
* @param initialAddress the address of the employee.
* @param initialSalary the salary of the employee.
*/
public Employee (String initialName, String initialAddress,
double initialSalary) {
super(initialName, initialAddress);
salary = initialSalary;
}
/**
* Returns the salary of this employee.
*
* @return the salary of this employee.
*/
public double getSalary() {
return this.salary;
}
/**
* Modifies the salary of this employee.
*
* @param newSalary the new salary.
*/
public void setSalary(double newSalary) {
salary = newSalary;
}
}
员工是-一个人,因此每个Employee对象也是一个Person对象。因此,可以将Employee参考变量分配给Person参考变量。
人员=新雇员(“乔·史密斯”,“大街100号”,3000.0);
但是也可以将其分配给Employee参考变量吗?
Employee employee = new Employee(“ Joe Smith”,“ 100 Main Ave”,3000.0);
如果是,那么两者之间有什么区别。我想掌握引用和分配变量的想法,因此我非常感谢您进行澄清。
答案 0 :(得分:2)
(1) Person person = new Employee("Joe Smith", "100 Main Ave", 3000.0);
(2) Employee employee = new Employee("Joe Smith", "100 Main Ave", 3000.0);
两个都是正确的。
(1)创建一个Employee,然后将其放入一个person对象。您会丢失员工字段/方法,但是可以像这样Employee employeeFromPerson=(Employee)person
进行强制转换。您只能引用person方法而不进行强制转换。
(2)基本上创建了一个雇员并将其放入一个雇员对象。您可以使用此对象引用Person和Employee方法/字段。
答案 1 :(得分:1)
两者都是正确的,并且都引用了雇员对象,但是都在不同的场景中使用。
第一个用于动态多态性,第二个用于简单的对象分配。
例如 让我们假设这种情况下,Employee和Student这两个类扩展了Person类,并且都覆盖了相同的方法。区别在于在运行时调用哪种方法。
class Person {
public String getTag(){
return "This is Person"
}
}
class Employee extends Person {
public String getTag(){
return "This is Employee"
}
}
class Student extends Person {
public String getTag(){
return "This is Student"
}
}
Person person1 = new Person();
Person person2 = new Employee();
Person person3 = new Student();
Employee employee = new Employee();
Student student = new Student();
person1.getTag(); //it will return "This is Person"
person2.getTag(); //it will return "This is Employee"
person3.getTag(); //it will return "This is Student"
employee.getTag(); //it will return "This is Employee"
student.getTag(); //it will return "This is Student"
请注意person2和person3引用了子类对象,它将调用子类方法的定义而不是其自身方法的定义
答案 2 :(得分:0)
真的没关系。您可以使用任何一个。如果您拥有A
类和A
B
的子类,并且只想使用A
的功能,则选择A a = new B();
。
但是,如果您使用Person
的数组,那就是另一回事了。如果您将Person
作为超类,并将Manager
和Employee
作为子类,则可以将任何类型的人放入Person[]
中。但是,如果创建数组Manager[]
,则不能在其中放置Employee
。