简体   繁体   English

这个警告的含义是什么?

[英]What's the meaning of this warning in solidity?

When I was writing my code I got warning at 10th line of my code.当我编写代码时,我在代码的第 10 行收到警告。 Can anyone tell me what's this warning means?谁能告诉我这个警告是什么意思?

My Code我的代码

// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.5.0 < 0.9.0;

contract PracticeTest // It's a class
{
    string name ;
    uint256 age;

    constructor() public
    {
        name = "Ali";
        age = 21 ;
    }
}

This is the Warning这是警告

Warning: Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.
  --> contracts/PracticeTest.sol:10:5:
   |
10 |     constructor() public
   |     ^ (Relevant source part starts here and spans across multiple lines).

Visibility ( public / internal ) is not needed for constructors anymore: To prevent a contract from being created, it can be marked abstract .构造函数不再需要可见性( public / internal ):为了防止创建合同,可以将其标记为abstract This makes the visibility concept for constructors obsolete.这使得构造函数的可见性概念过时了。

Source: https://docs.soliditylang.org/en/v0.8.13/070-breaking-changes.html#functions-and-events资料来源: https ://docs.soliditylang.org/en/v0.8.13/070-break-changes.html#functions-and-events


So, if you're compiling your contract with Solidity version 0.7 or newer, the constructor visibility (in your case public ) is ignored, and you can safely remove it.因此,如果您使用 Solidity 0.7 或更高版本编译合约,构造函数可见性(在您的情况下为public )将被忽略,您可以安全地删除它。

constructor()
{
    name = "Ali";
    age = 21 ;
}

I am just adding to the answer of Petr Hejda .我只是补充Petr Hejda的答案。 Prior to solidity compiler versions 0.7.0 we were required to provide the visibility of constructors as either public or internal .在 Solidity 编译器版本 0.7.0 之前,我们需要将构造函数的可见性提供为publicinternal With public visibility the contract can be directly deployed by itself on the blockchain whereas if the contract is not intended to be created directly but rather inherited, then we declared the constructor with internal visibility.public可见性的情况下,合约可以直接部署在区块链上,而如果合约不是直接创建而是继承,那么我们声明了具有internal可见性的构造函数。
However, for solidity compiler versions 0.7.0 or greater we do the same thing but differently.然而,对于 Solidity 编译器版本0.7.0或更高版本,我们做同样的事情但不同。 If we want the contract not to be deployed directly but inherited we declare the contract itself as abstract .如果我们不希望合约直接部署而是继承,我们将合约本身声明为abstract If we want that the contract can be deployed directly and can also be inherited, we don't declare it as abstract contract and we don't specify any visibility in the constructor of that class.如果我们希望合约可以直接部署也可以被继承,我们不会将其声明为抽象合约,也不会在该类的构造函数中指定任何可见性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM