C#中的对象创建.......... [英] OBJECT CREATION IN C#..........

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

问题描述

这里的对象是由两个不同类型的一个再生来调用方法,它通过new关键字返回静态实例一。这两个对象之间有什么区别,它背后的原因是什么

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

public class 计划
{
private static 程序实例= new Program();

public static 计划GetInstance()
{
return 实例;
}

public 程序Msg()
{
程序p1 = new Program();
Console.WriteLine( HII);
return p1;
}
}
class Class2:程序
{
private static void Main( string [] args)
{
Program program = new Program(); // 此处对象(基类)由简单的新关键字
程序创建。消息();

程序program1 = Program.GetInstance(); // 此处对象由GetInstance创建()返回静态的方法
program1.Msg();
Console.Read();
}
}

解决方案

对我而言,你的问题和你的代码,建议你真的需要去返回并研究声明为'static,并声明为'public的类,以及在两种类中使用Constructors的要求/含义之间的差异。



您可能会发现,对于在StackOverFlow上使用静态类的Jon Skeet的许多深思熟虑的帖子都很有用:[ ^ ],例如:[ ^ ]。



你的目标是什么?如果你想要实现的是一个'Singleton模式类,Skeet在他的博客上的这篇文章可能会有所帮助:[ ^ ]。



如果您是.Net和C#的新,我建议分别研究'public和'静态类的实现,而不是像代码中所示将它们混合在一起。我还没有看到一个真实场景,其中一个定义为非静态的类需要在代码中定义另一个静态版本。



WinForms .NET或控制台应用程序中的约定,通过访问静态类('Program)中的静态方法('Main),自动启动应用程序本身(只要编译器具有有效引用),是一个特殊情况,而不是你想尝试并用作模型来构建你自己的应用程序超级结构的东西。



请注意,在您的代码中,类的程序不是'静态的,而是'公共的。


您显然正在尝试使用单例模式。

所以你绝对不应该写

程序程序= 程序(); 



这会破坏你试图设置的设计模式。

只需使用

程序程序= Pr ogram.GetInstance(); 



使用Program类型的第二个变量是没有意义的。



或者:你肯定需要你的Program类的几个实例,在这种情况下,单例模式是无关紧要的,应该从你的类的定义中删除。





当我使用单例模式时,我总是使用以下链接中描述的模式:

在C#中实现单例模式 [ ^ ]

[/ EDIT


据我所知 -

 < span class =code-digit> 1 程序程序=  new 程序(); 



此对象创建将始终创建Program类的新实例。

  2 。程序program1 = Program.GetInstance(); 



此对象创建将只创建一个Program Class实例,其值在整个应用程序中是相同的。这种类型的对象创建称为Singleton Object。



欲了解更多信息,请参阅此链接---

http://msdn.microsoft.com/en-us/library/ff650316.aspx [ ^ ]


here object is created by two different type one bye calling the method which is returning the static instance one via new keyword . is there is difference between these two object and what is the reason behind it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Program
{
    private static Program instance = new Program();

    public static Program GetInstance()
    {
        return instance;
    }

    public Program Msg()
    {
        Program p1 = new Program();
        Console.WriteLine("HII");
        return p1;
    }
}
class Class2 : Program
{
    private static void Main(string[] args)
    {
        Program program = new Program();  //  here the object( base class) is create  by simple new Key word
        program.Msg();

        Program program1 = Program.GetInstance();// here the object is create by GetInstance() method which return static
        program1.Msg();
        Console.Read();
    }
}

解决方案

To me your questions, and your code, suggest that you really need to go back and study the differences between Classes declared as 'static, and declared as 'public, and the requirements/implications of the use of Constructors in both types of Classes.

You may find examining Jon Skeet's many thoughtful posts on use of 'static Classes on StackOverFlow helpful: [^], such as this one: [^].

What are your goals here ? If what you are trying to implement is a 'Singleton pattern class, this article by Skeet on his blog may be helpful: [^].

If you are new to .Net and C#, I'd recommend studying implementations of 'public and 'static Classes separately, rather than mixing them together as shown in your code. I have yet to see a real-world scenario in which a Class defined as non-static, needs to define another, 'static, version of itself in code.

The convention in WinForms .NET, or a Console Application, that the Application itself is "launched" automatically (as long as the compiler has a valid reference) by accessing a static method ('Main) in a static Class ('Program), is a special case, and not something that you want to try and use as a model to build your own Application "super-structure" around.

Note that in your code the Class 'Program is not 'static, but 'public.


You obviously are trying to use the singleton pattern.
So you definitely should not write

Program program = new Program();


This defeats the design pattern you tried to set up.
Just use

Program program = Program.GetInstance();


There is no point in using a second variable of type Program.

Or: you definitely need several instances of your Program class, in that case the singleton pattern is irrelevant and should be removed from the definition of your class.

[EDIT]
When I come to use the singleton pattern, I always use the one describe in the following link:
Implementing the Singleton Pattern in C#[^]
[/EDIT


As I know that -

1 Program program = new Program();


This object creation will always create a new instance of Program class..

2. Program program1 = Program.GetInstance();


This object creation will create only one instance of Program Class and its value is same in whole application. This type of object creation is known as Singleton Object.

For more information refer this link ---
http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]


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

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