关于C#中的对象创建 [英] Regarding object creation in C#

查看:47
本文介绍了关于C#中的对象创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用像这样的父类创建子类的对象:

manager obj =新员工,如果是,那么它需要在哪里?以及如何在内存级别实现

感谢您的回复



我尝试了什么:



i是C#和oops的新手。这是我第一次遇到c#而且用c#只用了15天所以请帮我学习

can i create object of child class using parent class like this:
manager obj=new employee if yes then where is it needed? and how at memory level it is implemented
thank u for response

What I have tried:

i am new to C# and oops . this is my first encounter with c# and it has been just 15 days with c# so help me to learn

推荐答案

不,你不能,因为Manager类不仅仅是一个员工。你可以反过来做,但这真的没有意义。
No you cannot, since the Manager class is more than just an Employee. You can do it the other way round, but that really makes no sense.


取决于。

如果你的类被正确声明,那么是 - 但是从那以后经理是员工,但员工不一定是经理,可能不是。

通常,你会这样:

It depends.
If your classes are declared correctly, then yes - but since a manager is an employee, but an employee does not have to be a manager, probably not.
Normally, you would have this:
class Employee
   {
   string Name {get; set;}
   int EmployeeNumber {get; set;}
   Employee ReportsTo {get; set;}
   }
class Manager : Employee
   {
   Car CompanyCar {get; set;}
   }



然后你可以愉快地说:


And then you can happily say:

Employee emp = new Manager();

因为经理是员工的超集 。

然后您可以访问任何员工属性:

Becuase a Manager is a "superset" of an employee.
You can then access any Employee properties:

Employee emp = new Manager();
...
Console.WriteLine(emp.Name);

但是......因为emp可以包含任何员工,你无法访问任何Manager特定的方法属性通过它。

But...because emp can contain "any employee" you can't access any Manager specific properties of methods via it.

Manager man new Manager();
Employee emp = man;
...
Console.WriteLine(emp.Name);
Console.WriteLine(man.CompanyCar);



但你不能这样做:


But you can't do this:

Manager man = new Employee(); 

因为Employee没有任何特定于Manager的方法属性:在这种情况下,因为Employee没有获得CompanyCar,而Managers则没有。

Because an Employee does not have any of the Manager specific properties of methods: in this case because an Employee does not get a CompanyCar, while Managers do.


Welcome学习C#的奇妙冒险!



请记住,这里的每个人都曾经是初学者,我们都...我保证...了解我们所知道的现在,犯了很多错误。我们大多数人都知道技术学习是一个持续的过程,包括在一段时间内对同一材料进行多次传递。



你需要从一些勤奋的学习和基础练习开始。获得关于C#和.NET的好的介绍;这是一个很好的,你现在可以免费下载:Charles Petzold,.NET Book Zero:[ ^ ]。有一本非常好的英语免费书,由一群保加利亚C#程序员创建,你可以在这里下载:[ ^ ]。



我强烈建议你得到保加利亚书籍并仔细审查:



1.首先是第14章,定义课程,第499页ff。



2.第11章,创建和使用对象,页面#385 ff。



实践练习非常重要;你需要如果你还没有得到Visual Studio设置,并拿走你的书,并在示例中编写代码;运行它们,观察会发生什么。当出现错误时...它们会发生...在你的中放入断点代码,然后运行代码。



学习调试至关重要,当代码在断点处停止时,您可以在Visual Studio中使用F11单步执行代码,通过将代码悬停在其名称上来观察变量和对象的值在代码中。



imho这些东西可以帮助你学习:



1.试试找到合适的节奏和混合的学习和实验:正确的步伐将让你好奇并渴望学习......试图学得太多太快会导致你感到沮丧。



2.使用Visual Studio提供的文档,这些文档非常容易访问。



3.花时间在CodeProject上阅读一些关于C#和.NET各个方面的许多优秀教程。



4.类和继承:记住,当您编写的编译代码被执行时,Class或Struct定义是构造实例(对象)的模板(在运行时)。



有许多强大的工具可用于在C#中实现OOP和继承,包括接口,抽象类,虚方法,能力过度使用方法等。



了解对于大多数人来说,需要几个月才能真正深入掌握这些工具。



5. C#:与您需要了解的任何计算机语言一样:



a。创建变量和属性。了解如何通过引用和使用'out修饰符将变量传递(和返回)到方法(作为参数)。放在变量/属性声明前面的访问修饰符(私有,公共,受保护等)。



b。创建像数组这样的数据结构。创建通用数据结构。



c。使用和不使用参数编写方法,编写返回某些值的方法,以及不返回值的方法(void)。



d。控制流:for-loop,while-loop,switch语句。的try / catch /最后。使用'休息和'继续。



当你学习并卡住(我们都这样做)时,尽量仔细撰写你在这里提出的问题,或者在其他论坛上。尝试并描述具体问题,并清楚地描述发生的错误(如果有的话)。显示与问题相关的精选部分代码。



现在,让我们开始学习...... :)
Welcome to the wonderful adventure of learning C# !

Remember that everyone here was once a beginner, and we all ... I guarantee you ... learned what we do know, now, by making many mistakes. Most of us have learned that technical learning is an on-going process involving making multiple "passes" over the same material over time.

You need to start with some diligent study and practice of the basics. Get a good introductory on C# and .NET; here's a good one you can download for free, right now: Charles Petzold, ".NET Book Zero: [^]. There is a very good free book in English created by a group of Bulgarian C# programmers you can download here: [^].

I strongly recommend you get the Bulgarian book and carefully review:

1. first Chapter 14,"Defining Classes," pages #499 ff.

2. Chapter 11., "Creating and Using Objects," pages #385 ff.

Hands-on practice is so important; you need to get Visual Studio set-up if you haven't already, and take your book, and code in the examples; run them, observe what happens. When errors occur ... and they will occur ... put break-points in your code, and then run the code.

Learning to debug is critical, and when your code stops at a break-point, then you can use F11 in Visual Studio to single-step through the code, observing the values of variables and objects by hovering over their names in the code.

imho these are some things that will help you learn:

1. try to find the right pace and "mix" of study and experimentation: the "right pace" will leave you curious and eager to learn ... trying to learn too much too fast will result in your being frustrated.

2. use the documentation provided by Visual Studio that's so easily accessible.

3. spend time here on CodeProject reading some of the many excellent tutorials on every aspect of C# and .NET.

4. Classes and inheritance: remember that a Class, or Struct, definition is a template for the construction of instances (objects) when the compiled code you write is executed (at run-time).

There are many powerful facilities that are available for implementing OOP and inheritance in C#, including Interfaces, Abstract classes, Virtual methods, the ability to over-ride methods, etc.

Understand that for most people it's going to take some months to really reach a deeper mastery of those tools.

5. C#: as with any computer language you are going to need to understand:

a. creation of variables, and properties. understanding how variables can be passed (and returned) to methods (as parameters) by reference and by using the 'out modifier. the access modifiers that put in front of variable/property declarations (private, public, protected, etc.).

b. creation of data structures like Arrays. creating generic data structures.

c. writing methods with and without parameters, writing methods that return some value(s), and methods that return no value (void).

d. flow-of-control: for-loop, while-loop, switch statement. try/catch/finally. use of 'break and 'continue.

As you learn and get "stuck" (as we all do), try to carefully compose the questions you ask here, or on other forums. Try and describe specific issues, and clearly describe what error occur (if any). Show carefully selected parts of your code relevant to the problem.

And, now, let's get started studying ... :)


这篇关于关于C#中的对象创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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