多态性赋值语句

发布于 2024-12-14 04:07:56 字数 2288 浏览 0 评论 0原文

我对其赋值语句的多态性有疑问,例如这是超类

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-12-21 04:07:56

在第一个对象中,您有 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).

凉墨 2024-12-21 04:07:56

第一个创建一个被引用为 PersonEmployee 和一个被引用为 EmployeeEmployeeemployee1 仅具有 Person 方法,而 employee2 具有 PersonEmployee 方法。

第二个创建一个Employee,然后创建一个对同一EmployeePerson 引用。只有在 Person 类中定义的方法才可用于 employeeKyel2 实例,而 employeeKyle 将拥有所有 Employee方法。

The first one creates an Employee that is referenced as a Person, and an Employee referenced as an Employee. employee1 has only Person methods, while employee2 has both Person and Employee methods.

The second one creates an Employee and then creates a Person reference to the same Employee. Only methods defined in the Person class will be available to the employeeKyel2 instance, while employeeKyle will have all the Employee methods.

千と千尋 2024-12-21 04:07:56

在第二个示例中,您只有一个对象,有两个指向它的指针。如果您修改 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, then employeeKyle2 will be modified as well (they are the same object). The fact that you refers to employeeKyle2 as a Person and not an Employee is only helpful for the methods you're allowed to use on that reference. Under the hood, it's still pointing to the same Employee object, so if you later cast employeeKyle2 as an Employee, 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 use Person methods on employee2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文