小编Alb*_*ola的帖子

Delphi在构造函数中引发异常

情况

我打算写一个类,构造函数是我自己创建的一个,因为我需要初始化一些值.这是我到目前为止编写的代码:

type
 TCombinatorio = class(TObject)
  private
   valN, valK: integer;
   result: double;
  public
   property K: integer read valK;
   property N: integer read valN;
   constructor Create(valN: integer; valK: integer);
 end;

constructor TCombinatorio.Create(valN: Integer; valK: Integer);
begin
  inherited Create;
   Self.valN := valN;
   Self.valK := valK;

  if ((valN < 0) or (valK < 0)) then
   begin
    raise Exception.Create('N and K must be >= 0');
   end;

end;
Run Code Online (Sandbox Code Playgroud)

由于我要做一些数学计算,我需要避免负数.


我可以用这种方式在构造函数中调用异常吗?我正在以这种方式运行代码:

procedure TForm1.Button1Click(Sender: TObject);
var a: TCombinatorio; 
    b: string;   
begin

 a := TCombinatorio.Create(5,-2);

 try
  //some …
Run Code Online (Sandbox Code Playgroud)

delphi

17
推荐指数
1
解决办法
2262
查看次数

Delphi是否真的需要记录构造函数?

情况

我正在研究Nick Hodges的"更多编码在Delphi中",他正在使用TFraction记录来解释运算符重载.我自己写了这样的记录:

type
  TFraction = record
  strict private
    aNumerator: integer;
    aDenominator: integer;
    function GCD(a, b: integer): integer;
  public
    constructor Create(aNumerator: integer; aDenominator: integer);
    procedure Reduce;
    class operator Add(fraction1, fraction2: TFraction): TFraction;
    class operator Subtract(fraction1, fraction2: TFraction): TFraction;
    //... implicit, explicit, multiply...
    property Numerator: integer read aNumerator;
    property Denominator: integer read aDenominator;
  end;
Run Code Online (Sandbox Code Playgroud)

当然,我必须创建一个构造函数,因为在Q(有理数)中我必须有一个不等于零的分母.

