Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 继承:超类内容的副本是否在其中生成?_Java_Object_Inheritance_Memory - Fatal编程技术网

Java 继承:超类内容的副本是否在其中生成?

Java 继承:超类内容的副本是否在其中生成?,java,object,inheritance,memory,Java,Object,Inheritance,Memory,下面的代码来自 嘿,伙计们,我想再次检查一下下面的陈述是否正确: 创建My_Calculation类的对象时,会在其中创建超类内容的副本。 我的问题是: 当一个子类继承了超类时,它实际上是否复制了它的所有内容(这意味着超类和子类都有重复的字段,如gear,speed…等) 或 子类中的档位和速度只是对超类字段的引用?否,没有任何内容被“复制”到子类。您的MountainBike是一个带有一组字段的单个对象。其中一些字段在MountainBike类上声明,一些字段在其Bicycle超类上声明,但

下面的代码来自

嘿,伙计们,我想再次检查一下下面的陈述是否正确:

创建My_Calculation类的对象时,会在其中创建超类内容的副本。

我的问题是:

当一个子类继承了超类时,它实际上是否复制了它的所有内容(这意味着超类和子类都有重复的字段,如
gear
speed
…等)


子类
中的
档位
速度
只是对
超类
字段的引用?

否,没有任何内容被“复制”到子类。您的
MountainBike
是一个带有一组字段的单个对象。其中一些字段在
MountainBike
类上声明,一些字段在其
Bicycle
超类上声明,但对象上仍然只有一组字段


通过IDE中的调试器运行此代码,然后可以查看对象的结构。

创建派生类的对象时,其中包含基类的子对象。此子对象与您自己创建基类的对象相同。只是从外部看,基类的子对象被包装在派生类对象中。

你所说的复制是什么意思?它是指克隆吗?lucumt不是,只是简单地复制它,没有复制。
MountainBike
只有一个档位和一个速度。@DawoodibnKareem谢谢你,伙计,我知道,只是想确认我上面贴的图片是否正确?(MountainBike对象中自行车方法和字段的副本)非常感谢,您能查看一下这一页吗。你认为它是对的还是错的(那页的图表)…我不同意作者在教程中的说法-它让人觉得好像存在多个对象。存在一个子类类型的单个对象,但由于继承,它也是父类的实例。看看这个,官方教程:我刚刚检查了java的思想,形式见第6章:当您创建派生类的对象时,其中包含基类的子对象。此子对象与您自己创建基类的对象相同。只是从外部看,基类的子对象被包装在派生类对象中@里安内尔酒店
//Java program to illustrate the 
    // concept of inheritance 

    // base class
    class Bicycle 
    {
        // the Bicycle class has two fields
        public int gear;
        public int speed;

        // the Bicycle class has one constructor
        public Bicycle(int gear, int speed)
        {
            this.gear = gear;
            this.speed = speed;
        }

        // the Bicycle class has three methods
        public void applyBrake(int decrement)
        {
            speed -= decrement;
        }

        public void speedUp(int increment)
        {
            speed += increment;
        }

        // toString() method to print info of Bicycle
        public String toString() 
        {
            return("No of gears are "+gear +"\n" + "speed of bicycle is "+speed);
        } 
    }

    // derived class
    class MountainBike extends Bicycle 
    {

        // the MountainBike subclass adds one more field
        public int seatHeight;

        // the MountainBike subclass has one constructor
        public MountainBike(int gear,int speed, int startHeight)
        {
            // invoking base-class(Bicycle) constructor
            super(gear, speed);
            seatHeight = startHeight;
        } 

        // the MountainBike subclass adds one more method
        public void setHeight(int newValue)
        {
            seatHeight = newValue;
        } 

        // overriding toString() method
        // of Bicycle to print more info
        @Override
        public String toString()
        {
            return (super.toString()+ "\nseat height is "+seatHeight);
        }

    }

    // driver class
    public class Test 
    {
                public static void main(String args[]) 
        {

            MountainBike mb = new MountainBike(3, 100, 25);
            System.out.println(mb.toString());

        }
    }