gpt4 book ai didi

java - java中的继承函数

转载 作者:行者123 更新时间:2023-11-30 03:39:55 25 4
gpt4 key购买 nike

Disabling inherited method on derived class 的重复项

这是我的类(class)代码:

class Parent{
public bool sum(int a){return true;}

public int mul(int a){return a*a;}

}

还有另外两个扩展父类的类:

class derived extend parent{
//here this child can see two methods of parent class and
//I want just see sum method in here and don't see mul method
}
class derivedb extend parent{
//here this child can see two methods of parent class and
//I want here just see mul method.
}

最佳答案

您可以通过将 mul 作为父类中的私有(private)方法来实现。因此 mul 方法仅在 Parent 类内可见

class Parent{
public bool sum(int a){return true;}

private int mul(int a){return a*a;}

}

关于java - java中的继承函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27016121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com