在另一个类中创建实例和继承一个类有什么区别 [英] what is difference creating instance and inheritance of a class in another class

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

问题描述



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

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.

推荐答案

创建实例是您实际使用类的方式:就像买车一样.

在购买汽车之前,您无法在需要时驾驶它:使用类,直到创建实例,您才能使用它的属性和方法.

Creating an instance is the way that you can actually use the class: it is like buying a car.

Until you buy a car, you can''t drive it when you want: with a class, until you create an instance you can''t use it''s properties and methods.

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.


,与封装和多态性一起,是面向对象编程的三个主要特征(或支柱)之一.继承使您可以创建可重用,扩展和修改其他类中定义的行为的新类.其成员被继承的类称为基类,而继承这些成员的类称为派生类.

派生类只能有一个直接基类.但是,继承是可传递的.如果ClassC派生自ClassB,而ClassB派生自ClassA,则ClassC继承在ClassB和ClassA中声明的成员.

班级可以
继承自另一个班级.这是通过在声明类时在类名后放置一个冒号,并在该冒号后命名要从其继承的类(基类)来实现的.

看看这个.....

Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behaviour that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.

A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

Classes can inherit from another class. This is accomplished by putting a colon after the class name when declaring the class, and naming the class to inherit from—the base class—after the colon.

Take a look at this.....

/// <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...


创建类的实例继承是不同的
创建实例意味着创建该类的对象,并使用该对象访问该类的属性和方法.
但是通过使用继承,您可以从该(基)类创建另一个类,该类包含基类的属性以及该类的新属性.
creating instance and inheritance of a class are different
creating instance means creating object of the class and by using that object access properties and methods of that class.
but by using inheritance you can create another class from that (base) class which contains properties of base class as well as new properties of that class.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