我创建了一个匿名类,其中我声明了一些变量和方法.我的java老师告诉我要把这些私有化.我没有看到改变修饰符有什么不同,因为无论如何这些变量和方法对于匿名类是私有的,所以我更喜欢根本没有修饰符.谁是对的,什么更有意义?请参阅下面的示例代码,其中我选择"map"和"convert"的修饰符而不是将其设为私有.
Collections.sort(list, new Comparator<String>(){
public int compare(String a, String b){
return convert(a).compareTo(convert(b));
}
Map<String, String> map = new HashMap<String, String>();
String convert(String s) {
String u = map.get(s);
if (u == null)
map.put(s, u = s.toUpperCase());
return u;
}
});
Run Code Online (Sandbox Code Playgroud) 我在Java中有一个"私有静态"嵌套类.访问修饰符对于此类中的字段和方法有何意义?我尝试过公开和私有对我的应用程序没有影响.
public class MyList<T>{
private static class Node{ // List node
private Object item;
private Node next;
private Node prev;
private Node(Node next){
this.next = next;
}
private static Node doStuff(){}
}
}
Run Code Online (Sandbox Code Playgroud) 在Visual Studio中编写C++时,它坚持在访问修饰符上给我这些神奇可怜的修正 - 如果有人真的喜欢这种方式,我表示哀悼;)(一个笑话人!)
public class MyClass
{
public:
MyClass();
~MyClass();
int wowAnInt();
}
Run Code Online (Sandbox Code Playgroud)
不用说,我想要这个:
public class MyClass
{
public:
MyClass();
~MyClass();
int wowAnInt();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法用任何东西(我有Resharper和Highlighter)或者香草VS来实现这个目标?
可能重复:
C#:为什么我的公共类不能扩展内部类?
如果以前曾问过这个问题,我道歉.我有点搜索,无法找到它.
我只是好奇这个设计背后的理由是什么.显然,我理解基类型的私有/内部成员不能,也不应该通过派生的公共类型公开.但我天真的想法似乎是,"隐藏"的部分可以很容易地隐藏起来,而一些基本功能仍然是共享的,并且新的界面是公开的.
我正在考虑这些方面的事情:
internal class InternalClass
{
protected virtual void DoSomethingProtected()
{
// Let's say this method provides some useful functionality.
// Its visibility is quite limited (only to derived types in
// the same assembly), but at least it's there.
}
}
public class PublicClass : InternalClass
{
public void DoSomethingPublic()
{
// Now let's say this method is useful enough that this type
// should be public. What's keeping us from leveraging the
// base …Run Code Online (Sandbox Code Playgroud) 我知道这适用于许多语言,而不仅仅是Java,但这是我最熟悉的语言.
我理解修饰符的作用以及如何使用它们.我只是想知道,为什么我们需要它们?为什么不能访问每个对象,无论它是否需要?
以前在C#工作过,我现在花了很多时间在Scala和Java上工作.这可能令人困惑,因为这三种语言的访问修饰符使用相似的名称,但并不总是意味着同样的事情.
Java和Scala中C#的访问修饰符的等价物是什么?
C#,Java和其他编程语言中的类,属性或方法的访问修饰符实际上是否会影响应用程序的安全性?他们是否也以某种方式防止未经授权的访问?或者它们只是清晰和适当编程的工具?
package example
class Apple {
val APPLE_SIZE_KEY: String = "APPLE_SIZE_KEY"
}
Run Code Online (Sandbox Code Playgroud)
类:
package example
class Store {
fun buy() {
val SIZE = Apple.APPLE_SIZE_KEY
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
'APPLE_SIZE_KEY'在'example.Apple'中拥有私人访问权限
但官方文档描述了如果我们不指定任何可见性修饰符,public则默认使用.
我的第二个问题是:
为什么会出现上述错误?
我正在使用 Visual C++,如果我编译这段代码:
class A {};
class B : private A {};
class C : public B
{
void func()
{
A a{};
}
};
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
错误 C2247:“A”不可访问,因为“B”使用“私有”从“A”继承
我知道如果我使用私有继承,那么类 'A' 的成员在 'B' 中将是私有的,在 'C' 中不可访问,但是为什么我不能在 'C' 中创建一个 'A' 的对象?
在我的另一个问题中,我问如何仅公开公开Foo<u32>私有泛型类型 () 的具体变体 ( Foo<T>)。建议的解决方案如下:
mod internal {
/// Private documentation of `Foo`.
pub struct Foo<X> {
/// Private documentation of `x`.
pub x: X,
}
impl Foo<u32> {
pub fn foo() -> u32 {
32
}
}
impl Foo<u8> {
pub fn foo() -> u8 {
8
}
}
}
/// Public documentation of `FooBar`.
pub type FooBar = internal::Foo<u32>;
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为公共 API 只包含FooBar,但不包含Foo。然而,从文档的角度来看,它是缺乏的。cargo doc这是for的输出FooBar:
如你看到的,
access-modifiers ×10
java ×5
c# ×3
c++ ×2
inheritance ×2
.net ×1
android ×1
class ×1
generics ×1
indentation ×1
kotlin ×1
methods ×1
name-lookup ×1
rust ×1
rustdoc ×1
scala ×1
scope ×1
security ×1
static ×1
variables ×1