【问题标题】:How to make variable in companion object protected如何使伴随对象中的变量受保护
【发布时间】:2022-01-13 02:48:42
【问题描述】:

我想在伴随对象中创建一个受保护的变量。我认为当我使变量受保护时,它只能从继承变量类的另一个类访问,而不是从其他类或函数访问。但它不起作用。

这是我的代码

open class Population{
    companion object{
        @JvmStatic // Though I added it because of recommendation of IntelliJ, It still doesn't work.
        protected var population_quantity: Int = 0
    }
}

class Marin : Population(){
    init{
        population_quantity += 1 // I want to make it possible
    }
}

class Checker : Population() {
    fun printPopulationQuantity()
    {
        println(population_quantity) // I also want to make it possible
    }
}

fun main(){
    var m1 = Marin()
    var checker = Checker()

    // But I want to make codes below here impossible
    // Population.population_quantity += 1
    // println(Population.population_quantity)
}

以下是我想要通过 C++ 制作的内容。我希望下面能澄清我在说什么。

#include <iostream>

class Population
{
    protected:
        static int population_quantity;
};
int Population::population_quantity = 0;

class Marin : Population
{
    public:
        Marin()
        {
            std::cout << "Marin is generated" << std::endl;
            population_quantity += 1;
        }
        ~Marin()
        {
            std::cout << "Marin is dead" << std::endl;
            population_quantity -= 1;
        }
};

class Checker : Population
{
    public:
        void printPopulationQuantity()
        {
            std::cout << population_quantity << std::endl;
        }
};

int main()
{
    Checker checker;
    checker.printPopulationQuantity();
    Marin *m1 = new Marin();
    checker.printPopulationQuantity();
    
    // std::cout << "population : " << Population::population_quantity << std::endl;
    // Can't access population_quantity from main which doesn't inherite Population
    // because Population::population_qantity is protected.
    // But it is possible to access Population:population_quantity from Marin which inherits Population.

    delete m1;
    checker.printPopulationQuantity();
    return 0;
}

当你帮我解决这个问题时,我相信我会从心底里感激不尽。

【问题讨论】:

  • 和c++有什么关系?
  • @ÖöTiib 如果您指的是标签,我只是添加它以正确突出显示 C++ 代码。如果仅出于此目的添加 C++ 标签不合适,我将删除它。
  • 哦。请参阅stackoverflow.com/editing-help,了解如何以不同方式指示不同代码 sn-ps 的突出显示语言。

标签: kotlin protected companion-object


【解决方案1】:

这可能是因为在class Checker : Population 中你有一个在public 中的 void 函数。 尝试将其更改为:

class Checker : Population
{
    protected:
        void printPopulationQuantity()
        {
            std::cout << population_quantity << std::endl;
        }
};

【讨论】:

  • 您的回答是否意味着删除class Checker : Population 可以解决这个问题?很抱歉,但是当删除class Checker : Population 时,它仍然不起作用,留下相同的错误消息Kotlin: Using non-JVM static members protected in the superclass companion is unsupported yet。
  • 好了。我已经对其进行了编辑以解释我的意思
  • 哦,现在我明白你的意思了。首先,感谢您的帮助。但我并不打算询问 C++。也许我错误的标签选择让你感到困惑。我问了科特林。我添加了运行良好的 C++ 代码来说明我的意图。
  • 在这种情况下,请阅读以下内容:kotlinlang.org/docs/visibility-modifiers.html#class-members
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 2019-12-31
  • 2011-08-02
  • 1970-01-01
相关资源
最近更新 更多