constructor TFraction.Create(aNumerator, aDenominator: integer);
begin
  if (aDenominator = 0) then
  begin
    raise Exception.Create('Denominator cannot be zero in rationals!');
  end;

  if ( (aNumerator < 0) …
Run Code Online (Sandbox Code Playgroud)

delphi constructor records

11
推荐指数
1
解决办法
3214
查看次数

Delphi自我关键字

我正在学习Delphi阅读Marco Cantu的书,它非常完整.这很清楚,但我对关键字有疑问self.我已经有了OOP的经验,我有它的基础知识.我的问题很简单.我可以将关键字self(Delphi)与关键字this(Java)进行比较吗?

当我读到关于self使用内部记录的书时,我想到了类似的东西self : Delphi = this : Java.查看我创建的代码进行测试:

type
 TMarioKart = packed record
   Character: String;
   Kart: String;
   Tires: String;
   Speed: double;
   Competitive: boolean;
  private
   air-speed: integer;
   ground-speed: integer;
   water-speed: integer;
  public
   constructor Create(Character: string);
   function ShowStats(a: TMarioKart):string; overload;
   function ShowStats(a: TMarioKart; b: TMarioKart): string; overload;
 end;
Run Code Online (Sandbox Code Playgroud)

我将切断代码的最大部分,我只是在这里展示构造函数:

constructor TMarioKart.Create(Character: string);
begin
  self.Character := Character;
end;
Run Code Online (Sandbox Code Playgroud)

self这里使用关键字我指的是记录的字符,而不是方法中传递的字符.这是使用自我的正确方法吗?它可能是Java的兄弟this吗?

delphi self

9
推荐指数
2
解决办法
1934
查看次数

由于doOutput,无法编写URLConnection

我在这里问了一个问题,我已经解决了我的问题,感谢用户.这是我目前正在使用的代码.

void UploadToDatabase() throws MalformedURLException, IOException {
          URL website = new URL("http://mk7vrlist.altervista.org/databases/record_file.txt");
          WritableByteChannel rbc = Channels.newChannel(website.openConnection().getOutputStream());

          FileOutputStream fos;
          fos = new FileOutputStream("record_file.txt");
          fos.getChannel().transferTo(0, Long.MAX_VALUE, rbc);
          fos.close();
     }
Run Code Online (Sandbox Code Playgroud)

我的目标是record_file.txt在特定的Web链接上传文件,如上所示.但是,当我尝试运行此代码时,NetBeans给了我这个错误:

java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
Run Code Online (Sandbox Code Playgroud)

我读了一些关于这个的东西,我在声明该rbc变量之后添加了以下代码.

 URLConnection urlc = website.openConnection();
 urlc.setDoOutput(true);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我总是有同样的错误.你可以帮帮我吗?

java

8
推荐指数
1
解决办法
2万
查看次数

Android WebBrowser向下滚动页面

我开发了一个Android应用程序,Delphi XE 5我放了一个WebBrowser,因为我必须显示一个网页.顺便说一下(网页)的宽度为1400px,因此不能完全显示.

在此输入图像描述

宽度是固定的,因为我在网页上使用width:100%,但正如你在图片上看到的,我只有表的一部分.我错过了一个滚动条,允许我向下滑动页面并查看表格的其余部分.

我尝试添加一个TVertScrollBox并在其中我放了WebBrowser,但我仍然只看到页面的开头.我无法向下滚动它.

我怎么能解决这个问题?该网页div左侧已经有一个滚动条.您可以单击此处查看整个页面.

html delphi android delphi-xe5

7
推荐指数
1
解决办法
685
查看次数

Delphi正确使用任务

情况

为了更好地理解PPL以及如何Task作品我试图做一个非常简单的程序中,一旦你点击一个按钮,一个ListBox充满磁盘目录列表.

procedure TForm3.Button1Click(Sender: TObject);
var proc: ITask;
begin

 //Show that something is going to happen
 Button1.Caption := 'Process...';

 proc := TTask.Create(

  procedure
  var strPath: string;
      sl: TStringDynArray;
  begin

   if (DirectoryExists('C:\Users\albertoWinVM\Documents\uni\maths')) then
    begin
     ListBox1.Items.Clear;
     sl := TDirectory.GetDirectories('C:\Users\albertoWinVM\Documents\uni\maths',
     TSearchOption.soAllDirectories, nil);

     for strPath in sl do
      begin
       ListBox1.Items.Add(strPath);
      end;

     //At the end of the task, I restore the original caption of the button 
     Button1.Caption := 'Go';
     Label1.Caption := 'Finished';

    end;
  end

 );

 proc.Start;

end;
Run Code Online (Sandbox Code Playgroud)

maths您可以在上面看到的文件夹不是很大,任务大约需要3秒钟才能执行.任务声明如下:

type
  TForm3 …
Run Code Online (Sandbox Code Playgroud)

delphi

6
推荐指数
1
解决办法
7967
查看次数

数学表达式的奇怪值

我正在开发一款适用于Windows和Android的游戏,但它有一个我无法解决的问题.基本上我有一个带有一些按钮的4x5网格,这些按钮每秒填充一个必须为2,4或8的随机数.如果点击两个具有相同数字的按钮,则计算总和.这是一个firemonkey项目.

游戏运行正常,但您可以在下面的图片中看到问题.当我在我的Windows机器运行游戏,它产生2,4或8的android下它会产生2,4,7和8随机数以这种方式创建:

valueToOutput := Trunc(Exp(Ln(2) * (1+Random(3))));
Run Code Online (Sandbox Code Playgroud)

该变量保存要在按钮中显示的数字.为什么我在windows和android中得到不同的结果?这是两个截图

我确信该函数是正确的,因为我已经绘制了它(exp(ln(2)*(1 + x))= http://prnt.sc/dmdc3u)并且当x是0,1或2时(=当随机数是0,1或2).这可能是编译器的问题吗?

注意:我已经使用您在下面看到的解决方法解决了这个问题,但首先我使用了您可以在问题中看到的代码,我想了解发生了什么.

valueToOutput := Trunc(Exp(Ln(2) * (1+Random(3))));

//this will always give 2, 4 or 8
if valueToOutput = 7 then
 valueToOutput := valueToOutput + 1;
Run Code Online (Sandbox Code Playgroud)

delphi android firemonkey

6
推荐指数
1
解决办法
200
查看次数

如何为FireMonkey组件添加设计时图标?

我已经阅读了PawełGłowacki的这篇文章,我已经能够为我的组件显示一个图标.结果如下:

在此输入图像描述

我可以在工具选项板和结构视图中看到图像.顺便说一下,在设计器中我看到了默认图标:

在此输入图像描述

如何在设计器中显示组件的图标?


我正在使用Delphi Tokyo 10.2 Update 2.我已经按照我链接的文章来显示图像.我的组件如下:

type
  TEquationSolver = class(TComponent)
    //code...
  end;
Run Code Online (Sandbox Code Playgroud)

基本上,我做了以下事情:

  1. 我创建了3个位图(16x16 24x24 32x32)和一个png(128x128)
  2. 我已将它们作为资源添加到Project> Resources and Images中 在此输入图像描述
  3. TEquationSolver用后缀表示它们的大小.通过这种方式,它们可以正确显示在IDE上.

我在设计时间部分缺少什么?在文章中,我已经阅读以下内容:

我们的指南是:如果您想要非常简单的向后兼容性或小文件(BPL)大小,请使用PNG; 如果要快速加载,请使用位图.我们使用位图为16,24和32px图标,PNG用于128px图标.

实际上我有16x16,24x24,32x32位图和128px png.还有别的吗?

delphi firemonkey delphi-10.2-tokyo

6
推荐指数
1
解决办法
199
查看次数

增加JTable中的行数

我正在为我的同学们编写通讯录,但是我遇到了问题JTable。在这里,您可以看到程序的预览,我正在使用NetBeans [ 单击 ]。如果单击Add to the Address Book,程序将在该表中添加新行,并用下面文本字段中的数据填充其单元格。我正在使用以下代码,但行数不会增加。

界面原样

 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        int h;
        DefaultTableModel model = new DefaultTableModel();
        h=jTable1.getRowCount()+1;
        model.setRowCount(h);
        jTable1.setValueAt(jTextField2.getText(), h, 1);
        jTable1.setValueAt(jTextField3.getText(), h, 2); 
        //I'll use more setValueAt() because I must fill all the cells
    } 
Run Code Online (Sandbox Code Playgroud)

您能给我一些有关如何解决此问题的建议吗?

java swing jtable

5
推荐指数
1
解决办法
5000
查看次数

Vert.x将http重定向到https

我们用Vert.x 4重新编写了我们的Web服务,我们感到非常满意。在将它们投入生产之前,我们要保护它们安全,并尝试启用https。这是主要版本:

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start() throws Exception {
    //Deploy the HTTP server
    vertx.deployVerticle(
      "com.albertomiola.equations.http.HttpServerVerticle",
      new DeploymentOptions().setInstances(3)
    );
  }

  // I use this only in IntelliJ Idea because when I hit "Run" the build starts
  public static void main(String[] args) {
    Launcher.executeCommand("run", MainVerticle.class.getName());
  }

}
Run Code Online (Sandbox Code Playgroud)

这是HTTP服务器Verticle代码中最相关的部分:

public class HttpServerVerticle extends AbstractVerticle {

  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    var options = new HttpServerOptions();
    options.setPort(443)
           .setSSl(true)
           .setPemTrustOptions(...)
           .setPemKeyCertOptions(...);

    var server = vertx.createHttpServer(options);
    var …
Run Code Online (Sandbox Code Playgroud)

java vert.x java-11 vertx4

5
推荐指数
1
解决办法
147
查看次数