为什么[新增"有时用构造函数,而不是其他(OOP的基本理解) [英] Why is "New" constructor used sometimes, but not others (basic OOP understanding)

查看:108
本文介绍了为什么[新增"有时用构造函数,而不是其他(OOP的基本理解)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的OOP问题:

I have a basic oop question:

我在操作一个构造的开始(在SharePoint开发为例)使用,但后来没有。例如:

I use (for example in the sharepoint development) at the beginning of an operation a New constructor, but later not. For example:

' CREATES A INSTANCE OF SPSITE FROM GIVEN URL '
Dim site As spsite = New spsite("http://myhost")

' HERE I DO NOT NEED TO USE NEW(...) AND IT RETURNS AN SPSITE OBJECT, '
' A NEW SPSITE OBJECT '
Dim web As spweb = site.openweb()

我的问题:为什么我需要在网站上的第一个对象使用新的,并在第二行不是

My question: Why do i need at the first object of site the use of new, and in the second line not!?

推荐答案

我真的不明白你的问题,但如果两条线是在code(以及他们应该是),那么它的工作原理是这样的: 这第一行创建一个新的SPSite对象,您可以与命名变量访问:网站

I don't really understand your problem, but if both lines are in your code (and they should be) then it works like this: This first line creates a new spsite object, which you can access with the variable named: site

dim site as spsite = new spsite("http://myhost")

下面创建一个新的SPWeb变量,通过调用pviously创建的网站变的$ P $的openweb()方法。这是一个函数调用。此函数返回的SPWeb实例,所以你不需要一个新的。

Here you create a new spweb variable, by invoking the openweb() method of your previously created site variable. This is a function call. this function returns an spweb instance so you don't need a new one.

dim web as spweb = site.openweb()

openweb()或者有一个新的SPWeb()里面,或者新的SPSite(myhost的)创建了一个新openweb你(这个你不必知道,你才知道,你会得到一个的SPWeb对象)

openweb() either has a new spweb() inside it, or the new spsite("myhost") created a new openweb for you (this you don't have to know, you just know, you'll get an spweb object)

这篇关于为什么[新增"有时用构造函数,而不是其他(OOP的基本理解)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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