面向对象的编程语言,例如java,c#,...支持对象类型转换.例如,这在java中完全有效:
URL url = new URL("url");
URLConnection conn = url.openConnection();
if( !conn instanceof HttpURLConnection )
throw new Exception("not http request");
HttpURLConnection con = (HttpURLConnection) conn;
Run Code Online (Sandbox Code Playgroud)
或者我试过的另一个基本例子:
public class Base
{
public void base(){}
}
public class Derived extends Base
{
public void derived(){}
}
Base b = new Derived();
b.base();
Run Code Online (Sandbox Code Playgroud)
派生类具有基类具有的所有方法,以及更多.没有理由不能通过调用派生类构造函数来创建基类.
我还看到了这个链接http://www.volantec.biz/castingObjects.htm,它解释了对象类型转换如何在内部工作.到目前为止,好的.
但是为什么第一个例子不使用HttpURLConnection con = new HttpURLConnection("url address")
(我知道HttpURLConnection是一个抽象类).它看起来更清晰,更简单.另一方面,当您处理接口时,对象类型转换就派上用场了.另一个例子是List<Object>
list,我有时会在某些类中看到它.这意味着您可以在此列表中保存所有可能的类.之后,如果您知道它是什么类型,您可以将其转换为原始版本.那岂不是更清楚,只保存特定的类列出,即List<String>
,List<MyClass>
.List<Object>
那么使用好的设计实践呢?
Width
通过设置为 来使三列具有相同的宽度Auto
。
<Grid x:Name="myGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0">One</Label>
<Label Grid.Row="0" Grid.Column="1" x:Name="label1">Two</Label>
<Label Grid.Row="0" Grid.Column="2">Three</Label>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我想实现的是,如果中间列折叠或隐藏,则其他两列占用剩余空间并获得相同的宽度。
如果我只是因为宽度=“*”而将可见性设置为折叠或隐藏,则其他两列宽度保持不变。
<Label Grid.Row="0" Grid.Column="1" Visibility="Collapsed">Two</Label>
Run Code Online (Sandbox Code Playgroud)
我通过以编程方式将第二列宽度设置为自动来实现所需的功能,但我正在寻找其他一些解决方案(最好是 xaml 方式一)。
private void Button_Click(object sender, RoutedEventArgs e)
{
this.myGrid.ColumnDefinitions[1].Width = GridLength.Auto;
this.label1.Visibility = Visibility.Collapsed;
}
Run Code Online (Sandbox Code Playgroud) 我想答案很简单:
List<int?> intList = new List<int?>(){null, null, null};
int? sum = list.Sum();
int sum2 = list.Sum(a => a.HasValue ? a.Value : 0);
int sum3 = list.Sum().Value;
Run Code Online (Sandbox Code Playgroud)
Sum 总是返回 0,为什么根本需要 nullable?有没有办法强制 linq sum 返回 null?我在这里缺少什么?
在处理数据库中未找到的记录时,我不确定什么是首选方法。编写返回 null 的 Find 方法或返回的 Get 方法更好RecordNotFoundException
吗?
[AuthenticateFilter(UsernameAndSecretKey)]
[Route("api/v1/activities/emails/{id}")]
[HttpGet]
public IHttpActionResult GetEmailActivity(int id)
{
try
{
// business logic service, could use only db service but this way we can do unit tests (just fill bl service method with fake objects)
var service = new EmailActivityBlService();
// 1. use Find method which returns null in case record with provided id does not exist in db
var model = service.FindActivity(id);
if( model != null )
return Ok(model);
return …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Angular 8(在 Visual Studio 2019 中启动了一个项目)并拥有fabric.init.ts
包含以下内容的文件:
import 'fabric';
import * as fabric from 'fabric/fabric-impl';
// (TS) Property 'DPI' does not exist on type 'typeof import(..@types/fabric/fabric-impl)'
fabric.DPI = 213;
....
Run Code Online (Sandbox Code Playgroud)
接下来就可以了,因为它符合 d.ts 定义:
// Override fabric to meet ourrequirements
fabric.util.object.extend(fabric.Object.prototype, {
centeredRotation: true,
Run Code Online (Sandbox Code Playgroud)
如何忽略DPI
TS2339 错误。拥有自动补全功能真是太棒了,特别是对于学习结构而言。
我的package.json:
"private": true,
"dependencies": {
"@angular/core": "8.2.14",
....
"@angular/router": "8.2.14",
"@types/fabric": "3.5.1",
"fabric": "3.5.1",
...
},
Run Code Online (Sandbox Code Playgroud)
编辑
现在我通过添加这一行解决了这个问题:
// @ts-ignore
fabric.DPI = 213;
Run Code Online (Sandbox Code Playgroud)
我知道这不是最好的解决方案,我更喜欢@wentjun 的解决方案。
通过更改此:我import * as fabric from …
我想等到 dotnet 托管捆绑包安装完成。通常会有某种返回代码:
PS D:\app> .\dotnet-hosting-6.0.4-win.exe /install /passive /norestart
Run Code Online (Sandbox Code Playgroud)
Msi不存在,还有其他办法吗?
我正在使用FileHelper 2.0来解析我的csv数据.filehelper有没有可以正确处理转义分隔符的选项?它可以将字段标识为数据而不是分隔符.
我们的csv格式:使用\来转义逗号(,)
示例数据:
姓名,姓氏
尼科\,文件,opeka
当前代码:
[DelimitedRecord(",")]
public class contactTemplate
{
public string firstName;
public string lastName;
}
Run Code Online (Sandbox Code Playgroud)
我如何获得firstName = nico,le和lastName = opeka.FileHelpers按逗号分割,现在返回:
firstName - > nico \
lastName - >,le,opeka
我必须验证下一个字符串格式:
text-text-id-text
Run Code Online (Sandbox Code Playgroud)
分隔符是字符' - '.第三列必须始终为id.我写了下一个regex(在python中)验证字符串:
import re
s = 'col1-col2-col3-id' # any additional text at the end
# is allowed e.g. -col4-col5
print re.match('^(.*-){3}id(-.*)?$', s) # ok
print re.match('^(.*-){1}id(-.*)?$', s) # still ok, is should not be
Run Code Online (Sandbox Code Playgroud)
我尝试添加非贪婪模式,但结果仍然相同:
^(.*?-){1}id(-.*)?$
Run Code Online (Sandbox Code Playgroud)
我的正则表达式中缺少什么?我可以像这样验证字符串:
>>> import re
>>> print re.split('-', 'col1-col2-col3-id')
['col1', 'col2', 'col3', 'id']
Run Code Online (Sandbox Code Playgroud)
然后检查第三个元素是否与id匹配,但我感兴趣的是为什么第一个正则表达式如上所述.
正如我们所知,Unicode 的发明是为了解决代码页问题并表示世界上所有(不是所有但大多数)语言的所有字符。接下来我们有 unicode 转换格式 - 如何以计算机字节表示 unicode 字符:
到目前为止,还好。接下来我们以两种语言为例:
英国的英语 (en-GB) 和斯洛文尼亚的斯洛文尼亚语 (sl-SI)。英语有下一个字符:a, b, c, d, e, ... x, y, z。除了 x,y 之外,斯洛文尼亚语具有相同的字符,并且还有其他字符:?、š、ž。如果我运行以下代码:
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
string upperCase = "?".ToUpper(); // returns ?, which is correct based on sl-SI culture
// returns ?, how does it know that it must convert ? to ?.
// What if some other …
Run Code Online (Sandbox Code Playgroud) 我的下一张表有近 200 万条记录,当然每天都在增加。一些表记录(columnId 下方是父表的外键,例如 DirectionId --> 方向表,...):
Id TypeId DirectionId UserId IndicatorId Date Size ExternalId
2003 100 1 1 1 2015-06-01 00:02:23.0000000 11931 28657340
2004 2 1 2 1 2015-06-01 00:03:21.0000000 10358 28657341
2005 2 2 2 1 2015-06-01 00:03:31.0000000 10848 28657342
2006 100 1 2 1 2015-06-01 00:03:52.0000000 7860 28657343
2007 100 1 3 1 2015-06-01 00:03:59.0000000 13353 28657344
Run Code Online (Sandbox Code Playgroud)
我需要获取最后一条消息 TypeId 和 DirectionId 的日期时间。下面的查询返回我需要的
select TypeId, DirectionID, max(date) as Date
from message
group by TypeId, DirectionID;
DirectionId TypeId Date …
Run Code Online (Sandbox Code Playgroud) c# ×4
.net ×1
.net-core ×1
angular ×1
casting ×1
csv ×1
fabricjs ×1
filehelpers ×1
group-by ×1
installation ×1
linq ×1
python ×1
regex ×1
sql-server ×1
string ×1
typescript ×1
unicode ×1
wpf ×1
wpf-grid ×1
xaml ×1