当我在Eclipse项目的默认包中创建一个新的main.java文件时,它会生成一个如下所示的main方法:
public static void main(String[] args)
{
}
Run Code Online (Sandbox Code Playgroud)
这立即引发了一个警告说This method has a constructor name.建议的解决方法是删除void:
public static main(String[] args)
{
}
Run Code Online (Sandbox Code Playgroud)
现在而不是警告,我得到一个错误:Illegal modifier for the constructor in type main; only public, protected & private are permitted.如果我删除了static,我的代码现在看起来像:
public main(String[] args)
{
}
Run Code Online (Sandbox Code Playgroud)
这一次,我仍然得到一个错误,但另一个错误说:
Error: Main method not found in class main, please define the main method as:
public static void main(String[] args)
Run Code Online (Sandbox Code Playgroud)
Argggh!但这让我回到了我开始的地方.如何定义主方法以便我不会收到任何错误或警告?
我正在使用Eclipse Juno Service Release 2和JavaSE-1.7.请注意,我对Java很新; …
有没有一种简单的方法可以深入到对象并通过其哈希码查找属性或字段?这可以是嵌套属性或集合中的值。我问的原因是,我偶尔会收到如下WPF警告:
System.Windows.ResourceDictionary Warning: 9 : Resource not found;
ResourceKey='#FF000000'; ResourceKey.HashCode='51639504';
ResourceKey.Type='System.Windows.Media.SolidColorBrush'
Run Code Online (Sandbox Code Playgroud)
警告并不总是出现,而我很难找到它。我知道如果我知道哪个对象具有该哈希码,那么我可以更进一步地解决这个问题。例如,如果我有这个对象:
var first = new { a = 1, b = 2, d = new { aa = 11, bb = 22 } };
Run Code Online (Sandbox Code Playgroud)
并对此进行了调用:
string str = FindHashCode(first, 22);
Run Code Online (Sandbox Code Playgroud)
结果将是:
"Anon > d > bb.hashcode = 22"
Run Code Online (Sandbox Code Playgroud)
或类似的东西。(我现在暂时忽略哈希码冲突)
编辑:这是基于@Alberto的答案,我将使用的内容。它搜索字段和属性,无论是公共的还是非公共的。它包括对IEnumerables(列表,数组等)的支持,尤其是对IDictionaries的支持。它还处理哈希码冲突。如果两个对象具有相同的哈希码,则StringBuilder将为每个对象单独使用一行。
using System.Reflection;
static string FindHashCode(object o, int hashCode)
{
StringBuilder strb = new StringBuilder();
FindHashCode(o, hashCode, o.GetType().Name, strb);
return strb.ToString().Trim();
}
static void FindHashCode(object o, int hashCode, string path, …Run Code Online (Sandbox Code Playgroud) 我正在尝试为自己的启发编写一个快速排序.我在维基百科上使用伪代码作为指南.我的代码有效.它似乎应该在O(n log n)时间内运行.我尝试实际计时我的代码以检查复杂性(即,当我将输入大小加倍时,运行时间增加大约n log n),但我仍然不确定.
我的代码看起来很简单.我按原样进行排序.我见过的其他实现使用了分区函数,我没有.这让我觉得我已经实现了一些其他的排序算法.这是一个快速排序算法吗?
public static void QuickSortInPlace(int[] arr)
{
QuickSortInPlace(arr, 0, arr.Length - 1);
}
private static void QuickSortInPlace(int[] arr, int left, int right)
{
if (left < right)
{
//move the middle int to the beginning and use it as the pivot
Swap(arr, left, (left + right) / 2);
int pivotIndex = left;
int pivot = arr[pivotIndex];
int min = left + 1;
int max = right;
for (int i = min; …Run Code Online (Sandbox Code Playgroud) 我正在 WPF 中学习 ICommands,但遇到了一些简单代码的问题。我有一个带命令的按钮。如果我将命令参数设置为这样的静态值CommandParameter="100",则parameterCanExecute中的参数值为 100,但是当我通过这样的绑定设置命令参数CommandParameter="{Binding}"的值时parameter,CanExecute中的参数值为 null。
这是我的 ICommand:
internal class MyCommand : ICommand
{
public bool CanExecute(object parameter) //parameter is null
{
var datacontext = parameter as MyDataContext;
if (datacontext == null)
return false;
return datacontext.IsChecked == true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
这是 XAML 代码。请注意,我在设置 Command 之前设置了 CommandParameter。我从这里得到的。同样,如果我将 CommandParameter 更改为类似CommandParameter="100",代码会按照我的预期运行(即,参数为 100,不为空)。
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<cmd:MyCommand x:Key="kCmd" /> …Run Code Online (Sandbox Code Playgroud) 我在 SQL Server 中有一个表。我想使用用户定义的表类型(示例here)将行批量上传到此表中。为此,我需要创建一个与目标表格式相同的用户定义表类型。
有没有简单的方法可以做到这一点,或者我必须手动复制所有列信息?
换句话说,我正在寻找这个问题的反面(根据用户定义的类型创建 SQL Server 表)。
我想转这个:
CREATE TABLE [dbo].[MyTable]
(
AAA [int] NOT NULL,
BBB [varchar](50) NOT NULL,
CCC [datetime] NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
进入这个:
CREATE TYPE [dbo].[udtMyTable]
AS TABLE
(
AAA [int] NOT NULL,
BBB [varchar](50) NOT NULL,
CCC [datetime] NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
仅进行文本替换是否安全?
我有一个用于在 Python (3.6) 中测试异步代码的方法:
@asyncio.coroutine
def coroutine_creater(value):
return value
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
async def test_test():
my_mock = Mock(return_value=coroutine_creater(5))
# I call with "await"
first_call = await my_mock()
second_call = await my_mock()
assert first_call == 5, 'first call failed'
assert second_call == 5, 'second call failed' # this call fails
Run Code Online (Sandbox Code Playgroud)
这样我就可以为异步调用创建模拟。我发现如果我调用异步方法两次,这不起作用。在我的代码中,first_call如我所料,等于 5,但second_call等于 None。这里发生了什么?如何测试多次调用 Mock 异步方法的代码?
我正在一个有角度的应用程序中研究网络套接字。我让它通过 nginx 连接到 python 后端。我发现大约 90% 的时间我都会收到 502“Bad Gateway”错误。我会这样做:
我不明白为什么会发生这种情况。我不知道为什么会收到 502 错误。我也无法弄清楚为什么进行硬重新加载可以解决问题。我尝试过的事情:
我应该寻找什么来帮助我解决这个问题?
编辑:这是我的 nginx conf.d 文件:
server {
listen 80;
index index.html;
root /var/www/mysite;
location / {
access_log /var/log/nginx/mysite/ui.access.log;
error_log /var/log/nginx/mysite/ui.error.log;
try_files $uri $uri/ /index.html;
}
location /ws/ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade"; …Run Code Online (Sandbox Code Playgroud) 我有一个类可以保存shared_ptr到另一个类.我收到一个编译错误,shared_ptr声明"没有使用此类型定义的成员".我复制此代码的代码很短:
#include <iostream>
#include <boost/shared_ptr.hpp>
class MyClassImpl
{
};
class MyClass
{
public:
boost::shared_ptr<MyClassImpl> _sptr;
//error C2208: 'boost::shared_ptr<T>' : no members defined using this type
};
int main()
{
MyClass mc;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我是带有Boost 1.54的Visual Studio Professional 2010.
我有一个关于C#中方法重载的问题.我有一个父类和一个子类.
class Parent
{
public virtual string GetMyClassName()
{
return "I'm a Parent";
}
}
class Child : Parent
{
public override string GetMyClassName()
{
return "I'm a Child";
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个静态方法在这两个类之外声明,它们作用于任何一种类型的对象:
static string MyClassName(Parent parent)
{
return "That's a Parent";
}
static string MyClassName(Child child)
{
return "That's a Child";
}
Run Code Online (Sandbox Code Playgroud)
当我测试这些方法是如何被调用的时候,我得到了我认为奇怪的结果:
Parent p = new Child();
var str1 = MyClassName(p); // = "That's a Parent"
var str2 = p.GetMyClassName(); // = "I'm a Child"
Run Code Online (Sandbox Code Playgroud)
为什么str1设置为"那是父母"?我可能误解了C#中的方法重载.有没有办法强制代码使用Child调用(将str1设置为"That is …
我有一个简单的F#区分联合,它结合了bool,一个字符串和一个浮点数.我想覆盖此联合的Object.Equals(arg),以便在检查浮点相等时我可以放入epsilon来解决精度错误.编译器抱怨说如果我覆盖this.Equals(arg),我也应该重写this.GetHashCode()和this.CompareTo(arg).对于这些覆盖,我没有计划特殊功能,所以我只想调用这些方法的默认版本.在我的实现中,我有三次调用GetHashCode,并且对我所区分的并集中的每种类型调用CompareTo三次:每种类型一次.
有没有办法只用一次GetHashCode调用来编码GetHashCode覆盖?CompareTo的问题是什么?我所区分的联盟中的所有类型都实现了ICompareable.
[<CustomEquality;CustomComparison>]
type MyType =
| Bool of bool
| Str of string
| Float of float
override this.Equals(arg) =
let epsilon = 0.1
match arg with
| :? MyType as other ->
match this, other with
| Bool(a), Bool(b) -> a = b
| Str(a), Str(b) -> a = b
| Float(a), Float(b) -> Math.Abs(a - b) < epsilon
| _ -> false
| _ -> false
override this.GetHashCode() =
match this with
// Three calls to GetHashCode. …Run Code Online (Sandbox Code Playgroud) c# ×4
algorithm ×1
boost ×1
c++ ×1
eclipse ×1
f# ×1
gethashcode ×1
hashcode ×1
icommand ×1
icomparable ×1
in-place ×1
java ×1
mocking ×1
nginx ×1
overloading ×1
python-3.x ×1
quicksort ×1
reflection ×1
shared-ptr ×1
sql-server ×1
warnings ×1
websocket ×1
wpf ×1