java SuperClass和SubClass之间的类型转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java SuperClass和SubClass之间的类型转换相关的知识,希望对你有一定的参考价值。

package slidenerd.javaoop;

/**
 * Created by Microsoft on 10/6/2015.
 */
public class SuperClassReferenceVariable {

    public static void main(String[] args) {

        Person p1 = new Person();
        Person p2 = new Person();

        Student s1 = new Student();
        Student s2 = new Student();

        // All info of Person present in Student
        Person p3 = new Student();

        // Without typecast
        // But all info of Student needed is
        // not present in Person class
        Student s3 = (Student) p3; // works
        System.out.println(s3);
    }
}

class Person {
    String name;
    int age;
    String address;
}

class Student extends Person {
    int rollnumber;
    int marks;
}

以上是关于java SuperClass和SubClass之间的类型转换的主要内容,如果未能解决你的问题,请参考以下文章

SuperClass s=new SubClass(); 是啥意思? [复制]

Subclass构造函数是否从Superclass构造函数继承变量?

java-多态

Java之继承

寄生组合式继承

js继承函数封装