多态性赋值语句
我对其赋值语句的多态性有疑问,例如这是超类
public class Person {
private String firstName;
private String middleInitial;
private String lastName;
private int age;
public Person(String firstName,String middleInitial , String lastName , int age){
setFirstName(firstName);
setMiddleInitial(middleInitial);
setLastName(lastName);
setAge(age);
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleInitial(String middleInitial){
this.middleInitial = middleInitial;
}
public String getMiddleInitial(){
return middleInitial;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public String toString(){
return String.format("First Name: "+getFirstName()+"\nMiddle Initial: "+getMiddleInitial()+
"\nLast Name: "+getLastName()+"\nAge: "+getAge());
}
}
这是派生类
public class Employee extends Person{
private Contact contact;
public Employee(String firstName,String middleInitial , String lastName , int age, Contact contact){
super(firstName,middleInitial,lastName,age);
this.contact = contact;
}
public String toString(){
return String.format(super.toString()+contact.toString());
}
}
现在我的问题是这些赋值语句之间有什么区别?和有什么区别?我知道 Employee 是一个人,但我想知道这两者之间有什么区别:
Person employee1 = new Employee();
Employee employee2 = new Employee();
这
Employee employeeKyle = new Employee();
Person employeeKyel2 = employeeKyle;
我对这些有点困难。
I have question regarding polymorphism about its assignment statement, for Example this is The Super class
public class Person {
private String firstName;
private String middleInitial;
private String lastName;
private int age;
public Person(String firstName,String middleInitial , String lastName , int age){
setFirstName(firstName);
setMiddleInitial(middleInitial);
setLastName(lastName);
setAge(age);
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleInitial(String middleInitial){
this.middleInitial = middleInitial;
}
public String getMiddleInitial(){
return middleInitial;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public String toString(){
return String.format("First Name: "+getFirstName()+"\nMiddle Initial: "+getMiddleInitial()+
"\nLast Name: "+getLastName()+"\nAge: "+getAge());
}
}
And this is The Derived Class
public class Employee extends Person{
private Contact contact;
public Employee(String firstName,String middleInitial , String lastName , int age, Contact contact){
super(firstName,middleInitial,lastName,age);
this.contact = contact;
}
public String toString(){
return String.format(super.toString()+contact.toString());
}
}
Now my question is what's the Difference Between these assignment statements ?? and what are the difference ? I know Employee is a Person but I want to know what's the difference between these two:
Person employee1 = new Employee();
Employee employee2 = new Employee();
and This
Employee employeeKyle = new Employee();
Person employeeKyel2 = employeeKyle;
I am kinda having a hard time about these .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在第一个对象中,您有 2 个对象,两者都是 Employee 类型,并且对它们有 2 个不同的引用,一个是 Person 引用,另一个是 Employee 引用。
在第二种情况下,您有 1 个对象和 2 个指向它的引用,其中一个是 Person,另一个是 Employee。
该对象始终是您使用 new 关键字实例化的类型(在本例中,该对象始终是雇员)。但是,您可以有不同类型的引用指向实际对象(引用可以是对象的类型或其超类之一)。
In the first one you have 2 OBJECTS, both are of the type Employee, and you have 2 different references to them, one being a Person reference and the other being an Employee reference.
In the second case, you have 1 OBJECT with 2 REFERENCES pointing to it, being one a Person and another an Employee.
The object is always of the type you've instantiated it with the new keyword (in this case, the object is always an employee). You can, however, have different types of references pointing to the actual object (the references can be either of the type of the object or of one of its superclasses).
第一个创建一个被引用为
Person
的Employee
和一个被引用为Employee
的Employee
。employee1
仅具有Person
方法,而employee2
具有Person
和Employee
方法。第二个创建一个
Employee
,然后创建一个对同一Employee
的Person
引用。只有在Person
类中定义的方法才可用于employeeKyel2
实例,而employeeKyle
将拥有所有Employee
方法。The first one creates an
Employee
that is referenced as aPerson
, and anEmployee
referenced as anEmployee
.employee1
has onlyPerson
methods, whileemployee2
has bothPerson
andEmployee
methods.The second one creates an
Employee
and then creates aPerson
reference to the sameEmployee
. Only methods defined in thePerson
class will be available to theemployeeKyel2
instance, whileemployeeKyle
will have all theEmployee
methods.在第二个示例中,您只有一个对象,有两个指向它的指针。如果您修改
employeeKyle
,则employeeKyle2
也会被修改(它们是同一对象)。您将employeeKyle2
引用为 Person 而不是Employee
这一事实仅对您允许在该引用上使用的方法有帮助。在幕后,它仍然指向同一个Employee
对象,因此,如果您稍后将employeeKyle2
转换为Employee
,它将起作用并且不会抛出异常强制转换异常。在第一个示例中,您创建了两个不同的对象,这是唯一的区别。它们是两个
Employee
对象,但您只能在employee2
上使用Person
方法。In your second example, you only have one object, with two pointers to it. If you modify
employeeKyle
, thenemployeeKyle2
will be modified as well (they are the same object). The fact that you refers toemployeeKyle2
as a Person and not anEmployee
is only helpful for the methods you're allowed to use on that reference. Under the hood, it's still pointing to the sameEmployee
object, so if you later castemployeeKyle2
as anEmployee
, it will work and not throw a cast exception.In the first example, you create two different objects, that's the only difference. They are two
Employee
objects under the hood, but you can only usePerson
methods onemployee2
.