小编D.A*_*fik的帖子

为什么我们需要在 C# 中进行 Upcasting 和 Downcasting?

考虑我们有三个类,Shape 是基类,另外两个类 Circle 和 Text 继承自基类(Shape)

形状文件

namespace ObjectOriented
{
    public class Shape
    {
        public int Height { get; set; }
        public int Width { get; set; }
        public int X { get; set; }
        public int Y { get; set; }

        public void Draw()
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

文本文件

using System;

namespace ObjectOriented
{
    public class Text : Shape
    {
        public string FontType { get; set; }
        public int FontSize { get; set; }

        public void Weirdo()
        {
            Console.WriteLine("Weird …
Run Code Online (Sandbox Code Playgroud)

c# downcast upcasting

1
推荐指数
1
解决办法
4782
查看次数

标签 统计

c# ×1

downcast ×1

upcasting ×1