如何使用继承类中的值隔离并返回继承的类? [英] How to isolate and return the inherited class with values from the inheriting class?

查看:58
本文介绍了如何使用继承类中的值隔离并返回继承的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是C#的新手,我想知道如何做我上面提到的。

下面是我使用的代码,我已经多次尝试过,并确认它每次都有效。但我的问题是它是如何工作的?

我需要知道将来的申请。

(顺便说一句,下面的代码不是全班但它包含所有的相关信息)



Hi guys,

I'm kinda new to C# and I want to know how to do what I mentioned above.
Below is the code that I use and I've tried it multiple times already and confirmed that it works every single time. But my question is how does it even work?
I need to know for future application.
(Btw, the code below is not the whole class but it contains all the relevant information)

public class GameData : StoredData
{
    public StoredData GetStoredData()
    {
        return new StoredData
        {
            CityPopulation = CityPopulation,
            CityConVill = CityConVill,
            WellOffGrowth = WellOffGrowth,
            CityJobs = CityJobs,
            JobsGrowthRate = JobsGrowthRate,
            Gold = Gold,
            StoryProgress = StoryProgress,
            CurrentMissions = CurrentMissions,
            SettTrade = SettTrade,
            Speed = Speed,
            DisInfoList = DisInfoList,
            GoodsPrices = GoodsPrices,
            GoldProd = GoldProd,
            CityPopCap = CityPopCap,
            DevCount = DevCount,
            PopulationLimit = PopulationLimit
        };
    }
}







任何类型的输入都将非常感激。提前谢谢! :)



我尝试了什么:



我试过冲浪网络,但我总是到达甚至没有触及我想知道的网页。

我删除了new StoredData {和};之间的所有内容。它神奇地不再起作用。




Any kind of input will be very much appreciated. Advance Thank you! :)

What I have tried:

I tried surfing the web but I always get to web pages that doesn't even touch to what I want to know.
I removed everything between "new StoredData{" and "};" and it magically doesn't work anymore.

推荐答案

有什么问题?您所做的只是创建一个类的新实例(恰好是当前类的基本情况)并返回它。变量具有相同名称的事实是无关紧要的,它只是看起来很奇怪,因为对象初始化程序的语法是这样的:

What's the problem? All you are doing is creating a new instance of a class (which happens to be the base case for the current class) and returning it. The fact that the variables have the same names is irrelevant, it just looks odd to you because the syntax of an object initializer works like that:
new ClassName { PropertyInClassName = value};

你可以写这个并得到同样的东西:

You could write this and get the same thing:

public class A { int X {get; set;};}
public class B
    {
    public int X = 333;
    A GetA() 
        {
        int X = 12345;
        A a1 = new A{X = X};
        A a2 = new A{X = 666};
        }
    }

系统知道X是A类中属性的名称,因此不能将其与本地X或类级别混淆那掩饰的X。

The system is aware that X is the name of a property in the A class, so it can't get it confused with the local X or the class level X that that masks.


这篇关于如何使用继承类中的值隔离并返回继承的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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