在java中分配引用变量(示例)
分为两类: 人在java中分配引用变量(示例),java,variables,reference,Java,Variables,Reference,分为两类: 人 /** * 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; /** * Co
/**
* 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;
}
}
雇员是人,所以每个雇员对象也是人对象。因此,可以将员工参考变量分配给人员参考变量
人员=新员工(“乔·史密斯”,“主大街100号”,3000.0)
但它也可以分配给员工参考变量吗
员工=新员工(“乔·史密斯”,“主大街100号”,3000.0)
如果是,这两者之间的区别是什么。我想掌握引用和分配变量的概念,因此我非常感谢您的澄清
(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) 创建员工,然后将其放入person对象中。您将丢失employee字段/方法,但可以通过如下方式进行强制转换获得它们:employee employeeFromPerson=(employee)person
。在不强制转换的情况下,只能引用person方法
(2) 基本上创建一个employee并将其放入employee对象中。您可以使用此对象引用Person和Employee方法/字段
两者都是正确的。
(1) 创建员工,然后将其放入person对象中。您将丢失employee字段/方法,但可以通过如下方式进行强制转换获得它们:employee employeeFromPerson=(employee)person
。在不强制转换的情况下,只能引用person方法
(2) 基本上创建一个employee并将其放入employee对象中。您可以使用此对象引用Person和Employee方法/字段。这真的不重要。你可以用任何一个。如果您有类
A
和子类A
B
,并且您只想使用A
的功能,那么使用A A=new B()代码>
但是,如果使用Person
的数组,情况就不同了。如果将Person
作为超级类,将Manager
和Employee
作为子类,则可以将任何类型的人放入Person[]
中。但是,如果创建数组Manager[]
,则不能将员工
放在其中。这真的不重要。你可以用任何一个。如果您有类A
和子类A
B
,并且您只想使用A
的功能,那么使用A A=new B()代码>
但是,如果使用Person
的数组,情况就不同了。如果将Person
作为超级类,将Manager
和Employee
作为子类,则可以将任何类型的人放入Person[]
中。但是,如果创建数组Manager[]
,则不能将员工
放在其中。两者都是正确的,都有员工对象的引用,但都在不同的场景中使用
第一个用于动态多态性,第二个用于简单的对象分配
比如说
让我们假设这样一个场景,其中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都引用了子类对象,它将调用子类方法的定义而不是它自己的方法的定义两者都是正确的,都引用了employee对象,但都在不同的场景中使用
第一个用于动态多态性,第二个用于简单的对象分配
比如说
让我们假设这样一个场景,其中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具有子类对象的引用,它将调用子类方法的定义,而不是它自己方法的定义