双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

new 和 static

seandro seandro 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在System.Web.UI.Page下
有几个页面要用到的语句是一样的,为了方便改动,当然是放在一个通用的类里。
第一种:
int Num = CommonRequest.GetUserLoginTimes(user)
代码:

public class CommonRequest//通用
{
public static int GetUserLoginTimes(User user)//static
{
return User.Type == "admin"?ServiceFactory.GetFactory().GetUserService().GetUserLoginTimes(User.userId):0;
}
}


第二种:
UserRequest ur = new UserRequest();//new
int Num = ur.GetUserLoginTimes(user)
代码:

public class UserRequest//独立类
{
public int GetUserLoginTimes(User user)
{
return User.Type == "admin"?ServiceFactory.GetFactory().GetUserService().GetUserLoginTimes(User.userId):0;
}
}




请问这有什么区别吗?
...全文

相关推荐

PHP中new static() 和 new self() 的区别介绍

主要介绍了PHP中new static() 和 new self() 的区别介绍,需要的朋友可以参考下

weixin_38591291

PHP中new static和new self的区别介绍

当我们在ChildClass中调用create()方法时,它返回的是ChildClass的实例,而不是ParentClass的实例。这意味着,如果我们使用继承来创建一个子类,并且在子类中调用了new static,它将返回子类的实例,而不是父类的实的实例。无论我们在哪个类中调用create()方法,它都只会返回ParentClass的实例,因为new self只会返回当前调用类的实例。当我们使用new self关键字创建对象实例时,它会始终返回当前调用的类的实例,而不考虑是否存在继承关系。

KkowServer

PHP的new static和new self的区别与使用

下面我们举个栗子: class Father { public static function getSelf() { return new self(); } public static function getStatic() { return new static(); } } class Son extends Father {} echo get_class(Son::getSelf()); // Father echo get_class(Son::getStatic()); // Son echo get_class(Father::getSel

weixin_38657835

浅谈PHP中new self()和new static()的区别

本文介绍了PHP中new self()和new static()的区别,分享给大家,也给自己留个笔记。 1.new static()是在PHP5.3版本中引入的新特性。 2.无论是new static()还是new self(),都是new了一个新的对象。 3.这两个方法new出来的对象有什么区别呢,说白了就是new出来的到底是同一个类实例还是不同的类实例呢? 为了探究上面的问题,我们先上一段简单的代码: class Father { public function getNewFather() { return new self(); } public function get

weixin_38653878

PHP new static 和 new self详解

使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类:使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。

weixin_38635794
评论 18
收藏 已收藏
分享
返回顶部
seandro
C# 111,129 成员 642,540 内容