简体   繁体   English

C#WPF应用程序:什么是“这”?

[英]C# WPF application: What is “this”?

I have been playing a lot with WPF applications in C# and there are a lot of things that are not really clear to me, I have been trying to look it up and play around with it to figure it out but without much success since english is my second tongue and I am still not that good with terminology nor with programming... 我一直在使用C#中的WPF应用程序进行很多操作,很多事情对我来说还不是很清楚,我一直在尝试查找并尝试解决它,但是由于英语不够成功我的第二种语言,我对术语或编程仍然不满意...

1: What is "this" in the main class? 1: 主类中的“ this”是什么? When I create the new WPF application in XAML I get window and a grid. 当我在XAML中创建新的WPF应用程序时,会出现窗口和网格。 However, I dislike XAML greatly and like to write code for all the elements and objects that I need so I delete that first grid, make a class, define my grid and to add it I have to write something like 但是,我非常不喜欢XAML,并且喜欢为我需要的所有元素和对象编写代码,因此我删除了第一个网格,创建了一个类,定义了网格并添加了它,我必须编写类似

   this.AddChild(myGrid);

which is fine, but if I want to use "this" from my main class in other classes, it becomes a bit complicated to me. 很好,但是如果我想在其他班级的主要班级使用“ this”,这对我来说会有些复杂。 So, which UIElement or Object is "this"? 那么,哪个UIElement或Object是“ this”? How do I define it so it can be used in methods? 如何定义它以便可以在方法中使用? "this", I suppose refers to the Window that is created at beginning, but what UIElement or Object is that Window? 我想“ this”是指在开始时创建的Window,但是那个Window是什么UIElement或Object?

2: Extended classes?? 2: 扩展类? I have been watching a lot of java tutorials lately, simply to learn more about programming. 最近,我一直在看很多Java教程,只是为了学习更多有关编程的知识。 There, to use the objects from other class you can simply write: 在那里,要使用其他类的对象,您只需编写以下代码:

   public class class1 extends class2{}

and everything is perfect, I have found out that I can mimic that same thing in C# WPF unless it's the main class, since main class extends :Window and I guess since it's defined as partial class... Is there a way to "extend" multiple classes or go around this? 而且一切都非常完美,我发现我可以在C#WPF中模仿相同的东西,除非它是主类,因为主类扩展了:Window,而且我猜因为它被定义为局部类...有没有一种方法可以“扩展”还是要多课?

Any help on clearing this up would be great :) 任何有关清除此问题的帮助都将非常棒:)

You should learn Object Oriented Programming in C# 您应该学习C#中的面向对象编程

  1. this means the current instance of the class. this意味着该类的当前实例。 So in each class this refers to a different object. 因此,在每个类中, this指的是一个不同的对象。 this can usually be omitted and just AddChild(myGrid) can be used. this通常可以省略,并且只是AddChild(myGrid)都可以使用。
  2. extends (or : in C#) means that the first class ( class1 ) inherits from the second ( class2 ) thus having access to methods and variables that are defined in class2 that are not marked private . extends (或:在C#中)意味着第一个类( class1 )从第二个class2class2 )继承,因此可以访问在class2中定义的没有标记为private方法和变量。

For the part about 'this' and its identity, the Window sits in a hierarchy of classes and can assume the identity of any of its ancestors. 对于“ this”及其身份,“ Window位于类的层次结构中,可以假定其任何祖先的身份。 For example... 例如...

    public MainWindow()
    {
        InitializeComponent();
        var contentControl = this as ContentControl;
        var control = this as Control;
        var frameworkElement = this as FrameworkElement;
        var uiElement = this as UIElement;
        var dependencyObject = this as DependencyObject;
        var dispatcher = this as DispatcherObject;
    }

...all of the assignments in this snippet are legal. ...此代码段中的所有作业都是合法的。 Also, there are more exotic assignments such as 此外,还有更多的外来任务,例如

var x = this as IInputElement;

The key here is to examine the framework and the various assignments available to each class. 这里的关键是检查框架和每个班级可用的各种作业。 As others have pointed out, offline reading is essential to a quick learning curve. 正如其他人指出的那样,离线阅读对于快速学习至关重要。

The etymology of 'this' as a keyword in an object oriented context extends back to the late 1970's when it first appeared in an early specification for C++. 在面向对象的上下文中,“ this”作为关键字的词源可追溯到1970年代末,当时它首次出现在C ++的早期规范中。

Finally, Xaml is one of the most attractive features of WPF for lots of reasons, and if Xaml isn't compatible with your approach, you MIGHT be better off in WinForms or Swing or similar tightly bound framework. 最后,由于多种原因,Xaml是WPF最具吸引力的功能之一,如果Xaml与您的方法不兼容,则在WinForms或Swing或类似的紧密绑定框架中可能会更好。

Simply said this is the class you are in. 简单地说, this是您所在的班级。

For an example 举个例子

class dim
{
    int sum = 0;
    public void num(int sum){
        this.sum = sum; //Places the sum from num to the sum in dim
    }
}

Extending a class is basically termed as Inheritance in Object Oriented Programming. 扩展类在面向对象编程中基本上被称为继承。 There are several types of inheritance like single,multiple,multi-level,hierarchial,hybrid.But C# and also Java doesn't support inhertance from more than one class, because multiple inheritance creates a lot of ambiguity. 继承有多种类型,例如单继承,多继承,多层次,层次结构,混合。但是C#和Java不支持来自多个类的继承,因为多重继承会产生很多歧义。

A feature that replaces multiple inheritance is the use of interfaces. 替代多重继承的功能是使用接口。 Instead of 'extending from a class' we 'implement an interface' using the keyword 'implements'.An interface is just a skeleton class where you declare method signatures and the interface will be implemented in the class where you 'implement the interface'.The important point is you can implement more than one interface 而不是“从类扩展”,我们使用关键字“ implements”“实现接口”。接口只是一个骨架类,您在其中声明方法签名,接口将在您“实现接口”的类中实现。重要的是您可以实现多个接口

To get an overview about Inheritance and Interfaces,the following link would be helpful: 要获得有关继承和接口的概述,以下链接将很有帮助:

http://msdn.microsoft.com/en-us/library/ms228387(v=vs.80).aspx http://msdn.microsoft.com/zh-CN/library/ms228387(v=vs.80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM