在一个类中,“A obj = new B();”的含义是什么? [英] In a class, what is the meaning of that "A obj = new B();"

查看:722
本文介绍了在一个类中,“A obj = new B();”的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class A
   {
       public A()
       {
           Console.WriteLine("A");
       }

   }
   public class B : A
   {
       public B()
       {
           Console.WriteLine("B");

       }
       public void method1()
       {
           Console.WriteLine("BM1");
       }
   }

class Program
   {
       static void Main(string[] args)
       {
           A obj = new B();// what is use of it?
           obj.method();
           Console.Read();
       }
       private void methodP1()
       {

       }
   }

推荐答案

这意味着你有一个变量obj,它是A型然后在右侧,您正在创建B的实例并将其分配给变量obj。因为B是从A继承的所以没有强制转换是可能的。



现在使用变量obj时,你将只获得属于A的方法(直接方法)写在A或由B覆盖取决于情况)。



谢谢。
That means you are having a variable "obj" which is of type A. Then at Right Hand Side, you are creating an instance of B and assigning it into variable "obj". Since B is inherited from A so it is possible without casting.

Now while using variable "obj", you will get only methods which belongs to A (either direct methods written in A or overridden by B depends on situation).

Thanks.


使用 A obj = new B(); 创建一个B类的新对象并将其赋值给变量obj。这个变量实际上是类A的类型。但是,B是从A派生的,这将起作用。



超类引用变量可以保存子类对象的引用没有强制转换。
By using A obj = new B(); a new object of class B is created and assigned to variable obj. This variable is actually of type class A. However, B is derived from A and this will work.

A super class reference variable can hold reference of sub-class object without casting.


一个obj = new B();

你正在构建一个对象 obj 可以完成B或A的工作。

B继承自A.这样你就可以访问所有的属性和方法A.





发现这个 here [ ^ ]:

这是多态的基础:想象一下你有几个孩子从您的父类继承的类。您希望通过父类中定义的接口/方法使用所有这些子类,而不必担心每个子类中的实现细节(每个子类可能做不同的事情,但具有相同的整体语义)。



这是可能的,因为子类与父类具有 IS-A 关系,因为子类继承自父类。



我建议你学习继承&多态性,

https://www.cs.utexas.edu /~scottm/cs307/handouts/InheritanceExplanation.htm [ ^ ]

http: //www.c-sharpcorner.com/UploadFile/pcurnow/polymorphcasting06222007131659PM/polymorphcasting.aspx [ ^ ]





-KR
A obj = new B();
you're constructing an object obj that can do the job of either a B or a A.
B is inherited from A. This way you can access all the properties and methods of A.


And found this here[^]:
This is the basis for polymorphism: Imagine you have several child classes that inherit from you parent class. You want to use all these child classes through the interface / methods defined on your parent class, without worrying about the implementation details in each child class (each might do something different, but with the same overall semantics).

This is possible because the child class has a IS-A relationship with its parent class since child inherits from parent.

I'd suggest you to learn Inheritance & Polymorphism,
https://www.cs.utexas.edu/~scottm/cs307/handouts/InheritanceExplanation.htm[^]
http://www.c-sharpcorner.com/UploadFile/pcurnow/polymorphcasting06222007131659PM/polymorphcasting.aspx[^]


-KR


这篇关于在一个类中,“A obj = new B();”的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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