如果结构不能继承其他类或结构,为什么的Int32具有toString()方法? [英] If a struct cannot inherit another class or struct, why does Int32 have a ToString() method?

查看:212
本文介绍了如果结构不能继承其他类或结构,为什么的Int32具有toString()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  INT A = 2;

Console.WriteLine(a.ToString()); //显示2

//的ToString()的定义在这里 - 公共重写字符串的ToString();
 

现在,这里是我的一些理解:

  1. 所有的类在.net中得到了的ToString()的方法,这是从对象类继承。
  2. 的结构不能来源于类或其他结构。 INT 是一个类型的结构的Int32 ,它得到一对夫妇中的ToString() [附参数]从它实现的接口方法。
  3. 还有一个的ToString() [无PARAMS]在结构功能的Int32

据<一href="http://msdn.microsoft.com/en-us/library/system.int32.tostring.aspx">http://msdn.microsoft.com/en-us/library/system.int32.tostring.aspx,

  

结构的Int32覆盖ValueType.ToString()方法

如果一个结构不能继承某个类或结构,可以请你解释一下这是如何的ToString()方法可用于的Int32

解决方案
  

如果一个结构不能继承某个类或结构,

这是不正确的。所有的结构(和内置的值类型,如 System.Int32的 System.Single 等)总是隐含从<一个继承href="http://msdn.microsoft.com/en-us/library/system.valuetype.aspx"><$c$c>System.ValueType (其中,反过来,由 System.Object的继承)。

不过,你不能让一个结构,它继承的别的的。

这是明确规定了C#语言规范,4.1.1:

  

4.1.1 System.ValueType类型

     

所有的值类型隐式地从类System.ValueType,这反过来,从类object继承。这是不可能的任何类型来从值类型派生,值类型都是从而隐式密封的(第10.1.1.2节)。

然后,更高版本(4.1.3)结构被明确定义为值类型:

  

4.1.3结构类型

     

一个结构类型是可以声明常量,字段,方法,属性,索引,操作符,实例构造函数,静态构造函数和嵌套类型值类型。

int a = 2;

Console.WriteLine(a.ToString()); // displays 2

// definition of ToString() here - public override string ToString();

Now, here are some of my understandings:

  1. All the classes in .net get a ToString() method, which is inherited from the Object class.
  2. A structure cannot be derived from a Class or another struct. int is a structure of type Int32, which gets a couple of ToString() [With Parameters] methods from the Interfaces which it implements.
  3. There is also a ToString() [without params] function in struct Int32

According to http://msdn.microsoft.com/en-us/library/system.int32.tostring.aspx,

struct Int32 overrides ValueType.ToString() method

If a struct cannot inherit some class or struct, can you please explain how this ToString() method is available for Int32?

解决方案

If a struct cannot inherit some class or struct,

This isn't true. All structs (and the built-in value types, like System.Int32, System.Single, etc) always implicitly inherit from System.ValueType (which, in turn, inherits from System.Object).

However, you can't make a struct that inherits from anything else.

This is clearly spelled out in the C# language spec, 4.1.1:

4.1.1 The System.ValueType type

All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object. It is not possible for any type to derive from a value type, and value types are thus implicitly sealed (§10.1.1.2).

Then, later (4.1.3) struct is explicitly defined to be a value type:

4.1.3 Struct types

A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types.

这篇关于如果结构不能继承其他类或结构,为什么的Int32具有toString()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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