使用转换方法覆盖类型中的虚方法

时间:2016-01-22 20:05:17

标签: c# .net

当我写Console.WriteLine( new Point (1,1));时,它不会调用方法ToString。但它将对象转换为Int32,并将其写入控制台。但为什么?它似乎忽略了被覆盖的方法ToString

struct Point
{
    public Int32 x;
    public Int32 y;

    public Point(Int32 x1,Int32 y1)
    {
        x = x1;
        y = y1;
    }

    public static Point operator +(Point p1, Point p2)
    {
        return new Point(p1.x + p2.x, p1.y + p2.y); 
    }


    public static implicit operator Int32(Point p)
    {
        Console.WriteLine("Converted to Int32");
        return p.y + p.x;
    }

    public override string ToString()
    {
        return String.Format("x = {0}  |  y = {1}", x, y);
    }
}

2 个答案:

答案 0 :(得分:8)

原因是隐式转换为Int32 (您可能知道)

Console.WriteLine有许多重载需要StringObject和其他人,包括Int32

由于Point可隐式转换为Int32,因此使用int的{​​{1}} overload,这也会进行隐式转换。

这可以通过以下方式解决:

Console.WriteLine

您可以在Overload Resolution in C#中找到有关它的更多信息。

  

否则,最好的函数成员是一个函数成员   比给定的所有其他功能成员更好   参数列表,只要将每个函数成员与所有函数成员进行比较   其他功能成员使用Section 7.4.2.2 中的规则。

还有:

7.4.2.2 Better function member

  

对于每个参数,从AX到PX的隐式转换不是   比从AX到QX的隐式转换更糟糕,

答案 1 :(得分:2)

这是因为结构类型中的隐式转换,即以下行:

16/01/22 15:51:50 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
16/01/22 15:51:51 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
16/01/22 15:51:51 INFO input.FileInputFormat: Total input paths to process : 33
16/01/22 15:51:52 INFO mapreduce.JobSubmitter: number of splits:33
16/01/22 15:51:52 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1453492366595_0009
16/01/22 15:51:52 INFO impl.YarnClientImpl: Submitted application application_1453492366595_0009
16/01/22 15:51:52 INFO mapreduce.Job: The url to track the job: http://Marys-MacBook-Pro.local:8088/proxy/application_1453492366595_0009/
16/01/22 15:51:52 INFO mapreduce.Job: Running job: job_1453492366595_0009
16/01/22 15:51:56 INFO mapreduce.Job: Job job_1453492366595_0009 running in uber mode : false
16/01/22 15:51:56 INFO mapreduce.Job:  map 0% reduce 0%
16/01/22 15:51:56 INFO mapreduce.Job: Job job_1453492366595_0009 failed with state FAILED due to: Application application_1453492366595_0009 failed 2 times due to AM Container for appattempt_1453492366595_0009_000002 exited with  exitCode: 127
For more detailed output, check application tracking page:http://Marys-MacBook-Pro.local:8088/cluster/app/application_1453492366595_0009Then, click on links to logs of each attempt.
Diagnostics: Exception from container-launch.
Container id: container_1453492366595_0009_02_000001
Exit code: 127
Stack trace: ExitCodeException exitCode=127: 
    at org.apache.hadoop.util.Shell.runCommand(Shell.java:545)
    at org.apache.hadoop.util.Shell.run(Shell.java:456)
    at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:722)
    at org.apache.hadoop.yarn.server.nodemanager.DefaultContainerExecutor.launchContainer(DefaultContainerExecutor.java:211)
    at org.apache.hadoop.yarn.server.nodemanager.containermanager.launcher.ContainerLaunch.call(ContainerLaunch.java:302)
    at org.apache.hadoop.yarn.server.nodemanager.containermanager.launcher.ContainerLaunch.call(ContainerLaunch.java:82)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)


Container exited with a non-zero exit code 127
Failing this attempt. Failing the application.

因此编译器通过调用上面的隐式转换方法将Point类型视为整数。

要解决此问题,您需要从您的类型中删除隐式转换,或者在执行Console.WriteLine()时放置ToString()方法

这应该可以解决您的问题。希望这会有所帮助。

最佳