请考虑以下两种情况:
//Data Contract
public class MyValue
{
}
Run Code Online (Sandbox Code Playgroud)
场景1:使用静态助手类.
public class Broker
{
private string[] _userRoles;
public Broker(string[] userRoles)
{
this._userRoles = userRoles;
}
public MyValue[] GetValues()
{
return BrokerHelper.GetValues(this._userRoles);
}
}
static class BrokerHelper
{
static Dictionary<string, MyValue> _values = new Dictionary<string, MyValue>();
public static MyValue[] GetValues(string[] rolesAllowed)
{
return FilterForRoles(_values, rolesAllowed);
}
}
Run Code Online (Sandbox Code Playgroud)
场景2:使用实例类.
public class Broker
{
private BrokerService _service;
public Broker(params string[] userRoles)
{
this._service = new BrokerService(userRoles);
}
public MyValue[] GetValues()
{
return …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class DocketType : Enumeration<DocketType, int, string>
{
public static DocketType ChangeOver = new DocketType(1, "Changeover");
public static DocketType Withdrawal = new DocketType(2, "Withdrawal");
public static DocketType Installation = new DocketType(3, "Installation");
private DocketType(int docketTypeId, string description)
: base(docketTypeId, description)
{
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下基类:
public abstract class Enumeration<TEnum, X, Y> : IComparable
where TEnum : Enumeration<TEnum, X, Y>
where X : IComparable
where Y : IComparable
{
protected Enumeration(X value, Y displayName)
{
AddToStaticCache(this);
}
public static TEnum Resolve(X …Run Code Online (Sandbox Code Playgroud) 请告诉我以下哪项是一项优秀的编程实践:
1.使用静态类,然后使用类MainWindow构造函数对它的引用,如下所示:
public partial class Mainwindow : Window
{
public MainWindow()
{
InitializeComponent();
UI.window = this;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Shutdownads attempt1 = new Shutdownads();
}
}
static class UI
{
public static MainWindow window;
}
/*and then refering to the wpf elements from other classes as follows
UI.window.textbox.Text="blahblah"
UI.window.button ... and so on
*/
Run Code Online (Sandbox Code Playgroud)
要么
2.在MainWindow类中包含我的程序中的所有类是否更好?
要么
3.有没有更好的选择(也可以实现更好的OOP以及我可以通过其他类访问UI)?
我有以下内容,我不能转移围绕DllImport的错误
#include "stdafx.h"
#include <msclr/auto_gcroot.h>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;
using namespace System::Threading;
using namespace System::Collections::Generic;
using namespace System::Text;
namespace WinFlix
{
class FlickWin
{
[DllImport("user32.dll")]
extern "C" bool SetForegroundWindow(IntPtr hWnd);
Run Code Online (Sandbox Code Playgroud)
我一直在从C#转换这个类,它是一个static类,在C++ .NET中不可用.我的新C++项目是"CLR控制台应用程序"类型.
我明白了
错误C2059:语法错误:'string'
和
错误C2238:';'之前的意外标记
都指的是'extern"C"'行.
我已经搜索过我之前的工作,虽然我能胜任C++/CLI,但我之前不必调用这样的WinAPI函数.这两个错误都是在打我: - /
TBH,这件事耗尽了我的耐心而我删除了它,可以回去重新创建它,但我更喜欢修复我的C#而不是.尽管如此,请帮助赞赏以供参考
我想创建一个具有流畅界面的静态类,但我收到错误:
'this'在静态属性中无效,方法......
这是我的代码:
public static MyClass
{
private static StringBuilder builder;
private static StringBuilder RouteBuilder
{
get
{
if (builder == null)
builder = new StringBuilder();
return builder;
}
}
public static MyClass Root()
{
RouteBuilder.Append("/");
return this;
}
public static MyClass And()
{
RouteBuilder.Append("and");
return this;
}
public static MyClass Something()
{
RouteBuilder.Append("something");
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
这使我能够做到 MyClass.Root().And().Something();
有没有办法做到这一点或解决方法?
我有一个数学辅助类,其中每个函数都是静态的,即作为参数输入的参数,返回值.我应该将整个类声明为静态吗?将静态修改器添加到类中是否会对性能产生影响?
此外,我不确定此指南的含义:"不要将静态类视为杂项." - 我有几个类只是一堆杂项静态函数...
所以我有一个类A。它定义了很多行为(方法),但也留下了很多子类来实现。这个类永远不会有实例。这只是定义所有子类共有的行为。将扩展 A 的子类(A1、A2、A3 等)都将是单例。我不希望这些子类有多个实例。
因此,最明显的方法是创建一个抽象类 A,然后创建扩展 A 的静态类 A1、A2、A3。
但显然,这在 C# 中是不允许的。我认为这是有充分理由的。微软的人可能比我更了解面向对象的软件设计。但我只需要一些帮助来弄清楚为什么这个设计“很差”,以及什么是替代的更好的设计。
我正在使用 WCF 编写 RESTful API。该服务将在一堆数据库表上执行 CRUD 操作。有很多代码对于所有表来说都是通用的,并且很多代码对于每个表来说都是特定的。此外,在任何给定时间只能对表执行一项操作。
所以我想我可以有一个抽象类TableHandler。然后是它的多个扩展,例如等TableAHandler。TableBHandler由于我只想要这些子类的一个实例,所以我想将它们设为静态。
首先,我来自.NET世界,其中有静态类(C#,也称为VB中的模块)和实例类 - 可以实例化的类.
这个问题是关于Javascript的,我正在尝试重新创建我已经知道的模式并创建一个模块/静态类.这是代码:
var MyModule = {
variable1: 0,
variable2: 1,
method1: function() {
//code goes here
},
method2: function() {
//mode code goes here
},
method3: function() {
//and some more just to prove the pattern
}
}
method1(); //error - no such method - ok, good, I expect that
variable1 = 5; //silently assigns a variable inside MyModule to 5 ?
Run Code Online (Sandbox Code Playgroud)
请解释为什么在命名空间内声明的方法不可见,但变量是什么?此外,有没有办法防止这些变量在外部可见MyModule,即使在同一个脚本中?
我想知道你是否可以在swift中创建类似于C#中的静态类,即一个无法实例化的类,它只能有静态函数和静态属性.这些类型的课程是否可以迅速实现?
如果没有,考虑到Swift中可用的工具,重建这种设计模式的最有效方法是什么?
我想实现一个简单的静态类,该类可以在练习时计算c ++中整数的pow值。所以我的代码在这里:
#pragma once
#ifndef MATH_H
#define MATH_H
static class Math
{
public:
static int pow(int,int);
};
#endif /* MATH_H */
Run Code Online (Sandbox Code Playgroud)
和pow功能的实现:
#include "Math.h"
int Math::pow(int base, int exp){
if(exp==1)
return base;
else if(exp%2==0)
return pow(base,exp/2)*pow(base,exp/2);
else
return pow(base,exp/2)*pow(base,exp/2)*base;
}
Run Code Online (Sandbox Code Playgroud)
但是cygwin编译器抛出编译错误:
In file included from Math.cpp:16:0:
Math.h:16:1: error: a storage class can only be specified for objects and functions
static class Math
^~~~~~
Run Code Online (Sandbox Code Playgroud)
static-classes ×10
c# ×7
.net ×3
oop ×2
c#-4.0 ×1
c++ ×1
c++-cli ×1
dllimport ×1
inheritance ×1
javascript ×1
module ×1
scope ×1
swift ×1
this ×1
translation ×1
wpf ×1