考虑Singleton:
public final class MySingleton {
private static class Nested
{
private static final MySingleton INSTANCE = new MySingleton();
}
private MySingleton()
{
if (Nested.INSTANCE != null)
{
throw new IllegalStateException("Already instantiated");
}
}
public static MySingleton getInstance()
{
return Nested.INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有放任何锁,但为什么这是Singleton问题的线程安全解决方案?
考虑一下代码:
const int MAX = 3;
int (*array)[MAX] ;
int intArray[3] = {30 , 40 , 50};
array = &intArray;
for (int h = 0 ; h < MAX ; h++)
{
std::cout << *(array[h]) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
30
0
3
Run Code Online (Sandbox Code Playgroud)
显然,数组的初始化有问题,为什么我会得到
30 , 0 , 3而不是30,40,50 ?
我想允许用户只输入以下模式:
9720545455454
056565656345
03-43434344
0546-4234234
*9090
+ 97203-0656534
意思是,我不想让用户一起乱搞一切,比如:
+ 954-4343 + 3232*4343 + -
+ -4343- + 5454 + 9323 + 234
我该如何修复这个模式
public static bool IsPhoneNumberCorrect(string phoneNumber)
{
return Regex.IsMatch(phoneNumber, @"^[0-9*+-]+$");
}
Run Code Online (Sandbox Code Playgroud)
为了这个目的?
考虑以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Foo
{
public int x { get; set; }
public String y { get; set; }
public Foo()
{
this.x = 1;
this.y = "Jack";
}
}
class Testing
{
public static void funcChange(Foo bar)
{
bar.x = 2;
bar.y = "Inga";
}
public static void funcNull(Foo bar)
{
bar = null;
}
public static void Main()
{
Foo foo = new Foo();
Foo foo2 …Run Code Online (Sandbox Code Playgroud) 我有两个彼此没有联系的类:
public class A
{
public String Address {get;set}
}
public class B
{
public String Address {get;set}
}
List<A> addressList = DB.Addresses.GetAll();
Run Code Online (Sandbox Code Playgroud)
当我做
List<B> addressListOther = addressList.Cast<B>().ToList();
Run Code Online (Sandbox Code Playgroud)
输出是:
附加信息:无法将"A"类型的对象强制转换为"B"类型.
知道怎么解决这个问题吗?