C#问题 - 我正在尝试确定是否可以使用静态方法,在该方法中它确实使用了一些局部变量.局部变量是否在方法的使用中"共享"?例如,如果从不同的线程同时调用/使用静态方法会发生什么?一个线程阻塞,直到另一个线程完成等吗?
也许通用的问题是,在线程应用程序中,何时"不"使用静态方法?
struct B {
void foo () {}
};
struct D : B {
using B::foo;
static void foo () {}
};
int main ()
{
D obj;
obj.foo(); // calls D::foo() !?
}
Run Code Online (Sandbox Code Playgroud)
成员方法和static成员方法完全不同有两个原因:
static 方法不会覆盖base中的虚函数 class当一个方法被一个对象调用时,成员方法逻辑上不应该有更高的偏好吗?(只是C++允许static使用对象调用方法,它会被视为重写方法吗?)
如何调用静态方法?我想从我创建的类中调用它,我想从IP获取位置.我已经宣布了它,但我需要做的是调用方法......如static...
为了跟你说实话,我很困惑在这里,我需要实例化address,city等等?
到目前为止我已经这样做了;
LocationTools.cs
public static class LocationTools
{
public static void GetLocationFromIP(string address, out string city, out string region, out string country, out double? latitude, out double? longitude)
{
Run Code Online (Sandbox Code Playgroud)
Home.cs
public string IPAPIKey
{
get
{
return WebConfigurationManager.AppSettings["IPAPIKey"];
}
}
////To get the ip address of the machine and not the proxy use the following code
static void GetLocationFromIP()
{
string strIPAddress = Request.UserHostAddress.ToString();
strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIPAddress == null || strIPAddress …Run Code Online (Sandbox Code Playgroud) 考虑:
//在Vector2.h中
class Vector2
{
public:
// returns the degrees in radians
static double calcDir(double x, double y);
}
Run Code Online (Sandbox Code Playgroud)
//在Vector2.cpp中
double Vector2::calcDir(double x, double y)
{
double rad = ...;
return rad;
}
Run Code Online (Sandbox Code Playgroud)
为什么Vector2.cpp中的签名不需要关键字static?当我尝试这个时,它会产生一个错误:
static double Vector2::calcDir(double x, double y)
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎不一致.方法签名的所有其他部分都需要在.cpp文件中重复(返回类型,方法名称(duh),args的名称和类型,const-ness).我不喜欢一眼就知道方法是否是静态的(在查看实现时).
有没有理由这不仅不是必需的,而且是禁止的?
据说"静态方法是可测试性的死亡".如果是这样,那么下面的可行替代模式是什么?
class User {
private $phone,
$status = 'default',
$created,
$modified;
public function __construct($phone) {
$this->phone = $phone;
$this->created = new DateTime;
$this->modified = new DateTime;
}
public static function getByPhone(PDO $pdo, $phone) {
$stmt = $pdo->prepare('SELECT * FROM `users` WHERE `phone` = :phone');
$stmt->execute(compact('phone'));
if (!$stmt->rowCount()) {
return false;
}
$record = $stmt->fetch(PDO::FETCH_ASSOC);
$user = new self($record['phone']);
$user->status = $record['status'];
$user->created = new DateTime($record['created']);
$user->modified = new DateTime($record['modified']);
return $user;
}
public function save(PDO $pdo) {
$stmt …Run Code Online (Sandbox Code Playgroud) 可以使用私有静态方法或使用静态块初始化静态变量.这两者之间有什么微妙的区别吗?有没有我不能使用静态方法初始化静态成员的情况?我发现后者更具可读性.
静态块初始化:
private static int NUM_ITER;
static {
// Operations
NUM_ITER = //val from above operations.
}
Run Code Online (Sandbox Code Playgroud)
私有静态方法初始化:
private static int NUM_ITER = calculateNumIter();
// Some method comment on how we are calculating.
private static int calculateNumIter()
{
// Operations.
return //value_from_operations.
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢第二个,因为它更具可读性.有没有我必须先使用的情况(静态块)?
初始化静态成员(最终成员和变量成员)的最佳编码约定/设计是什么?即使从这个线程我学习私有静态方法也比静态块有优势.
谢谢,
php 5.3+
很抱歉这个问题很长,但我想完全了解这一点.
我知道我不能从静态方法中调用非静态相同类方法,而不将类实例化为对象.
class Person
{
private $people_array;
function data_all_get()
{ // touch database, return array of people
$this->people_array = // etc dbquery results
}
static function showPeople()
{ // call class method
$people_data = $this->data_all_get();
// Fatal error: Using $this when not in object context
}
} // end class Person
Run Code Online (Sandbox Code Playgroud)
从搜索SO,我发现了一些有趣的方法,但想知道每种方法如何影响代码环境.
我的问题如下:
我可以将类实例化为静态方法中的对象,以获得对非静态方法的访问
static function showPeople()
{ // instantiate as object
$person = New Person();
// call class method
$people_data = $this->data_all_get();
}
Run Code Online (Sandbox Code Playgroud)
Q1 - 这会导致什么问题? 在我的情况下,该类没有构造函数,因此该实例不会影响其他类方法或变量.在脚本执行期间,这个新对象会占用内存中的一小块空间吗?似乎不太糟糕...... …
我实际上是在尝试实现Paging的模拟,在我的内存管理器中,我尝试创建一个静态页表,但是当我尝试打印时它给出了引用错误.
#ifndef MEMORYMANAGER_H
#define MEMORYMANAGER_H
#include "memory.h"
class MemoryManager
{
private:
PhysicalMemory RAM;
LogicalMemory VM;
int offsetValue;
static int ** pageTable;
public:
MemoryManager();
bool addProcess(TimeSliceRequest);
void printVirtualMemory();
/*
* Page Table Initialization
**/
static void initializePageTable(){
pageTable = new int * [pageSize];
for (int i=0; i<pageSize; i++) {
pageTable[i] = new int [2];
}
}
static int getPageTabe(int x, int y) {
return MemoryManager::pageTable[x][y]; // undefined reference to `MemoryManager::pageTable'
}
static void printPageTable(){
for(int i=0; i<pageSize; i++){
for(int j=0; j<2; …Run Code Online (Sandbox Code Playgroud) 关于静态的东西:
类似的问题:
我很困惑:
我开始使用ReSharper的,它表明,当一个方法可以进行静态的.将几百种方法转换为静态方法会在很长一段时间内增加内存占用量吗?
static-methods ×10
c# ×3
c++ ×3
static ×3
java ×2
php ×2
instance ×1
methods ×1
oop ×1
overriding ×1
unit-testing ×1