在另一个类中创建实例和继承一个类有什么区别

在另一个类中创建实例和继承一个类有什么区别

本文介绍了在另一个类中创建实例和继承一个类有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不知道在另一个类中创建实例和继承一个类有什么区别,以及我可以从一个类访问另一个类的方法有多少种.

Hi,

I have no idea on what is difference creating instance and inheritance of a class in another class and how many ways can i access a class from one class to another class.

推荐答案

MyClass mc = new MyClass();

创建MyClass的实例,您现在可以通过变量mc访问-这是对实际实例的引用.这听起来有点令人困惑(没有图片),但是这样想着山雀:mc并不是实际的MyClass实例本身,它只是告诉系统在哪里可以找到它-您可以更改MyClass而不影响实例:

Creates an instance of MyClass which you can now access via the variable mc - which is a reference to the actual instance. That sounds a little confusing (and without picture it is) but think of tit this way: mc is not the actual MyClass instance itself, it just tells the system where to find it - you can change MyClass without affecting the instance:

MyClass mc;

mc存在,但为空-它没有与之关联的实例,任何使用mc的尝试都将导致对象引用未设置为实例"对象"错误.

mc exists, but it is null - it has no instance associated with it, and any attempt to use mc will cause an "Object reference not set to an instance of an object" error.

mc = new MyClass();

创建MyClass的实例,并将其分配给mc-您现在可以访问类实例的属性:

Creates an instance of MyClass, and assigns it to mc - you can now access the properties of the class instance:

mc.Text = "My first instance";
Console.WriteLine(ms.Text);

您还可以创建其他变量和其他实例:

You can also create other variables, and other instances:

MyClass mc2 = new MyClass();
mc2.Text = "My second instance";
Console.WriteLine(mc);
Console.WriteLine(mc2);

将写入

My first instance
My second instance

您可以复制mc

You can copymc

MyClass mc3 = mc;
Console.WriteLine(mc);
Console.WriteLine(mc2);
Console.WriteLine(mc3);


会写


Will write

My first instance
My second instance
My first instance

您可以更改mc

You can change mc

mc = new MyClass();
mc.Text = "My third instance";
Console.WriteLine(mc);
Console.WriteLine(mc2);
Console.WriteLine(mc3);

将写入

My third instance
My second instance
My first instance

还有更多,但目前应该能给您足够的机会!

继承是不同的:在创建新类的实例之前,它不会创建任何代码可使用的东西-它所做的是允许新类具有继承类的所有属性和方法,而无需编写它们再来一次.
如果愿意,汽车的品牌(福特,宝马等)是汽车的继承类,因为所有汽车都有四个车轮,一个发动机,一个方向盘等.福特类增加了一个椭圆形的徽章,BWM类增加了一个圆形的徽章,依此类推.如果您可以驾驶汽车,则可以驾驶福特和宝马,以及所有其他衍生自汽车的品牌.

There is more too it, but that should give you enough for the moment!

Inheritance is different: it doesn''t create anything you code can use without an instance of the new class being created - what it does do is allow the new class to have all the properties and methods of the inherited class without having to write them all again.
If you like, the make of a car (Ford, BMW, etc.) is an inherited class from Car, because the all have four wheels, an engine, a steering wheel, and so forth. The Ford class adds an oval badge, the BWM class adds a round badge and so forth. If you can Drive a Car, you can Drive a Ford, and a BMW, and all the other makes which are derived from Car.


/// <summary>The Class Car - A normal car.
    /// </summary>
    public class Car
    {
        /// <summary>Creates a new car object.
        /// </summary>
        public Car()
        { }

        public virtual string Plate
        { get; set; }

        public void Drive()
        { }

        public void Reverse()
        { }
    }

    /// <summary>A type of Advanced car.
    /// </summary>
    public class SuperCar : Car
    {
        public SuperCar()
        { }

        public void Fly()
        { }

        public void Swim()
        { }
    }

    /// <summary>BatMan's car.
    /// </summary>
    public class BatMobile : SuperCar
    {
        /// <summary>Creates a new instance of this class.
        /// </summary>
        public BatMobile()
        { }

        public void Weapons()
        { }

        public override string Plate
        {
            get
            {
                return base.Plate;
            }
            set
            {
                base.Plate = value;
            }
        }
    }



请注意,这些类是如何继承的?

头等车可以行驶,也可以倒车,它还具有 virtual 字符串,这意味着可以在其他派生类中对其进行修改.

第二个类Supercar可以飞行和游泳,但它也可以驱动和反转,因为它是从其基类Car
继承而来的
现在,BatMobile类只有一种方法Weapons,但它也可以进行Fly,Swim,Drive和Reverse,因为它是从SuperCar的基类继承而来的,而SuperCar也是从Car继承的.

其他示例.

因此,要创建类BatMobile的实例,就像准备使用汽车BatMobile ...在C#



Notice, how the classes are inherited?

The First class car can Drive and can Reverse, it also has a virtual string which means that it can be modified in other derived classes.

The second class Supercar, can Fly and Swim but it can also Drive and Reverse because it has inherited it from it''s base class Car

Now, the class BatMobile only has one method Weapons, but it can also Fly, Swim, Drive and Reverse because it has inherited it from it base class SuperCar which also inherited it from Car.

Other examples.

So to create an instance of the class BatMobile, this is like preparing to use the car BatMobile... In C#

BatMobile BatCar = new BatMobile()



在VB中



In VB

Dim BatCar as new BatMobile()



现在我们已经创建了汽车,现在我们可以使用它来做我们想做的任何事情.



Now that we have created the car, we can now use it to do whatever we want.

BatCar.Drive();
BarCar.Fly();



请注意,所有类都从System.Object
继承Object
实例化...的其他方式



Note that all classes inherit the Object class from System.Object

Other ways of instantiating...



这篇关于在另一个类中创建实例和继承一个类有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:34